Skip to content

Commit 1ac345b

Browse files
fix: patternhub warnings (#4291)
* fix(): add key to fix warning that each child in a list should have a unique "key" prop * fix(): add htmlFor to fix warning of invalid DOM property for * fix(): moved function for option keys to utils and use it in select and custom-select * fix(): use props.for instead of props.htmlFor and props.checked instead of props.defaultChecked * fix(): remove getCustomKey from component models * fix(): use key in optgroup * fix(): use key in optgroup options
1 parent 85daa75 commit 1ac345b

File tree

5 files changed

+49
-19
lines changed

5 files changed

+49
-19
lines changed

packages/components/src/components/custom-select/custom-select.lite.tsx

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
getBoolean,
2222
getBooleanAsString,
2323
getHideProp,
24+
getOptionKey,
2425
getSearchInput,
2526
handleDataOutside,
2627
hasVoiceOver,
@@ -201,9 +202,6 @@ export default function DBCustomSelect(props: DBCustomSelectProps) {
201202

202203
return false;
203204
},
204-
getOptionKey: (option: CustomSelectOptionType) => {
205-
return (option.id ?? option.value ?? uuid()).toString();
206-
},
207205
getTagRemoveLabel: (index: number) => {
208206
if (
209207
props.removeTagsTexts &&
@@ -776,9 +774,10 @@ export default function DBCustomSelect(props: DBCustomSelectProps) {
776774
key={useTarget({
777775
vue: undefined,
778776
stencil: undefined,
779-
default:
780-
'native-select-option-' +
781-
state.getOptionKey(option)
777+
default: getOptionKey(
778+
option,
779+
'native-select-option-'
780+
)
782781
})}
783782
disabled={option.disabled}
784783
value={option.value}>
@@ -823,9 +822,10 @@ export default function DBCustomSelect(props: DBCustomSelectProps) {
823822
key={useTarget({
824823
vue: undefined,
825824
stencil: undefined,
826-
default:
827-
'tag-' +
828-
state.getOptionKey(option)
825+
default: getOptionKey(
826+
option,
827+
'tag-'
828+
)
829829
})}
830830
removeButton={state.getTagRemoveLabel(
831831
index
@@ -920,11 +920,10 @@ export default function DBCustomSelect(props: DBCustomSelectProps) {
920920
key={useTarget({
921921
vue: undefined,
922922
stencil: undefined,
923-
default:
924-
'custom-select-list-item-' +
925-
state.getOptionKey(
926-
option
927-
)
923+
default: getOptionKey(
924+
option,
925+
'custom-select-list-item-'
926+
)
928927
})}
929928
type={
930929
props.multiple

packages/components/src/components/custom-select/model.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ export type DBCustomSelectDefaultState = {
260260
getNativeSelectValue: () => string;
261261
getOptionLabel: (option: CustomSelectOptionType) => string;
262262
getOptionChecked: (value?: string) => boolean;
263-
getOptionKey: (option: CustomSelectOptionType) => string;
264263
getTagRemoveLabel: (index: number) => string;
265264
selectAllEnabled: boolean;
266265
searchEnabled: boolean;

packages/components/src/components/select/select.lite.tsx

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
delay,
1616
getBoolean,
1717
getHideProp,
18+
getOptionKey,
1819
hasVoiceOver,
1920
stringPropVisible,
2021
uuid
@@ -228,28 +229,51 @@ export default function DBSelect(props: DBSelectProps) {
228229
aria-describedby={props.ariaDescribedBy ?? state._descByIds}>
229230
{/* Empty option for floating label */}
230231
<option hidden></option>
231-
<Show when={props.options} else={props.children}>
232+
<Show when={props.options?.length} else={props.children}>
232233
<For each={props.options}>
233234
{(option: DBSelectOptionType) => (
234235
<>
235236
<Show
236237
when={option.options}
237238
else={
238239
<option
240+
key={useTarget({
241+
vue: undefined,
242+
stencil: undefined,
243+
default: getOptionKey(
244+
option,
245+
'select-option-'
246+
)
247+
})}
239248
value={option.value}
240249
disabled={option.disabled}
241250
selected={option.selected}>
242251
{state.getOptionLabel(option)}
243252
</option>
244253
}>
245254
<optgroup
246-
label={state.getOptionLabel(option)}>
255+
label={state.getOptionLabel(option)}
256+
key={useTarget({
257+
vue: undefined,
258+
stencil: undefined,
259+
default: getOptionKey(
260+
option,
261+
'select-optgroup-'
262+
)
263+
})}>
247264
<For each={option.options}>
248265
{(
249266
optgroupOption: DBSelectOptionType
250267
) => (
251268
<option
252-
key={optgroupOption.value.toString()}
269+
key={useTarget({
270+
vue: undefined,
271+
stencil: undefined,
272+
default: getOptionKey(
273+
optgroupOption,
274+
'select-optgroup-option-'
275+
)
276+
})}
253277
value={optgroupOption.value}
254278
selected={
255279
optgroupOption.selected

packages/components/src/utils/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,11 @@ export const stringPropVisible = (
224224

225225
export const getSearchInput = (element: HTMLElement): HTMLInputElement | null =>
226226
element.querySelector<HTMLInputElement>(`input[type="search"]`);
227+
228+
export const getOptionKey = (
229+
option: { id?: string; value?: string | number | string[] | undefined },
230+
prefix: string
231+
) => {
232+
const key = option.id ?? option.value ?? uuid();
233+
return `${prefix}${key}`;
234+
};

showcases/shared/tag.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
"name": "True",
431431
"props": {
432432
"icon": "x_placeholder",
433-
"content": true
433+
"content": "true"
434434
}
435435
}
436436
]

0 commit comments

Comments
 (0)