Skip to content

Commit 7fc3077

Browse files
authored
[LG-2163] chore: Select component removed readOnly prop (#3241)
1 parent 82c0efe commit 7fc3077

File tree

8 files changed

+23
-33
lines changed

8 files changed

+23
-33
lines changed

.changeset/moody-cougars-sell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@leafygreen-ui/select': minor
3+
---
4+
5+
Removed readOnly prop

packages/select/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ function ControlledSelect() {
9797
| `value` | `string` | Sets the `<Option />` that will appear selected and makes the component a controlled component. | `''` |
9898
| `defaultValue` | `string` | Sets the `<Option />` that will appear selected on page load when the component is uncontrolled. | `''` |
9999
| `onChange` | `function` | A function that gets called when the selected value changes. Receives the value string as the first argument. | `() => {}` |
100-
| `readOnly` | `boolean` | Disables the console warning when the component is controlled and no `onChange` prop is provided. | `false` |
101100
| `allowDeselect` | `boolean` | Enables or disables the option for a user to select a null default value. | `true` |
102101
| `open` | `boolean` | Controls whether the dropdown menu is open. When provided, the component becomes a controlled component for the open state. | |
103102
| `setOpen` | `function` | Callback function that is called when the open state should change. Required when `open` prop is provided. Receives a boolean value as the first argument. | |

packages/select/src/MenuButton/MenuButton.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const MenuButton = React.forwardRef<HTMLButtonElement, MenuButtonProps>(
3636
text,
3737
name,
3838
deselected,
39-
readOnly,
4039
onClose,
4140
onOpen,
4241
state,

packages/select/src/MenuButton/MenuButton.types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface MenuButtonBaseProps extends React.ComponentProps<'button'> {
99
text: React.ReactNode;
1010
name?: string;
1111
deselected: boolean;
12-
readOnly?: boolean;
1312
onClose: () => void;
1413
onOpen: () => void;
1514
state?: State;

packages/select/src/Select.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ const meta: StoryMetaType<typeof Select> = {
119119
label: { control: 'text' },
120120
description: { control: 'text' },
121121
defaultValue: { control: 'text' },
122-
readOnly: { control: 'boolean' },
123122
errorMessage: { control: 'text' },
124123
allowDeselect: { control: 'boolean' },
125124
open: {

packages/select/src/Select/Select.spec.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,21 +313,19 @@ describe('packages/select', () => {
313313
Context.within(Jest.spyContext(console, 'warn'), spy => {
314314
spy.mockImplementation();
315315

316-
// @ts-ignore React18-expect-error - expecting `readOnly` prop
317316
render(<Select {...defaultProps} value="" />);
318317

319318
expect(spy).toHaveBeenCalledTimes(1);
320319
expect(spy).toHaveBeenCalledWith(
321320
'You provided a `value` prop to a form field without an `onChange` handler. ' +
322321
'This will render a read-only field. ' +
323322
'If the field should be mutable use `defaultValue`. ' +
324-
'Otherwise, set either `onChange` or `readOnly`.',
323+
'Otherwise, set `onChange`.',
325324
);
326325

327326
spy.mockClear();
328327

329328
render(<Select {...defaultProps} defaultValue="" />);
330-
render(<Select {...defaultProps} value="" readOnly />);
331329
render(<Select {...defaultProps} value="" onChange={() => {}} />);
332330

333331
expect(spy).not.toHaveBeenCalled();

packages/select/src/Select/Select.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ export const Select = forwardRef<HTMLDivElement, SelectProps>(
8585
defaultValue,
8686
value,
8787
onChange,
88-
readOnly,
8988
portalContainer,
9089
portalRef,
9190
scrollContainer,
@@ -142,15 +141,15 @@ export const Select = forwardRef<HTMLDivElement, SelectProps>(
142141
}, [size, open, disabled, lgIds]);
143142

144143
useEffect(() => {
145-
if (value !== undefined && onChange === undefined && readOnly !== true) {
144+
if (value !== undefined && onChange === undefined) {
146145
console.warn(
147146
'You provided a `value` prop to a form field without an `onChange` handler. ' +
148147
'This will render a read-only field. ' +
149148
'If the field should be mutable use `defaultValue`. ' +
150-
'Otherwise, set either `onChange` or `readOnly`.',
149+
'Otherwise, set `onChange`.',
151150
);
152151
}
153-
}, [onChange, readOnly, value]);
152+
}, [onChange, value]);
154153

155154
useEffect(() => {
156155
if (openProp !== undefined && setOpenProp === undefined) {
@@ -623,7 +622,6 @@ export const Select = forwardRef<HTMLDivElement, SelectProps>(
623622
id={menuButtonId}
624623
ref={menuButtonRef}
625624
name={name}
626-
readOnly={readOnly}
627625
value={getOptionValue(selectedOption)}
628626
text={
629627
selectedOption !== null

packages/select/src/Select/Select.types.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export type SelectProps = BaseSelectProps &
139139
Either<LabelProp, 'label' | 'aria-labelledby' | 'aria-label'> &
140140
(
141141
| // Uncontrolled
142-
({
142+
{
143143
/**
144144
* `value` makes the component a controlled component and using `defaultValue` makes it uncontrolled.
145145
*/
@@ -148,7 +148,6 @@ export type SelectProps = BaseSelectProps &
148148
* `value` makes the component a controlled component and using `defaultValue` makes it uncontrolled.
149149
*/
150150
value?: undefined;
151-
} & {
152151
/**
153152
* A function that takes in the value of the selected option, and the event that was used to select the value (i.e. React.MouseEvent | KeyboardEvent | React.KeyboardEvent).
154153
*
@@ -158,25 +157,19 @@ export type SelectProps = BaseSelectProps &
158157
value: string,
159158
event: React.MouseEvent | KeyboardEvent | React.KeyboardEvent,
160159
) => void;
160+
}
161+
// Controlled
162+
| {
163+
value: string;
164+
defaultValue?: undefined;
161165
/**
162-
* Indicates that the component's value cannot be changed.
166+
* A function that takes in the value of the selected option, and the event that was used to select the value (i.e. React.MouseEvent | KeyboardEvent | React.KeyboardEvent).
167+
*
168+
* Note: This API is different from the native HTML `<select>` element's `onChange` prop given the current technical design of this component.
163169
*/
164-
readOnly?: false;
165-
})
166-
// Controlled
167-
| ({ value: string; defaultValue?: undefined } & (
168-
| {
169-
/**
170-
* A function that takes in the value of the selected option, and the event that was used to select the value (i.e. React.MouseEvent | KeyboardEvent | React.KeyboardEvent).
171-
*
172-
* Note: This API is different from the native HTML `<select>` element's `onChange` prop given the current technical design of this component.
173-
*/
174-
onChange: (
175-
value: string,
176-
event: React.MouseEvent | KeyboardEvent | React.KeyboardEvent,
177-
) => void;
178-
readOnly?: false;
179-
}
180-
| { readOnly: true; onChange?: undefined }
181-
))
170+
onChange?: (
171+
value: string,
172+
event: React.MouseEvent | KeyboardEvent | React.KeyboardEvent,
173+
) => void;
174+
}
182175
);

0 commit comments

Comments
 (0)