Skip to content

Commit cba2df5

Browse files
authored
Merge pull request #169 from csandman/is-disabled-fix
Fix: Prevent a selected option from being removed when isDisabled is passed
2 parents 1eff922 + 38aa367 commit cba2df5

File tree

6 files changed

+60
-38
lines changed

6 files changed

+60
-38
lines changed

README.md

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ Check out these demos:
3232

3333
- [Usage](#usage)
3434
- [Extra Props](#extra-props)
35-
- [`size`](#size--options-sm-md-lg--default-md)
35+
- [`size`](#size--options-sm--md--lg--default-md)
3636
- [`colorScheme`](#colorscheme)
37-
- [`tagVariant`](#tagvariant--options-subtle-solid-outline--default-subtle)
37+
- [`tagVariant`](#tagvariant--options-subtle--solid--outline--default-subtle)
3838
- [`isInvalid`](#isinvalid--default-false)
3939
- [`focusBorderColor` / `errorBorderColor`](#focusbordercolor--default-blue500--errorbordercolor--default-red500)
4040
- [`useBasicStyles`](#usebasicstyles--default-false)
41-
- [`hasStickyGroupHeaders`](#hasstickygroupheaders--default-false)
42-
- [`selectedOptionStyle`](#selectedoptionstyle--options-color-check--default-color)
41+
- [`selectedOptionStyle`](#selectedoptionstyle--options-color--check--default-color)
4342
- [`selectedOptionColor`](#selectedoptioncolor--default-blue)
43+
- [`hasStickyGroupHeaders`](#hasstickygroupheaders--default-false)
4444
- [`isFixed`](#isfixed)
4545
- [Styling](#styling)
4646
- [`chakraStyles`](#chakrastyles)
@@ -95,13 +95,13 @@ const {
9595
} = require("chakra-react-select");
9696
```
9797

98-
All of the types and other exports from the original `react-select` package are also exported from this package, so you can import any of them if you need them.
98+
All of exports, including types, from the original `react-select` package are also exported from this package, so you can import any of them if you need them. The only exception is the root `Select` components.
9999

100-
In order to use this component, you can implement it and use it like you would normally use [react-select](https://react-select.com/home). It should accept almost all of the props that the original takes, with a few additions and exceptions listed below.
100+
Implementing this component in your application should be almost identical to how you'd normally use [react-select](https://react-select.com/home). It will accept all of the props that the original package does, with a few additions and exceptions listed below. So if you have a question on basic usage, your best bet is to check the original docs or google "How to (some functionality) with react-select" and just swap out `react-select` for `chakra-react-select`.
101101

102102
## Extra Props
103103

104-
#### `size` — Options: `sm`, `md`, `lg` — Default: `md`
104+
#### `size` — Options: `sm` | `md` | `lg` — Default: `md`
105105

106106
You can pass the `size` prop with either `sm`, `md`, or `lg` (default is `md`). These will reflect the sizes available on the [Chakra `<Input />` component](https://chakra-ui.com/docs/components/input#changing-the-size-of-the-input) (with the exception of `xs` because it's too small to work).
107107

@@ -115,6 +115,8 @@ return (
115115
);
116116
```
117117

118+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-sizes-w9sf8e?file=/example.js)
119+
118120
#### `colorScheme`
119121

120122
You can pass the `colorScheme` prop to the select component to change all of the selected options tags' colors. You can view the whole list of available color schemes in [the Chakra docs](https://chakra-ui.com/docs/components/tag/props), or if you have a custom color palette, any of the custom color names in that will be available instead.
@@ -141,7 +143,9 @@ return (
141143
);
142144
```
143145

144-
#### `tagVariant` — Options: `subtle`, `solid`, `outline` — Default: `subtle`
146+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-color-schemes-c38jlo?file=/example.js)
147+
148+
#### `tagVariant` — Options: `subtle` | `solid` | `outline` — Default: `subtle`
145149

146150
You can pass the `tagVariant` prop with either `subtle`, `solid`, or `outline` (default is `subtle`). These will reflect the `variant` prop available on the [Chakra `<Tag />` component](https://chakra-ui.com/docs/components/tag/props).
147151

@@ -167,6 +171,8 @@ return (
167171
);
168172
```
169173

174+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-tag-variants-w31gnt?file=/example.js)
175+
170176
#### `isInvalid` — Default: `false`
171177

172178
You can pass `isInvalid` to the select component to style it like the Chakra `<Input />` is styled when it receives the same prop.
@@ -191,21 +197,25 @@ return (
191197
);
192198
```
193199

200+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-invalid-disabled-0hyl8l?file=/example.js)
201+
194202
#### `focusBorderColor` — Default: `blue.500` | `errorBorderColor` — Default: `red.500`
195203

196204
The props `focusBorderColor` and `errorBorderColor` can be passed with Chakra color strings which will emulate the respective props being passed to [Chakra's `<Input />` component](https://chakra-ui.com/docs/components/input#changing-the-focus-and-error-border-colors).
197205

198206
```js
199207
return (
200208
<>
209+
<Select errorBorderColor="orange.500" isInvalid />
201210
<Select focusBorderColor="green.500" />
202-
<Select errorBorderColor="orange.500" />
203211
</>
204212
);
205213
```
206214

207215
![Orange errorBorderColor](./github/custom-error-border.png)
208216

217+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-border-colors-gjo4zd?file=/example.js)
218+
209219
#### `useBasicStyles` — Default: `false`
210220

211221
If this prop is passed, the dropdown indicator at the right of the component will be styled in the same way [the original Chakra `Select` component](https://chakra-ui.com/docs/components/select) is styled, instead of being styled as an [`InputRightAddon`](https://chakra-ui.com/docs/components/input#left-and-right-addons). The original purpose of styling it as an addon was to create visual separation between the dropdown indicator and the button for clearing the selected options. However, as this button only appears when `isMulti` is passed, using this style could make more sense for a single select.
@@ -220,23 +230,11 @@ return (
220230

221231
![useBasicStyles dark mode](./github/use-basic-styles-dark.png)
222232

223-
#### `hasStickyGroupHeaders` — Default: `false`
224-
225-
One additional feature which isn’t specific to Chakra or react-select is sticky group headers. It adds a border to the bottom of the header and keeps it in view while its corresponding group of options is visible. This can be very nice for when you have long lists of grouped options so you can always tell which group of options you're looking at. To add it, pass the `hasStickyGroupHeaders` prop to the select component.
226-
227-
```js
228-
return <Select hasStickyGroupHeaders />;
229-
```
230-
231-
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-hasstickygroupheaders-wg39g?file=/example.js)
232-
233-
**NOTE:** It has recently been discovered that when using this prop, navigating up through the available options with the arrow key will keep the focused option underneath the header, as it will not scroll enough to account for it being there. So if this is an issue for you, avoid this prop. A fix for this is being investigated.
234-
235-
![Sticky Group Headers](./github/sticky-group-headers.png)
233+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-usebasicstyles-jjnqsd?file=/example.js)
236234

237-
#### `selectedOptionStyle` — Options: `color`, `check` — Default: `color`
235+
#### `selectedOptionStyle` — Options: `color` | `check` — Default: `color`
238236

239-
In `v1.3.0` you can now pass the prop `selectedOptionStyle` with either `"color"` or `"check"` (defaults to `"color"`). The default option `"color"` will style a selected option similar to how react-select does it, by highlighting the selected option in the color blue. Alternatively if you pass `"check"` for the value, the selected option will be styled like the [Chakra UI Menu component](https://chakra-ui.com/docs/components/menu#menu-option-groups) and include a check icon next to the selected option(s). If `isMulti` and `selectedOptionStyle="check"` are passed, space will only be added for the check marks if `hideSelectedOptions={false}` is also passed.
237+
As of `v1.3.0` you can pass the prop `selectedOptionStyle` with either `"color"` or `"check"`. The default option `"color"` will style a selected option similar to how react-select does it, by highlighting the selected option in the color blue. Alternatively if you pass `"check"` for the value, the selected option will be styled like the [Chakra UI Menu component](https://chakra-ui.com/docs/components/menu#menu-option-groups) and include a check icon next to the selected option(s). If `isMulti` and `selectedOptionStyle="check"` are passed, space will only be added for the check marks if `hideSelectedOptions={false}` is also passed.
240238

241239
```js
242240
return (
@@ -251,6 +249,8 @@ return (
251249

252250
![Check Highlighted Selected Option](./github/check-selected-option.png)
253251

252+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-border-selectedoptionstyle-yxkcos?file=/example.js)
253+
254254
#### `selectedOptionColor` — Default: `blue`
255255

256256
If you choose to stick with the default `selectedOptionStyle="color"`, you have one additional styling option. If you do not like the default of blue for the highlight color, you can pass the `selectedOptionColor` prop to change it. This prop will accept any named color from your color theme, and it will use the `500` value in light mode or the `300` value in dark mode.
@@ -268,6 +268,22 @@ return (
268268

269269
![Purple Selected Option Color (dark mode)](./github/purple-selected-option-dark.png)
270270

271+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-border-selectedoptioncolor-yyd321?file=/example.js)
272+
273+
#### `hasStickyGroupHeaders` — Default: `false`
274+
275+
One additional feature which isn’t specific to Chakra or react-select is sticky group headers. It adds a border to the bottom of the header and keeps it in view while its corresponding group of options is visible. This can be very nice for when you have long lists of grouped options so you can always tell which group of options you're looking at. To add it, pass the `hasStickyGroupHeaders` prop to the select component.
276+
277+
```js
278+
return <Select hasStickyGroupHeaders />;
279+
```
280+
281+
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-hasstickygroupheaders-wg39g?file=/example.js)
282+
283+
**NOTE:** It has recently been discovered that when using this prop, navigating up through the available options with the arrow key will keep the focused option underneath the header, as it will not scroll enough to account for it being there. So if this is an issue for you, avoid this prop. A fix for this is being investigated.
284+
285+
![Sticky Group Headers](./github/sticky-group-headers.png)
286+
271287
#### `isFixed`
272288

273289
In your options objects, you can add the key `isFixed: true` to emulate the example in the [react-select docs](https://react-select.com/home#fixed-options). This will prevent the options which have this flag from having the remove button on its corresponding tag, and it changes the default `tagVariant` for that tag to be solid. This only applies when using `isMulti`.
@@ -319,7 +335,7 @@ function option(provided, state) {
319335
}
320336
```
321337

322-
All of the style keys offered in the original package can be used here, except for `input` as that does not allow me to use the `chakraStyles` from the select props. The `input` styles are also much more dynamic and should be left alone for the most part.
338+
All of the style keys offered in the original package can be used in the `chakraStyles` prop except for `menuPortal`. Along with [some other caveats](#caveats), this is explained below.
323339

324340
Most of the components rendered by this package use the basic [Chakra `<Box />` component](https://chakra-ui.com/docs/components/box) with a few exceptions. Here are the style keys offered and the corresponding Chakra component that is rendered:
325341

@@ -696,17 +712,25 @@ Being a wrapper for `react-select`, all of the customizations done to react-sele
696712
To do so, simply import the hook from this package, and call it by passing in any extra custom props you'd like into it and spread it onto a base `react-select` component:
697713

698714
```jsx
715+
import { useState } from "react";
699716
import { useChakraSelectProps } from "chakra-react-select";
700717
import Select from "react-select";
718+
import { options } from "./data";
719+
720+
const CustomSelect = () => {
721+
const [selectedOptions, setSelectedOptions] = useState([]);
701722

702-
const CustomSelect = (customSelectProps) => {
703-
const selectProps = useChakraSelectProps(customSelectProps);
723+
const selectProps = useChakraSelectProps({
724+
isMulti: true,
725+
value: selectedOptions,
726+
onChange: setSelectedOptions,
727+
});
704728

705729
return <Select {...selectProps} />;
706730
};
707731
```
708732

709-
One example of how you might use this is to customize the component `react-google-places-autocomplete`, which is an autocomplete dropdown for Google Places that uses the `AsyncSelect` from `react-select` as it's core. Therefore, it accepts all of the same select props as the core react-select does which means you can use the `useChakraSelectProps` hook to style it:
733+
One example of how you might use this is to customize the component `react-google-places-autocomplete`, which is an autocomplete dropdown for Google Places that uses the `AsyncSelect` from `react-select` as it's core. Therefore, it accepts all of the same select props as the core react-select does meaning you can use the `useChakraSelectProps` hook to style it:
710734

711735
```jsx
712736
import { useState } from "react";

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chakra-react-select",
3-
"version": "4.1.4",
3+
"version": "4.1.5",
44
"description": "A Chakra UI wrapper for the popular library React Select",
55
"license": "MIT",
66
"author": "Chris Sandvik <chris.sandvik@gmail.com>",

src/chakra-components/containers.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ export const SelectContainer = <
3030
const initialSx: CSSObject = {
3131
position: "relative",
3232
direction: isRtl ? "rtl" : undefined,
33-
// When disabled, react-select sets the pointer-state to none which prevents
34-
// the `not-allowed` cursor style from chakra from getting applied to the
35-
// Control when it is disabled
36-
pointerEvents: "auto",
33+
...(isDisabled ? { cursor: "not-allowed" } : {}),
3734
};
3835

3936
const sx = chakraStyles?.container

src/chakra-components/control.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const Control = <
5959
overflow: "hidden",
6060
height: "auto",
6161
minHeight: heights[size || "md"],
62+
...(isDisabled ? { pointerEvents: "none" } : {}),
6263
};
6364

6465
const sx = chakraStyles?.control

src/module-augmentation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ declare module "react-select/dist/declarations/src/Select" {
2828
* Options: `sm` | `md` | `lg`
2929
*
3030
* @defaultValue `md`
31-
* @see {@link https://github.com/csandman/chakra-react-select#size--options-sm-md-lg--default-md}
31+
* @see {@link https://github.com/csandman/chakra-react-select#size--options-sm--md--lg--default-md}
3232
* @see {@link https://chakra-ui.com/docs/components/input#changing-the-size-of-the-input}
3333
*/
3434
size?: Size;
@@ -82,7 +82,7 @@ declare module "react-select/dist/declarations/src/Select" {
8282
* Options: "subtle" | "solid" | "outline"
8383
*
8484
* @defaultValue `subtle`
85-
* @see {@link https://github.com/csandman/chakra-react-select#tagvariant--options-subtle-solid-outline--default-subtle}
85+
* @see {@link https://github.com/csandman/chakra-react-select#tagvariant--options-subtle--solid--outline--default-subtle}
8686
* @see {@link https://chakra-ui.com/docs/data-display/tag#props}
8787
*/
8888
tagVariant?: TagVariant;
@@ -104,7 +104,7 @@ declare module "react-select/dist/declarations/src/Select" {
104104
* Options: `color` | `check`
105105
*
106106
* @defaultValue `color`
107-
* @see {@link https://github.com/csandman/chakra-react-select#selectedoptionstyle--options-color-check--default-color}
107+
* @see {@link https://github.com/csandman/chakra-react-select#selectedoptionstyle--options-color--check--default-color}
108108
* @see {@link https://chakra-ui.com/docs/components/menu#menu-option-groups}
109109
*/
110110
selectedOptionStyle?: SelectedOptionStyle;

0 commit comments

Comments
 (0)