Skip to content

Commit 87173ad

Browse files
lixiaoyanclaude
andauthored
feat: accept readonly array for multiple combobox value (adobe#9741)
* feat: accept readonly array for multiple combobox value Port the readonly array changes from adobe#9221 (Select) to ComboBox. This allows passing `readonly Key[]` as the `value` prop for multiple selection mode without TypeScript errors. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: sort imports alphabetically to fix lint CI failure Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0f17e27 commit 87173ad

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

packages/@react-stately/combobox/src/useComboBoxState.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {ChangeValueType, ComboBoxProps, MenuTriggerAction, SelectionMode, ValueType} from '@react-types/combobox';
1314
import {Collection, CollectionStateBase, FocusStrategy, Key, Node, Selection} from '@react-types/shared';
14-
import {ComboBoxProps, MenuTriggerAction, SelectionMode, ValueType} from '@react-types/combobox';
1515
import {FormValidationState, useFormValidationState} from '@react-stately/form';
1616
import {getChildNodes} from '@react-stately/collections';
1717
import {ListCollection, ListState, useListState} from '@react-stately/list';
@@ -373,7 +373,7 @@ export function useComboBoxState<T extends object, M extends SelectionMode = 'si
373373
// If multiple things are controlled, call onSelectionChange
374374
if (value !== undefined && props.inputValue !== undefined) {
375375
props.onSelectionChange?.(selectedKey);
376-
props.onChange?.(displayValue);
376+
props.onChange?.(displayValue as ChangeValueType<M>);
377377

378378
// Stop menu from reopening from useEffect
379379
let itemText = selectedKey != null ? collection.getItem(selectedKey)?.textValue ?? '' : '';
@@ -504,7 +504,7 @@ function getDefaultInputValue(defaultInputValue: string | null | undefined, sele
504504
return defaultInputValue;
505505
}
506506

507-
function convertValue(value: Key | Key[] | null | undefined) {
507+
function convertValue(value: Key | readonly Key[] | null | undefined) {
508508
if (value === undefined) {
509509
return undefined;
510510
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import {
3535

3636
export type MenuTriggerAction = 'focus' | 'input' | 'manual';
3737
export type SelectionMode = 'single' | 'multiple';
38-
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
38+
export type ValueType<M extends SelectionMode> = M extends 'single' ? Key | null : readonly Key[];
39+
export type ChangeValueType<M extends SelectionMode> = M extends 'single' ? Key | null : Key[];
3940
type ValidationType<M extends SelectionMode> = M extends 'single' ? Key : Key[];
4041

4142
export interface ComboBoxValidationValue<M extends SelectionMode = 'single'> {
@@ -50,7 +51,7 @@ export interface ComboBoxValidationValue<M extends SelectionMode = 'single'> {
5051
inputValue: string
5152
}
5253

53-
export interface ComboBoxProps<T, M extends SelectionMode = 'single'> extends CollectionBase<T>, InputBase, ValueBase<ValueType<M>>, TextInputBase, Validation<ComboBoxValidationValue>, FocusableProps<HTMLInputElement>, LabelableProps, HelpTextProps {
54+
export interface ComboBoxProps<T, M extends SelectionMode = 'single'> extends CollectionBase<T>, InputBase, ValueBase<ValueType<M>, ChangeValueType<M>>, TextInputBase, Validation<ComboBoxValidationValue>, FocusableProps<HTMLInputElement>, LabelableProps, HelpTextProps {
5455
/** The list of ComboBox items (uncontrolled). */
5556
defaultItems?: Iterable<T>,
5657
/** The list of ComboBox items (controlled). */

0 commit comments

Comments
 (0)