Skip to content

Commit

Permalink
Made key and textValue optional (deephaven#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed Mar 5, 2024
1 parent aee4047 commit c68a12f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/components/src/spectrum/picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export function Picker({
// elements that back the Spectrum Picker. These are not visible in the UI,
// but are used for accessibility purposes, so we set to an arbitrary
// 'Empty' value so that they are not empty strings.
<Item key={key as Key} textValue={textValue === '' ? 'Empty' : textValue}>
<Item
key={key as Key}
textValue={textValue === '' || textValue == null ? 'Empty' : textValue}
>
<PickerItemContent>{content}</PickerItemContent>
{tooltipOptions == null || content === '' ? null : (
<Tooltip options={tooltipOptions}>
Expand Down
3 changes: 0 additions & 3 deletions packages/components/src/spectrum/picker/PickerUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ const expectedItems = {
</Item>,
{
content: <span>No textValue</span>,
key: '',
textValue: '',
},
],
explicitKey: [
Expand Down Expand Up @@ -112,7 +110,6 @@ const expectedSections = {
noTitle: [
<Section>{expectedItems.singleStringChild[0]}</Section>,
{
key: '',
items: [expectedItems.singleStringChild[1]],
},
],
Expand Down
20 changes: 10 additions & 10 deletions packages/components/src/spectrum/picker/PickerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export type PickerSelectionChangeHandler = (key: PickerItemKey) => void;
* in separate util methods.
*/
export interface NormalizedPickerItem {
key: PickerItemKey;
key?: PickerItemKey;
content: ReactNode;
textValue: string;
textValue?: string;
}

export interface NormalizedPickerSection {
key: Key;
key?: Key;
title?: ReactNode;
items: NormalizedPickerItem[];
}
Expand Down Expand Up @@ -120,11 +120,11 @@ export function isNormalizedPickerSection(
* @param itemOrSection The picker item or section
* @returns A `PickerItemKey` for the picker item
*/
function normalizeItemKey(item: PickerItem): PickerItemKey;
function normalizeItemKey(section: PickerSection): Key;
function normalizeItemKey(item: PickerItem): PickerItemKey | undefined;
function normalizeItemKey(section: PickerSection): Key | undefined;
function normalizeItemKey(
itemOrSection: PickerItem | PickerSection
): Key | PickerItemKey {
): Key | PickerItemKey | undefined {
// string, number, or boolean
if (typeof itemOrSection !== 'object') {
return itemOrSection;
Expand All @@ -139,15 +139,15 @@ function normalizeItemKey(
if (isSectionElement(itemOrSection)) {
return typeof itemOrSection.props.title === 'string'
? itemOrSection.props.title
: '';
: undefined;
}

// Item element
return (
itemOrSection.props.textValue ??
(typeof itemOrSection.props.children === 'string'
? itemOrSection.props.children
: '')
: undefined)
);
}

Expand All @@ -156,7 +156,7 @@ function normalizeItemKey(
* @param item The picker item
* @returns A string `textValue` for the picker item
*/
function normalizeTextValue(item: PickerItem): string {
function normalizeTextValue(item: PickerItem): string | undefined {
if (typeof item !== 'object') {
return String(item);
}
Expand All @@ -169,7 +169,7 @@ function normalizeTextValue(item: PickerItem): string {
return item.props.children;
}

return '';
return undefined;
}

/**
Expand Down

0 comments on commit c68a12f

Please sign in to comment.