@@ -63,9 +63,16 @@ export const Select = memo(
63
63
return `select-${ id } ` ;
64
64
} ) ( ) ;
65
65
66
- const stateDescriptionId = `select-${ useId ( ) } -desc` ;
67
66
const messagesGroupId = `${ selectId } -messages-group` ;
68
67
68
+ const selectStateDescriptionId = useId ( ) ;
69
+ const stateIsDefault = state === "default" ;
70
+
71
+ const stateDescriptionId = getStateDescriptionId ( stateIsDefault , selectStateDescriptionId ) ;
72
+ const nativeDescription = getSelectNativeDescription ( nativeSelectProps [ "aria-describedby" ] ) ;
73
+
74
+ const selectAriaDescribedBy = `${ stateDescriptionId } ${ nativeDescription } ` . trim ( ) ;
75
+
69
76
return (
70
77
< div
71
78
id = { id }
@@ -104,17 +111,13 @@ export const Select = memo(
104
111
{ ...nativeSelectProps }
105
112
className = { cx ( fr . cx ( "fr-select" ) , nativeSelectProps . className ) }
106
113
id = { selectId }
107
- aria-describedby = {
108
- nativeSelectProps [ "aria-describedby" ] !== undefined
109
- ? `${ stateDescriptionId } ${ nativeSelectProps [ "aria-describedby" ] } `
110
- : stateDescriptionId
111
- }
114
+ aria-describedby = { selectAriaDescribedBy }
112
115
disabled = { disabled }
113
116
>
114
117
{ children }
115
118
</ select >
116
119
< div id = { messagesGroupId } className = { fr . cx ( "fr-messages-group" ) } aria-live = "polite" >
117
- { state !== "default" && (
120
+ { ! stateIsDefault && (
118
121
< p
119
122
id = { stateDescriptionId }
120
123
className = { fr . cx (
@@ -140,6 +143,16 @@ export const Select = memo(
140
143
} )
141
144
) ;
142
145
146
+ function getStateDescriptionId ( stateIsDefault : boolean , selectStateDescriptionId : string ) {
147
+ return stateIsDefault ? "" : `select-${ selectStateDescriptionId } -desc` ;
148
+ }
149
+
150
+ function getSelectNativeDescription (
151
+ nativeSelectPropsAriaDescribedBy : React . SelectHTMLAttributes < HTMLSelectElement > [ "aria-describedby" ]
152
+ ) {
153
+ return nativeSelectPropsAriaDescribedBy !== undefined ? nativeSelectPropsAriaDescribedBy : "" ;
154
+ }
155
+
143
156
Select . displayName = symToStr ( { Select } ) ;
144
157
145
158
export default Select ;
0 commit comments