Skip to content

Commit 9019653

Browse files
committed
feat: accept readonly array for multiple select value
1 parent 5d472d4 commit 9019653

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

packages/@react-stately/select/src/useSelectState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface SelectState<T, M extends SelectionMode = 'single'> extends List
4646
readonly defaultValue: ValueType<M>,
4747

4848
/** Sets the select value. */
49-
setValue(value: Key | Key[] | null): void,
49+
setValue(value: Key | readonly Key[] | null): void,
5050

5151
/**
5252
* The value of the first selected item.
@@ -88,7 +88,7 @@ export function useSelectState<T extends object, M extends SelectionMode = 'sing
8888
let value = useMemo(() => {
8989
return props.value !== undefined ? props.value : (selectionMode === 'single' ? props.selectedKey : undefined) as ValueType<M>;
9090
}, [props.value, props.selectedKey, selectionMode]);
91-
let [controlledValue, setControlledValue] = useControlledState<Key | Key[] | null>(value, defaultValue, props.onChange as any);
91+
let [controlledValue, setControlledValue] = useControlledState<Key | readonly Key[] | null>(value, defaultValue, props.onChange as any);
9292
// Only display the first selected item if in single selection mode but the value is an array.
9393
let displayValue = selectionMode === 'single' && Array.isArray(controlledValue) ? controlledValue[0] : controlledValue;
9494
let setValue = (value: Key | Key[] | null) => {

packages/@react-types/select/src/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ import {
3232
} from '@react-types/shared';
3333

3434
export type SelectionMode = 'single' | 'multiple';
35-
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
35+
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];
36+
export type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
3637
type ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];
3738

38-
export interface SelectProps<T, M extends SelectionMode = 'single'> extends CollectionBase<T>, Omit<InputBase, 'isReadOnly'>, ValueBase<ValueType<M>>, Validation<ValidationType<M>>, HelpTextProps, LabelableProps, TextInputBase, FocusableProps {
39+
export interface SelectProps<T, M extends SelectionMode = 'single'> extends CollectionBase<T>, Omit<InputBase, 'isReadOnly'>, ValueBase<ValueType<M>, ChangeValueType<M>>, Validation<ValidationType<M>>, HelpTextProps, LabelableProps, TextInputBase, FocusableProps {
3940
/**
4041
* Whether single or multiple selection is enabled.
4142
* @default 'single'

0 commit comments

Comments
 (0)