Skip to content
Myr

fix(jsx/dom): apply select value after children are rendered (#4847)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx/dom): apply select value after children are rendered When options and value were set simultaneously on a select element, the value was applied before option children existed in the DOM, causing selectedIndex to fall back to 0. Defer select value assignment to after children rendering, similar to Preact's approach. This ensures options exist in the DOM before value matching, while other props like `multiple` are still applied before children. * fix(jsx/dom): fix select value sync edge cases For multiple selects with no matching option, selectedIndex was incorrectly forced to 0. Only reset selectedIndex for single selects, preserving the browser's default -1 for unmatched multiple selects. * perf(jsx/dom): use child.tag instead of el.nodeName for select check Avoid DOM property access in the hot render loop by checking the JSX node's tag property directly.

authored by Taku Amano · committed by GitHub · parent f47b559 · Browse files · 2 files+114 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+114 added−9 removed

  • tests:+91 added−1 removed
  • other:+23 added−8 removed

2 files+114 added−9 removed

modified:src/jsx/dom/index.test.tsx+91 added-1 removed
902 unmodified lines
903904 it('assign undefined', async () => {905 let setValue: (value: string | undefined) => void = () => {}906 const App = () => {907 const [value, _setValue] = useState<string | undefined>('a')907 const [value, _setValue] = useState<string | undefined>('b')908 setValue = _setValue909 return (910 <select value={value}>911 <option value='a'>A</option>
9 unmodified lines
921 setValue(undefined)922 await Promise.resolve()923 const select = root.querySelector('select') as HTMLSelectElement924 expect(select.value).toBe('a') // select the first option925 expect(select.selectedIndex).toBe(0)926 })927928 it('apply value after options are added', async () => {929 let setOptions: (options: string[]) => void = () => {}930 const App = () => {931 const [options, _setOptions] = useState<string[]>([])932 setOptions = _setOptions933 return (934 <select value='option2'>935 {options.map((opt) => (936 <option key={opt} value={opt}>937 {opt}938 </option>939 ))}940 </select>941 )942 }943 render(<App />, root)944 setOptions(['option1', 'option2', 'option3'])945 await Promise.resolve()946 const select = root.querySelector('select') as HTMLSelectElement947 expect(select.value).toBe('option2')948 expect(select.selectedIndex).toBe(1)949 expect(select.options[1].selected).toBe(true)950 })951952 it('select the first option when undefined after options are added', async () => {953 let setOptions: (options: string[]) => void = () => {}954 const App = () => {955 const [options, _setOptions] = useState<string[]>([])956 setOptions = _setOptions957 return (958 <select value={undefined}>959 {options.map((opt) => (960 <option key={opt} value={opt}>961 {opt}962 </option>963 ))}964 </select>965 )966 }967 render(<App />, root)968 setOptions(['option1', 'option2', 'option3'])969 await Promise.resolve()970 const select = root.querySelector('select') as HTMLSelectElement971 expect(select.value).toBe('option1')972 expect(select.selectedIndex).toBe(0)973 expect(select.options[0].selected).toBe(true)974 })975976 it('do not select the first option for invalid multiple value', () => {977 const App = () => {978 return (979 <select multiple value='z'>980 <option value='a'>A</option>981 <option value='b'>B</option>982 <option value='c'>C</option>983 </select>984 )985 }986 render(<App />, root)987 const select = root.querySelector('select') as HTMLSelectElement988 expect(select.value).toBe('')989 expect(select.selectedIndex).toBe(-1)990 expect([...select.options].every((option) => !option.selected)).toBe(true)991 })992993 it('keep invalid multiple value unselected after options are added', async () => {994 let setOptions: (options: string[]) => void = () => {}995 const App = () => {996 const [options, _setOptions] = useState<string[]>([])997 setOptions = _setOptions998 return (999 <select multiple value='z'>1000 {options.map((opt) => (1001 <option key={opt} value={opt}>1002 {opt}1003 </option>1004 ))}1005 </select>1006 )1007 }1008 render(<App />, root)1009 setOptions(['a', 'b', 'c'])1010 await Promise.resolve()1011 const select = root.querySelector('select') as HTMLSelectElement1012 expect(select.value).toBe('')1013 expect(select.selectedIndex).toBe(-1)1014 expect([...select.options].every((option) => !option.selected)).toBe(true)1015 })1016 })10171018 describe('option', () => {1019 it('selected', async () => {
1804 unmodified lines
modified:src/jsx/dom/render.ts+23 added-8 removed
139 unmodified lines
140 key.match(/^(?:o|pai|str|u|ve)/)) // Other un-deprecated kebab-case attributes. "overline-position", "paint-order", "strikethrough-position", etc.141 ? key.replace(/([A-Z])/g, '-$1').toLowerCase()142 : key143144const normalizeFormValue = (value: Props['value']) =>145 value === null || value === undefined || value === false ? null : value146147const applySelectValue = (select: HTMLSelectElement, props: Props): void => {148 if (!('value' in props)) {149 return150 }151152 select.value = normalizeFormValue(props['value'])153 if (!select.multiple && select.selectedIndex === -1) {154 select.selectedIndex = 0155 }156}157158const applyProps = (159 container: SupportedElement,160 attributes: Props,161 oldAttributes?: Props
39 unmodified lines
201 }202 } else {203 if (key === 'value') {204 const nodeName = container.nodeName191 if (nodeName === 'INPUT' || nodeName === 'TEXTAREA' || nodeName === 'SELECT') {192 ;(container as unknown as HTMLInputElement).value =193 value === null || value === undefined || value === false ? null : value205 if (nodeName === 'SELECT') {206 // Deferred to applySelectValue() after children are rendered207 continue208 } else if (nodeName === 'INPUT' || nodeName === 'TEXTAREA') {209 ;(container as unknown as HTMLInputElement).value = normalizeFormValue(value)210211 if (nodeName === 'TEXTAREA') {212 container.textContent = value213 continue198 } else if (nodeName === 'SELECT') {199 if ((container as unknown as HTMLSelectElement).selectedIndex === -1) {200 ;(container as unknown as HTMLSelectElement).selectedIndex = 0201 }202 continue214 }215 }216 } else if (217 (key === 'checked' && container.nodeName === 'INPUT') ||
196 unmodified lines
414 } else {415 el = child.e ||= child.n416 ? (document.createElementNS(child.n, child.tag as string) as SVGElement | MathMLElement)417 : document.createElement(child.tag as string)418419 applyProps(el as HTMLElement, child.props, child.pP)420 applyNodeObject(child, el as HTMLElement, isNewLocal)421 if (child.tag === 'select') {422 applySelectValue(el as HTMLSelectElement, child.props)423 }424 }425 }426 if (child.tag === HONO_PORTAL_ELEMENT) {427 offset--
380 unmodified lines