Skip to content

Commit 98a2b4c

Browse files
authored
Merge pull request #173 from csandman/read-only
Make the `isReadOnly` prop functional
2 parents 2eea294 + 89cd678 commit 98a2b4c

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Check out these demos:
3535
- [`size`](#size--options-sm--md--lg--default-md)
3636
- [`colorScheme`](#colorscheme)
3737
- [`tagVariant`](#tagvariant--options-subtle--solid--outline--default-subtle)
38-
- [`isInvalid`](#isinvalid--default-false)
38+
- [`isInvalid` / `isReadOnly`](#isinvalid--default-false--isreadonly---default-false)
3939
- [`focusBorderColor` / `errorBorderColor`](#focusbordercolor--default-blue500--errorbordercolor--default-red500)
4040
- [`useBasicStyles`](#usebasicstyles--default-false)
4141
- [`selectedOptionStyle`](#selectedoptionstyle--options-color--check--default-color)
@@ -174,19 +174,23 @@ return (
174174

175175
[![CS-JS]](https://codesandbox.io/s/chakra-react-select-tag-variants-w31gnt?file=/example.js)
176176

177-
#### `isInvalid` — Default: `false`
177+
#### `isInvalid` — Default: `false` | `isReadOnly` - Default: `false`
178178

179-
You can pass `isInvalid` to the select component to style it like the Chakra `<Input />` is styled when it receives the same prop.
179+
You can pass `isInvalid` to the select component to style it like the Chakra `Input` is styled when it receives the same prop. Alternatively you can pass `isReadOnly` to make the component non-interactive in the same way Chakra's `Input` does.
180180

181-
You can pass also pass `isInvalid` or `isDisabled` to a wrapping `<FormControl />` and it will output their corresponding `<Input />` on the select.
181+
You can pass also pass `isInvalid`, `isDisabled`, or `isReadOnly` into a wrapping `<FormControl />` to achieve the same result as passing these props into the `Select` component.
182182

183183
```js
184184
return (
185185
<>
186186
{/* This will show up with a red border */}
187187
<Select isInvalid />
188188

189-
{/* This will show up with a red border, and grayed out */}
189+
{/* This will show up normally but will not be interactive */}
190+
<Select isReadOnly />
191+
192+
{/* This will show up grayed out and will not be interactive */}
193+
{/* Additionally, it will have a red border and the error message will be shown */}
190194
<FormControl isInvalid isDisabled>
191195
<FormLabel>Invalid & Disabled Select</FormLabel>
192196
<Select />

src/chakra-components/control.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const Control = <
3434
selectProps: {
3535
size,
3636
isInvalid,
37+
isReadOnly,
3738
chakraStyles,
3839
focusBorderColor,
3940
errorBorderColor,
@@ -86,6 +87,7 @@ const Control = <
8687
data-focus-visible={isFocused ? true : undefined}
8788
data-invalid={isInvalid ? true : undefined}
8889
data-disabled={isDisabled ? true : undefined}
90+
aria-readonly={isReadOnly ? true : undefined}
8991
>
9092
{children}
9193
</Box>
@@ -179,7 +181,6 @@ export const DropdownIndicator = <
179181
height: "100%",
180182
borderRadius: 0,
181183
borderWidth: 0,
182-
cursor: "pointer",
183184
fontSize: iconSize,
184185
...(useBasicStyles && {
185186
background: "transparent",

src/chakra-components/input.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const Input = <
7373
ref={innerRef}
7474
sx={inputSx}
7575
disabled={isDisabled}
76-
readOnly={isReadOnly}
76+
readOnly={isReadOnly ? true : undefined}
7777
aria-readonly={isReadOnly ? true : undefined}
7878
aria-required={isRequired ? true : undefined}
7979
{...innerProps}

src/module-augmentation.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ declare module "react-select/dist/declarations/src/Select" {
4141
* If the `aria-invalid` prop is not passed, this prop will also set that
4242
*
4343
* @defaultValue `false`
44-
* @see {@link https://github.com/csandman/chakra-react-select#isinvalid--default-false}
44+
* @see {@link https://github.com/csandman/chakra-react-select#isinvalid--default-false--isreadonly---default-false}
4545
* @see {@link https://chakra-ui.com/docs/components/input/props}
46+
* @see {@link https://chakra-ui.com/docs/components/form-control/props}
4647
*/
4748
isInvalid?: boolean;
4849

4950
/**
5051
* If `true`, the form control will be `readonly`
5152
*
53+
* @defaultValue `false`
54+
* @see {@link https://github.com/csandman/chakra-react-select#isinvalid--default-false--isreadonly---default-false}
5255
* @see {@link https://chakra-ui.com/docs/components/input/props}
5356
* @see {@link https://chakra-ui.com/docs/components/form-control/props}
5457
*/

src/use-chakra-select-props.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const useChakraSelectProps = <
2727
chakraStyles = {},
2828
onFocus,
2929
onBlur,
30+
menuIsOpen,
3031
...props
3132
}: Props<Option, IsMulti, Group>): Props<Option, IsMulti, Group> => {
3233
/**
@@ -44,6 +45,9 @@ const useChakraSelectProps = <
4445
onBlur,
4546
});
4647

48+
// Unless `menuIsOpen` is controlled, disable it if the select is readonly
49+
const realMenuIsOpen = menuIsOpen ?? inputProps.readOnly ? false : undefined;
50+
4751
/** Ensure that the size used is one of the options, either `sm`, `md`, or `lg` */
4852
let realSize: Size = size;
4953
const sizeOptions: Size[] = ["sm", "md", "lg"];
@@ -110,6 +114,8 @@ const useChakraSelectProps = <
110114
isDisabled: inputProps.disabled,
111115
isInvalid: !!inputProps["aria-invalid"],
112116
inputId: inputProps.id,
117+
isReadOnly: inputProps.readOnly,
118+
menuIsOpen: realMenuIsOpen,
113119
...props,
114120
// aria-invalid can be passed to react-select, so we allow that to
115121
// override the `isInvalid` prop

0 commit comments

Comments
 (0)