Skip to content

Commit bd3e944

Browse files
authored
fix: make textValue default to key for Normalized Item (#2113)
Resolves #2112
1 parent 4734865 commit bd3e944

4 files changed

Lines changed: 36 additions & 8 deletions

File tree

packages/components/src/spectrum/utils/itemUtils.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('getItemTextValue', () => {
6161
<Item key="">
6262
<span>object</span>
6363
</Item>,
64-
undefined,
64+
'',
6565
],
6666
])(
6767
'should return the expected `textValue`: %s, %s',

packages/components/src/spectrum/utils/itemUtils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ export function getItemKey<
137137
*/
138138
export function getItemTextValue<T>(item: ItemElement<T>): string | undefined {
139139
if (item.props.textValue == null) {
140+
const itemKeyStr = item.key == null ? undefined : String(item.key);
140141
return ['string', 'boolean', 'number'].includes(typeof item.props.children)
141142
? String(item.props.children)
142-
: undefined;
143+
: itemKeyStr;
143144
}
144145

145146
return item.props.textValue === ''

packages/components/src/spectrum/utils/useRenderNormalizedItem.test.tsx

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe.each([
105105
it.each([
106106
[
107107
{ key: 'mock.key', textValue: undefined },
108-
'Empty',
108+
'mock.key',
109109
'wrapIcon(undefined, illustration)',
110110
'wrapPrimitiveWithText(undefined, undefined)',
111111
'wrapPrimitiveWithText(undefined, description)',
@@ -115,6 +115,16 @@ describe.each([
115115
key: 'mock.key',
116116
item: { content: 'mock.content', textValue: undefined },
117117
},
118+
'mock.key',
119+
'wrapIcon(undefined, illustration)',
120+
'wrapPrimitiveWithText(mock.content, undefined)',
121+
'wrapPrimitiveWithText(undefined, description)',
122+
],
123+
[
124+
{
125+
key: 'mock.key',
126+
item: { content: 'mock.content', textValue: '' },
127+
},
118128
'Empty',
119129
'wrapIcon(undefined, illustration)',
120130
'wrapPrimitiveWithText(mock.content, undefined)',
@@ -132,15 +142,30 @@ describe.each([
132142
],
133143
[
134144
{
135-
key: 'mock.key',
145+
key: '',
136146
item: {
137-
textValue: 'mock.textValue',
147+
textValue: undefined,
138148
icon: 'mock.icon',
139149
content: 'mock.content',
140150
description: 'mock.description',
141151
},
142152
},
143-
'mock.textValue',
153+
'Empty',
154+
'wrapIcon(mock.icon, illustration)',
155+
'wrapPrimitiveWithText(mock.content, undefined)',
156+
'wrapPrimitiveWithText(mock.description, description)',
157+
],
158+
[
159+
{
160+
key: undefined,
161+
item: {
162+
textValue: undefined,
163+
icon: 'mock.icon',
164+
content: 'mock.content',
165+
description: 'mock.description',
166+
},
167+
},
168+
undefined,
144169
'wrapIcon(mock.icon, illustration)',
145170
'wrapPrimitiveWithText(mock.content, undefined)',
146171
'wrapPrimitiveWithText(mock.description, description)',

packages/components/src/spectrum/utils/useRenderNormalizedItem.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { ListActionMenu, ListActionMenuProps } from '../ListActionMenu';
88
import { Item } from '../shared';
99
import {
1010
getItemKey,
11-
ItemIconSlot,
1211
ITEM_EMPTY_STRING_TEXT_VALUE,
12+
ItemIconSlot,
1313
NormalizedItem,
1414
TooltipOptions,
1515
} from './itemUtils';
@@ -50,7 +50,9 @@ export function useRenderNormalizedItem({
5050
(normalizedItem: NormalizedItem) => {
5151
const itemKey = getItemKey(normalizedItem);
5252
const content = wrapPrimitiveWithText(normalizedItem.item?.content);
53-
const textValue = normalizedItem.item?.textValue ?? '';
53+
const textValue =
54+
normalizedItem.item?.textValue ??
55+
(itemKey == null ? undefined : String(itemKey));
5456

5557
const description = showItemDescriptions
5658
? wrapPrimitiveWithText(normalizedItem.item?.description, 'description')

0 commit comments

Comments
 (0)