Skip to content

Commit 51c71db

Browse files
committed
fixed exports, option key and placeholder checks on Select component
1 parent 78ff4e7 commit 51c71db

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

src/Select.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type DefaultOptionValue = string | number | readonly string[] | undefined;
4242
/**
4343
* @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-select>
4444
* */
45-
export const Select = <T extends GenericOption<DefaultOptionValue>[]>(
45+
const SelectComponent = <T extends GenericOption<DefaultOptionValue>[]>(
4646
props: SelectProps<T>,
4747
ref: React.LegacyRef<HTMLDivElement>
4848
) => {
@@ -61,19 +61,22 @@ export const Select = <T extends GenericOption<DefaultOptionValue>[]>(
6161
} = props;
6262

6363
assert<Equals<keyof typeof rest, never>>();
64-
const elementId = nativeSelectProps?.id || useId();
65-
const selectId = `select-${elementId}`;
66-
const stateDescriptionId = `select-${elementId}-desc`;
67-
const displayedOptions = placeholder
68-
? [
69-
{
70-
label: placeholder,
71-
value: "",
72-
disabled: true
73-
},
74-
...options
75-
]
76-
: options;
64+
const elementId = useId();
65+
const selectId = nativeSelectProps?.id || `select-${elementId}`;
66+
const stateDescriptionId = nativeSelectProps?.id
67+
? `${nativeSelectProps?.id}-desc`
68+
: `select-${elementId}-desc`;
69+
const displayedOptions =
70+
placeholder !== undefined
71+
? [
72+
{
73+
label: placeholder,
74+
value: "",
75+
disabled: true
76+
},
77+
...options
78+
]
79+
: options;
7780
return (
7881
<div
7982
className={cx(
@@ -109,8 +112,10 @@ export const Select = <T extends GenericOption<DefaultOptionValue>[]>(
109112
aria-describedby={stateDescriptionId}
110113
disabled={disabled}
111114
>
112-
{displayedOptions.map(option => (
113-
<option {...option}>{option.label}</option>
115+
{displayedOptions.map((option, index) => (
116+
<option {...option} key={`${option.value}-${index}`}>
117+
{option.label}
118+
</option>
114119
))}
115120
</select>
116121
{state !== "default" && (
@@ -135,13 +140,16 @@ export const Select = <T extends GenericOption<DefaultOptionValue>[]>(
135140
);
136141
};
137142

138-
const ForwardedSelect = forwardRef(Select) as <T extends GenericOption<DefaultOptionValue>[]>(
143+
const ForwardedSelect = forwardRef(SelectComponent) as <
144+
T extends GenericOption<DefaultOptionValue>[]
145+
>(
139146
props: SelectProps<T> & { ref?: ForwardedRef<HTMLDivElement> }
140-
) => ReturnType<typeof Select>;
147+
) => ReturnType<typeof SelectComponent>;
141148

142-
const MemoizedSelect = memo(ForwardedSelect) as typeof ForwardedSelect & {
149+
export const Select = memo(ForwardedSelect) as typeof ForwardedSelect & {
143150
displayName: string;
144151
};
145-
MemoizedSelect.displayName = symToStr({ Select });
146152

147-
export default MemoizedSelect;
153+
Select.displayName = symToStr({ Select });
154+
155+
export default Select;

stories/Select.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Select, { type SelectProps, type GenericOption } from "../dist/Select";
1+
import { Select, type SelectProps, type GenericOption } from "../dist/Select";
22
import { sectionName } from "./sectionName";
33
import { getStoryFactory } from "./getStory";
44
import { assert } from "tsafe/assert";

0 commit comments

Comments
 (0)