Skip to content

Commit

Permalink
feat(combobox): prefer strings as option values
Browse files Browse the repository at this point in the history
  • Loading branch information
geotrev committed Apr 2, 2024
1 parent 360f505 commit f529dc0
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 42 deletions.
190 changes: 190 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions packages/combobox/demo/stories/ComboboxStory.tsx
Expand Up @@ -19,8 +19,7 @@ import {
} from '@zendeskgarden/container-combobox';
import { useGrid } from '@zendeskgarden/container-grid';

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

interface IOptionProps extends LiHTMLAttributes<HTMLLIElement> {
option: IOption;
Expand All @@ -45,7 +44,7 @@ const Option = ({ option, isGrouped, activeValue, selection, getOptionProps }: I
{(Array.isArray(selection)
? selection.find(value => value.value === option.value) !== undefined
: selection && selection.value === option.value) && '✓ '}
{toString(option)}
{getLabel(option)}
</li>
);

Expand All @@ -71,7 +70,7 @@ const Tags = ({ selection, getTagProps }: ITagsProps) => {
option,
'aria-label': option.disabled
? ''
: `Press delete or backspace to remove ${toString(option)}`
: `Press delete or backspace to remove ${getLabel(option)}`
});
const previousDisabledOptions = selection.filter(
(_option, _index) => _option.disabled && _index < index
Expand All @@ -87,7 +86,7 @@ const Tags = ({ selection, getTagProps }: ITagsProps) => {
return (
<td key={index} role={role} className="inline">
<button className="mr-1 px-1" disabled={option.disabled} {...props} type="button">
{toString(option)}
{getLabel(option)}
</button>
</td>
);
Expand Down Expand Up @@ -286,7 +285,7 @@ export const ComboboxStory: Story<IArgs> = ({ as, ...props }) => {

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

setOptions(_options.filter(option => toString(option).match(regex)));
setOptions(_options.filter(option => getLabel(option).match(regex)));
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions packages/combobox/src/ComboboxContainer.spec.tsx
Expand Up @@ -91,7 +91,7 @@ describe('ComboboxContainer', () => {
{Array.isArray(selection) &&
selection.map(option => (
<button
key={option.value as string}
key={option.value}
data-test-id={tagTestId}
disabled={option.disabled}
{...getTagProps({ 'aria-label': 'tag', option })}
Expand All @@ -110,7 +110,7 @@ describe('ComboboxContainer', () => {
{Array.isArray(selection) &&
selection.map(option => (
<button
key={option.value as string}
key={option.value}
data-test-id={tagTestId}
disabled={option.disabled}
{...getTagProps({ 'aria-label': 'tag', option })}
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('ComboboxContainer', () => {
>
{option.options.map((groupOption, groupIndex) => (
<li
key={(groupOption.value as string) || groupIndex}
key={groupOption.value || groupIndex}
data-test-id={`${optionTestIdPrefix}-${index + 1}.${groupIndex + 1}`}
{...getOptionProps({ option: groupOption })}
>
Expand All @@ -151,7 +151,7 @@ describe('ComboboxContainer', () => {
</li>
) : (
<li
key={(option.value as string) || index}
key={option.value || index}
data-test-id={`${optionTestIdPrefix}-${index + 1}`}
{...getOptionProps({ option })}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/combobox/src/ComboboxContainer.tsx
Expand Up @@ -31,7 +31,7 @@ ComboboxContainer.propTypes = {
hasMessage: PropTypes.bool,
options: PropTypes.arrayOf(PropTypes.any).isRequired,
inputValue: PropTypes.string,
selectionValue: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
selectionValue: PropTypes.any,
isExpanded: PropTypes.bool,
defaultExpanded: PropTypes.bool,
initialExpanded: PropTypes.bool,
Expand Down

0 comments on commit f529dc0

Please sign in to comment.