Skip to content

Commit 42f340e

Browse files
authored
Merge pull request #6211 from pat270/LPD-71044
fix(@clayui/drop-down): LPD-71044 Items should use `title` instead of deprecated `label`
2 parents 14eeda6 + 0b3d178 commit 42f340e

File tree

4 files changed

+153
-82
lines changed

4 files changed

+153
-82
lines changed

packages/clay-drop-down/docs/drop-down.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,21 +388,21 @@ To render a cascading menu it is necessary to set the `type` of the item to `con
388388

389389
```js
390390
const items = [
391-
{label: 'Folder'},
391+
{title: 'Folder'},
392392
{type: 'divider'},
393393
{
394394
items: [
395-
{label: 'Basic Document'},
396-
{label: 'Contract'},
397-
{label: 'Marketing Banner'},
398-
{label: 'Spreadsheet'},
399-
{label: 'Presentation'},
395+
{title: 'Basic Document'},
396+
{title: 'Contract'},
397+
{title: 'Marketing Banner'},
398+
{title: 'Spreadsheet'},
399+
{title: 'Presentation'},
400400
],
401-
label: 'Document',
401+
title: 'Document',
402402
type: 'contextual',
403403
},
404-
{label: 'Shortcut'},
405-
{label: 'Repository'},
404+
{title: 'Shortcut'},
405+
{title: 'Repository'},
406406
];
407407
```
408408

packages/clay-drop-down/src/Items.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ interface IInternalItem {
5252
const Checkbox = ({
5353
checked = false,
5454
onChange = () => {},
55+
title,
5556
...otherProps
5657
}: Item & IInternalItem) => {
5758
const [value, setValue] = useState<boolean>(checked);
@@ -63,6 +64,7 @@ const Checkbox = ({
6364
<ClayCheckbox
6465
{...otherProps}
6566
checked={value}
67+
label={title}
6668
onChange={() => {
6769
setValue((val) => !val);
6870
onChange(!value);
@@ -76,7 +78,7 @@ const Checkbox = ({
7678
type Context = {
7779
back?: () => void;
7880
close: () => void;
79-
onForward?: (label: string, id: string) => void;
81+
onForward?: (title: string, id: string) => void;
8082
messages?: Record<string, string>;
8183
};
8284

@@ -89,17 +91,20 @@ const Item = ({
8991
label,
9092
onClick,
9193
spritemap,
94+
title,
9295
...props
9396
}: Omit<Item, 'onChange'> & IInternalItem) => {
9497
const {back, close, messages, onForward} = useContext(ClayDropDownContext);
9598

99+
title = label || title;
100+
96101
return (
97102
<DropDown.Item
98103
{...props}
99104
onClick={(event) => {
100-
if (onForward && child && label) {
105+
if (onForward && child && title) {
101106
event.preventDefault();
102-
onForward(label, child);
107+
onForward(title, child);
103108

104109
return;
105110
}
@@ -117,13 +122,13 @@ const Item = ({
117122
}}
118123
spritemap={spritemap}
119124
>
120-
{label}
125+
{title}
121126

122127
{child && (
123128
<span
124-
aria-label={`${messages!['goTo']} ${label}`}
129+
aria-label={`${messages!['goTo']} ${title}`}
125130
className="dropdown-item-indicator-end"
126-
title={`${messages!['goTo']} ${label}`}
131+
title={`${messages!['goTo']} ${title}`}
127132
>
128133
<Icon spritemap={spritemap} symbol="angle-right" />
129134
</span>
@@ -132,18 +137,20 @@ const Item = ({
132137
);
133138
};
134139

135-
const Group = ({items, label, spritemap}: Item & IInternalItem) => {
140+
const Group = ({items, label, spritemap, title}: Item & IInternalItem) => {
141+
title = label || title;
142+
136143
warning(
137144
typeof items !== 'undefined',
138-
`ClayDropDownWithItems -> The '${label}' group contains no items to render.`
145+
`ClayDropDownWithItems -> The '${title}' group contains no items to render.`
139146
);
140147

141148
if (typeof items === 'undefined') {
142149
return null;
143150
}
144151

145152
return (
146-
<DropDownGroup header={label}>
153+
<DropDownGroup header={title}>
147154
{items && <Items items={items} spritemap={spritemap} />}
148155
</DropDownGroup>
149156
);
@@ -180,6 +187,7 @@ const Contextual = ({
180187
items,
181188
label,
182189
spritemap,
190+
title,
183191
...otherProps
184192
}: Omit<Item, 'onChange'> & IInternalItem) => {
185193
const [visible, setVisible] = useState(false);
@@ -215,6 +223,8 @@ const Contextual = ({
215223
[]
216224
);
217225

226+
title = label || title;
227+
218228
return (
219229
<DropDown.Item
220230
{...otherProps}
@@ -263,7 +273,7 @@ const Contextual = ({
263273
spritemap={spritemap}
264274
symbolRight="angle-right"
265275
>
266-
{label}
276+
{title}
267277

268278
{items && (
269279
<DropDown.Menu
@@ -331,7 +341,7 @@ interface IRadioContext {
331341

332342
const RadioGroupContext = React.createContext({} as IRadioContext);
333343

334-
const Radio = ({value = '', ...otherProps}: Item & IInternalItem) => {
344+
const Radio = ({title, value = '', ...otherProps}: Item & IInternalItem) => {
335345
const {checked, name, onChange} = useContext(RadioGroupContext);
336346

337347
const {tabFocus} = useContext(DropDownContext);
@@ -342,6 +352,7 @@ const Radio = ({value = '', ...otherProps}: Item & IInternalItem) => {
342352
{...otherProps}
343353
checked={checked === value}
344354
inline
355+
label={title}
345356
name={name}
346357
onChange={() => onChange(value as string)}
347358
tabIndex={!tabFocus ? -1 : undefined}
@@ -357,6 +368,7 @@ const RadioGroup = ({
357368
name,
358369
onChange = () => {},
359370
spritemap,
371+
title,
360372
value: defaultValue = '',
361373
}: Item & IInternalItem) => {
362374
const [value, setValue] = useState(defaultValue);
@@ -370,13 +382,15 @@ const RadioGroup = ({
370382
},
371383
};
372384

385+
title = label || title;
386+
373387
warning(
374388
items && items.filter((item) => item.type !== 'radio').length === 0,
375389
'ClayDropDownWithItems -> Items of type `radiogroup` should be used `radio` if you need to use others, it is recommended to use type `group`.'
376390
);
377391

378392
return (
379-
<DropDownGroup header={label} role="radiogroup">
393+
<DropDownGroup header={title} role="radiogroup">
380394
{items && (
381395
<RadioGroupContext.Provider value={params}>
382396
<Items items={items} spritemap={spritemap} />

packages/clay-drop-down/src/__tests__/DropDownWithItems.tsx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import {ClayDropDownWithItems} from '..';
77
import ClayButton from '@clayui/button';
8-
import {cleanup, render} from '@testing-library/react';
8+
import {cleanup, fireEvent, render, screen} from '@testing-library/react';
99
import React from 'react';
1010

1111
const spritemap = 'icons.svg';
@@ -178,4 +178,59 @@ describe('ClayDropDownWithItems', () => {
178178

179179
expect(document.body).toMatchSnapshot();
180180
});
181+
182+
it('renders a DropDownWithItems using title', () => {
183+
render(
184+
<ClayDropDownWithItems
185+
items={[
186+
{
187+
href: '#',
188+
title: 'linkable',
189+
},
190+
{
191+
items: [
192+
{
193+
checked: true,
194+
title: 'checkbox',
195+
type: 'checkbox' as const,
196+
},
197+
{
198+
checked: false,
199+
title: 'checkbox 1',
200+
type: 'checkbox' as const,
201+
},
202+
],
203+
title: 'checkbox',
204+
type: 'group' as const,
205+
},
206+
{
207+
items: [
208+
{
209+
title: 'one',
210+
type: 'radio' as const,
211+
value: 'one',
212+
},
213+
{
214+
title: 'two',
215+
type: 'radio' as const,
216+
value: 'two',
217+
},
218+
],
219+
name: 'radio',
220+
title: 'radio',
221+
type: 'radiogroup' as const,
222+
},
223+
]}
224+
renderMenuOnClick
225+
spritemap={spritemap}
226+
trigger={<ClayButton>Click Me</ClayButton>}
227+
/>
228+
);
229+
230+
const toggleButton = document.querySelector('.dropdown-toggle');
231+
232+
fireEvent.click(toggleButton as HTMLButtonElement, {});
233+
234+
expect(screen.getByText('linkable')).toBeDefined();
235+
});
181236
});

0 commit comments

Comments
 (0)