-
Notifications
You must be signed in to change notification settings - Fork 6.5k
refactor: remove select dual states #8821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'; | ||
| import * as SelectPrimitive from '@radix-ui/react-select'; | ||
| import classNames from 'classnames'; | ||
| import { useEffect, useId, useMemo, useState } from 'react'; | ||
| import { useId, useMemo } from 'react'; | ||
|
|
||
| import Skeleton from '#ui/Common/Skeleton'; | ||
|
|
||
|
|
@@ -28,7 +28,7 @@ export type SelectGroup<T extends string> = { | |
|
|
||
| export type SelectProps<T extends string> = { | ||
| values: Array<SelectGroup<T>> | Array<T> | Array<SelectValue<T>>; | ||
| defaultValue?: T; | ||
| value?: T; | ||
| placeholder?: string; | ||
| label?: string; | ||
| inline?: boolean; | ||
|
|
@@ -49,7 +49,7 @@ export type SelectProps<T extends string> = { | |
|
|
||
| const Select = <T extends string>({ | ||
| values = [], | ||
| defaultValue, | ||
| value, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests pass obsolete
|
||
| placeholder, | ||
| label, | ||
| inline, | ||
|
|
@@ -62,9 +62,6 @@ const Select = <T extends string>({ | |
| fallbackClass = '', | ||
| }: SelectProps<T>): ReactNode => { | ||
| const id = useId(); | ||
| const [value, setValue] = useState(defaultValue); | ||
|
|
||
| useEffect(() => setValue(defaultValue), [defaultValue]); | ||
|
|
||
| const mappedValues = useMemo(() => mapValues(values), [values]) as Array< | ||
| SelectGroup<T> | ||
|
|
@@ -111,10 +108,7 @@ const Select = <T extends string>({ | |
| // eslint-disable-next-line @eslint-react/exhaustive-deps | ||
| }, [JSON.stringify(values)]); | ||
|
|
||
| // Both change the internal state and emit the change event | ||
| const handleChange = (value: T) => { | ||
| setValue(value); | ||
|
|
||
| if (typeof onChange === 'function') { | ||
| onChange(value); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithNoScriptSelectaccepts a prop nameddefaultValue, but it is now passed through to the interactive<Select>as a controlledvalue. If a consumer provides a staticdefaultValuewithout updating it ononChange, the selection will appear “stuck” (no internal updates). To avoid this API trap, consider renaming this prop tovalue(and/or adding a wrapper state for the JS-enabled Select while keepingdefaultValuesemantics for the noscript version).