@@ -67,7 +67,7 @@ function collectOptionValues(children: unknown): PicklistValue[] {
67
67
function findSelectedItemLabel (
68
68
children : unknown ,
69
69
selectedValue : PicklistValue
70
- ) : React . ReactNode | null {
70
+ ) : string | number | null {
71
71
return (
72
72
React . Children . map ( children , ( child ) => {
73
73
if ( ! React . isValidElement ( child ) ) {
@@ -107,21 +107,56 @@ function findSelectedItemLabel(
107
107
typeof label === 'string' ||
108
108
typeof label === 'number' ||
109
109
React . isValidElement ( label )
110
- ? label
110
+ ? extractTextContent ( label )
111
111
: undefined ;
112
112
const childrenValue =
113
113
typeof itemChildren === 'string' ||
114
114
typeof itemChildren === 'number' ||
115
115
React . isValidElement ( itemChildren ) ||
116
116
Array . isArray ( itemChildren )
117
- ? itemChildren
117
+ ? extractTextContent ( itemChildren )
118
118
: undefined ;
119
119
120
120
return labelValue || childrenValue ;
121
121
} ) . find ( ( result ) => result !== null ) ?? null
122
122
) ;
123
123
}
124
124
125
+ /**
126
+ * Extract text content from React node recursively
127
+ */
128
+ function extractTextContent ( node : unknown ) : string | number | null {
129
+ if ( node == null ) {
130
+ return null ;
131
+ }
132
+
133
+ if ( typeof node === 'string' || typeof node === 'number' ) {
134
+ return node ;
135
+ }
136
+
137
+ if ( typeof node === 'boolean' ) {
138
+ return String ( node ) ;
139
+ }
140
+
141
+ if ( Array . isArray ( node ) ) {
142
+ return node
143
+ . map ( extractTextContent )
144
+ . filter ( ( result ) => result !== null )
145
+ . join ( '' ) ;
146
+ }
147
+
148
+ if (
149
+ React . isValidElement ( node ) &&
150
+ node . props &&
151
+ typeof node . props === 'object' &&
152
+ 'children' in node . props
153
+ ) {
154
+ return extractTextContent ( node . props . children ) ;
155
+ }
156
+
157
+ return null ;
158
+ }
159
+
125
160
/**
126
161
*
127
162
*/
0 commit comments