Skip to content

Commit

Permalink
fix: make textValue default to key for Normalized Item (#2113)
Browse files Browse the repository at this point in the history
Resolves #2112
  • Loading branch information
AkshatJawne authored Jun 28, 2024
1 parent 4734865 commit bd3e944
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/spectrum/utils/itemUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('getItemTextValue', () => {
<Item key="">
<span>object</span>
</Item>,
undefined,
'',
],
])(
'should return the expected `textValue`: %s, %s',
Expand Down
3 changes: 2 additions & 1 deletion packages/components/src/spectrum/utils/itemUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ export function getItemKey<
*/
export function getItemTextValue<T>(item: ItemElement<T>): string | undefined {
if (item.props.textValue == null) {
const itemKeyStr = item.key == null ? undefined : String(item.key);
return ['string', 'boolean', 'number'].includes(typeof item.props.children)
? String(item.props.children)
: undefined;
: itemKeyStr;
}

return item.props.textValue === ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe.each([
it.each([
[
{ key: 'mock.key', textValue: undefined },
'Empty',
'mock.key',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(undefined, undefined)',
'wrapPrimitiveWithText(undefined, description)',
Expand All @@ -115,6 +115,16 @@ describe.each([
key: 'mock.key',
item: { content: 'mock.content', textValue: undefined },
},
'mock.key',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(undefined, description)',
],
[
{
key: 'mock.key',
item: { content: 'mock.content', textValue: '' },
},
'Empty',
'wrapIcon(undefined, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
Expand All @@ -132,15 +142,30 @@ describe.each([
],
[
{
key: 'mock.key',
key: '',
item: {
textValue: 'mock.textValue',
textValue: undefined,
icon: 'mock.icon',
content: 'mock.content',
description: 'mock.description',
},
},
'mock.textValue',
'Empty',
'wrapIcon(mock.icon, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(mock.description, description)',
],
[
{
key: undefined,
item: {
textValue: undefined,
icon: 'mock.icon',
content: 'mock.content',
description: 'mock.description',
},
},
undefined,
'wrapIcon(mock.icon, illustration)',
'wrapPrimitiveWithText(mock.content, undefined)',
'wrapPrimitiveWithText(mock.description, description)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { ListActionMenu, ListActionMenuProps } from '../ListActionMenu';
import { Item } from '../shared';
import {
getItemKey,
ItemIconSlot,
ITEM_EMPTY_STRING_TEXT_VALUE,
ItemIconSlot,
NormalizedItem,
TooltipOptions,
} from './itemUtils';
Expand Down Expand Up @@ -50,7 +50,9 @@ export function useRenderNormalizedItem({
(normalizedItem: NormalizedItem) => {
const itemKey = getItemKey(normalizedItem);
const content = wrapPrimitiveWithText(normalizedItem.item?.content);
const textValue = normalizedItem.item?.textValue ?? '';
const textValue =
normalizedItem.item?.textValue ??
(itemKey == null ? undefined : String(itemKey));

const description = showItemDescriptions
? wrapPrimitiveWithText(normalizedItem.item?.description, 'description')
Expand Down

0 comments on commit bd3e944

Please sign in to comment.