Skip to content

Commit 24e743b

Browse files
committed
ids in closure, add storybook example + fixed stories with values
1 parent baaa9d3 commit 24e743b

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/Select.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,17 @@ const SelectComponent = <T extends GenericOption<DefaultOptionValue>[]>(
6161
} = props;
6262

6363
assert<Equals<keyof typeof rest, never>>();
64-
const elementId = useId();
65-
const selectId = nativeSelectProps?.id || `select-${elementId}`;
66-
const stateDescriptionId = nativeSelectProps?.id
67-
? `${nativeSelectProps?.id}-desc`
68-
: `select-${elementId}-desc`;
64+
65+
const { selectId, stateDescriptionId } = (function useClosure() {
66+
const elementId = useId();
67+
const selectId = nativeSelectProps?.id ?? `select-${elementId}`;
68+
const stateDescriptionId = nativeSelectProps?.id
69+
? `${nativeSelectProps?.id}-desc`
70+
: `select-${elementId}-desc`;
71+
72+
return { selectId, stateDescriptionId };
73+
})();
74+
6975
const displayedOptions =
7076
placeholder !== undefined
7177
? [

stories/Select.stories.tsx

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,6 @@ export const Default = getStory({
156156

157157
export const DefaultWithPlaceholder = getStory({
158158
"label": "Label pour liste déroulante",
159-
"nativeSelectProps": {
160-
"value": ""
161-
},
162159
"placeholder": "Sélectionnez une option",
163160
"options": defaultOptions
164161
});
@@ -174,29 +171,20 @@ export const SuccessState = getStory({
174171
"label": "Label pour liste déroulante",
175172
"state": "success",
176173
"stateRelatedMessage": "Texte de validation",
177-
"nativeSelectProps": {
178-
"value": "2"
179-
},
180174
"placeholder": "Sélectionnez une option",
181175
"options": defaultOptions
182176
});
183177

184178
export const Disabled = getStory({
185179
"label": "Label pour liste déroulante",
186180
"disabled": true,
187-
"nativeSelectProps": {
188-
"value": ""
189-
},
190181
"placeholder": "Sélectionnez une option",
191182
"options": defaultOptions
192183
});
193184

194185
export const WithHint = getStory({
195186
"label": "Label pour liste déroulante",
196187
"hint": "Texte de description additionnel",
197-
"nativeSelectProps": {
198-
"value": ""
199-
},
200188
"placeholder": "Sélectionnez une option",
201189
"options": defaultOptions
202190
});
@@ -206,7 +194,14 @@ export const TypedSelect = getStory({
206194
"placeholder": "Sélectionnez une option",
207195
"options": optionsWithTypedValues,
208196
"nativeSelectProps": {
209-
"defaultValue": "dc9d15ee-7794-470e-9dcf-a8d1dd1a6fcf",
210-
"value": "dc9ee-7794-470e-9dcf-a8d1dd1a6fcf"
197+
"defaultValue": "dc9d15ee-7794-470e-9dcf-a8d1dd1a6fcf"
211198
}
212199
});
200+
201+
export const SelectWithCustomId = getStory({
202+
"label": "Label pour liste déroulante",
203+
"nativeSelectProps": {
204+
id: "my-unique-id"
205+
},
206+
"options": defaultOptions
207+
});

0 commit comments

Comments
 (0)