Skip to content

Commit 2de1b9b

Browse files
authored
feat(combobox)!: prefer strings as option values (#637)
BREAKING CHANGE: - Removes `object` from `string | object` option value type on `useCombobox`.
1 parent 360f505 commit 2de1b9b

File tree

7 files changed

+207
-20
lines changed

7 files changed

+207
-20
lines changed

package-lock.json

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

packages/combobox/demo/stories/ComboboxStory.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import {
1919
} from '@zendeskgarden/container-combobox';
2020
import { useGrid } from '@zendeskgarden/container-grid';
2121

22-
const toString = (option: IOption) =>
23-
option.label || (typeof option.value === 'string' ? option.value : JSON.stringify(option.value));
22+
const getLabel = (option: IOption) => option.label || option.value;
2423

2524
interface IOptionProps extends LiHTMLAttributes<HTMLLIElement> {
2625
option: IOption;
@@ -45,7 +44,7 @@ const Option = ({ option, isGrouped, activeValue, selection, getOptionProps }: I
4544
{(Array.isArray(selection)
4645
? selection.find(value => value.value === option.value) !== undefined
4746
: selection && selection.value === option.value) && '✓ '}
48-
{toString(option)}
47+
{getLabel(option)}
4948
</li>
5049
);
5150

@@ -71,7 +70,7 @@ const Tags = ({ selection, getTagProps }: ITagsProps) => {
7170
option,
7271
'aria-label': option.disabled
7372
? ''
74-
: `Press delete or backspace to remove ${toString(option)}`
73+
: `Press delete or backspace to remove ${getLabel(option)}`
7574
});
7675
const previousDisabledOptions = selection.filter(
7776
(_option, _index) => _option.disabled && _index < index
@@ -87,7 +86,7 @@ const Tags = ({ selection, getTagProps }: ITagsProps) => {
8786
return (
8887
<td key={index} role={role} className="inline">
8988
<button className="mr-1 px-1" disabled={option.disabled} {...props} type="button">
90-
{toString(option)}
89+
{getLabel(option)}
9190
</button>
9291
</td>
9392
);
@@ -286,7 +285,7 @@ export const ComboboxStory: Story<IArgs> = ({ as, ...props }) => {
286285

287286
const regex = new RegExp(value.replace(/[.*+?^${}()|[\]\\]/giu, '\\$&'), 'gui');
288287

289-
setOptions(_options.filter(option => toString(option).match(regex)));
288+
setOptions(_options.filter(option => getLabel(option).match(regex)));
290289
}
291290
}
292291
};

packages/combobox/src/ComboboxContainer.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ describe('ComboboxContainer', () => {
9191
{Array.isArray(selection) &&
9292
selection.map(option => (
9393
<button
94-
key={option.value as string}
94+
key={option.value}
9595
data-test-id={tagTestId}
9696
disabled={option.disabled}
9797
{...getTagProps({ 'aria-label': 'tag', option })}
@@ -110,7 +110,7 @@ describe('ComboboxContainer', () => {
110110
{Array.isArray(selection) &&
111111
selection.map(option => (
112112
<button
113-
key={option.value as string}
113+
key={option.value}
114114
data-test-id={tagTestId}
115115
disabled={option.disabled}
116116
{...getTagProps({ 'aria-label': 'tag', option })}
@@ -140,7 +140,7 @@ describe('ComboboxContainer', () => {
140140
>
141141
{option.options.map((groupOption, groupIndex) => (
142142
<li
143-
key={(groupOption.value as string) || groupIndex}
143+
key={groupOption.value || groupIndex}
144144
data-test-id={`${optionTestIdPrefix}-${index + 1}.${groupIndex + 1}`}
145145
{...getOptionProps({ option: groupOption })}
146146
>
@@ -151,7 +151,7 @@ describe('ComboboxContainer', () => {
151151
</li>
152152
) : (
153153
<li
154-
key={(option.value as string) || index}
154+
key={option.value || index}
155155
data-test-id={`${optionTestIdPrefix}-${index + 1}`}
156156
{...getOptionProps({ option })}
157157
>

packages/combobox/src/ComboboxContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ComboboxContainer.propTypes = {
3131
hasMessage: PropTypes.bool,
3232
options: PropTypes.arrayOf(PropTypes.any).isRequired,
3333
inputValue: PropTypes.string,
34-
selectionValue: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
34+
selectionValue: PropTypes.any,
3535
isExpanded: PropTypes.bool,
3636
defaultExpanded: PropTypes.bool,
3737
initialExpanded: PropTypes.bool,

packages/combobox/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { IUseFieldReturnValue } from '@zendeskgarden/container-field';
99
import { HTMLProps, ReactNode, RefObject } from 'react';
1010

11-
export type OptionValue = string | object;
11+
export type OptionValue = string;
1212

1313
interface ISelectedOption {
1414
value: OptionValue;

0 commit comments

Comments
 (0)