-
-
Notifications
You must be signed in to change notification settings - Fork 24.7k
fix: keep free-solo dropdown values #6594
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 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||||||||||
| export const DEFAULT_DROPDOWN_VALUE = 'choose an option' | ||||||||||||||
|
|
||||||||||||||
| export const getOptionName = (option) => { | ||||||||||||||
| if (typeof option === 'string') return option | ||||||||||||||
| return option?.name ?? '' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export const getOptionLabel = (option) => { | ||||||||||||||
| if (typeof option === 'string') return option | ||||||||||||||
| return option?.label ?? option?.name ?? '' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export const isEmptyDropdownValue = (value) => value === undefined || value === null || value === '' || value === DEFAULT_DROPDOWN_VALUE | ||||||||||||||
|
|
||||||||||||||
| export const findMatchingOption = (options = [], value) => (options || []).find((option) => option.name === value) | ||||||||||||||
|
|
||||||||||||||
| export const getSingleAutocompleteValue = ({ options = [], value, freeSolo = false }) => { | ||||||||||||||
| const matchingOption = findMatchingOption(options, value) | ||||||||||||||
| if (matchingOption) return matchingOption | ||||||||||||||
|
|
||||||||||||||
| if (freeSolo && !isEmptyDropdownValue(value)) return value | ||||||||||||||
|
|
||||||||||||||
| return '' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export const getSelectionValue = (selection) => { | ||||||||||||||
| if (typeof selection === 'string') return selection | ||||||||||||||
| return selection?.name ?? '' | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export const isOptionEqualToValue = (option, value) => getOptionName(option) === getOptionName(value) | ||||||||||||||
|
Contributor
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. If
Suggested change
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { | ||
| DEFAULT_DROPDOWN_VALUE, | ||
| getOptionLabel, | ||
| getSelectionValue, | ||
| getSingleAutocompleteValue, | ||
| isOptionEqualToValue | ||
| } from './dropdownUtils' | ||
|
|
||
| describe('dropdownUtils', () => { | ||
| const options = [ | ||
| { label: 'GPT 5', name: 'gpt-5' }, | ||
| { label: 'GPT 5 Mini', name: 'gpt-5-mini' } | ||
| ] | ||
|
|
||
| it('keeps a typed free-solo value when it is not in the option list', () => { | ||
| expect( | ||
| getSingleAutocompleteValue({ | ||
| options, | ||
| value: 'gpt-5.4-nano-suporte-juridico', | ||
| freeSolo: true | ||
| }) | ||
| ).toBe('gpt-5.4-nano-suporte-juridico') | ||
| }) | ||
|
|
||
| it('does not keep unknown values when free-solo entry is disabled', () => { | ||
| expect( | ||
| getSingleAutocompleteValue({ | ||
| options, | ||
| value: 'gpt-5.4-nano-suporte-juridico', | ||
| freeSolo: false | ||
| }) | ||
| ).toBe('') | ||
| }) | ||
|
|
||
| it('treats the default placeholder as empty for free-solo fields', () => { | ||
| expect(getSingleAutocompleteValue({ options, value: DEFAULT_DROPDOWN_VALUE, freeSolo: true })).toBe('') | ||
| }) | ||
|
|
||
| it('extracts typed free-solo selections and option selections', () => { | ||
| expect(getSelectionValue('gpt-5.4-nano-suporte-juridico')).toBe('gpt-5.4-nano-suporte-juridico') | ||
| expect(getSelectionValue(options[0])).toBe('gpt-5') | ||
| }) | ||
|
|
||
| it('handles option labels and equality for string values', () => { | ||
| expect(getOptionLabel('gpt-5.4-nano-suporte-juridico')).toBe('gpt-5.4-nano-suporte-juridico') | ||
| expect(isOptionEqualToValue(options[0], 'gpt-5')).toBe(true) | ||
| }) | ||
|
Comment on lines
+44
to
+47
Contributor
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. Add a test case to verify that it('handles option labels and equality for string values', () => {
expect(getOptionLabel('gpt-5.4-nano-suporte-juridico')).toBe('gpt-5.4-nano-suporte-juridico')
expect(isOptionEqualToValue(options[0], 'gpt-5')).toBe(true)
expect(isOptionEqualToValue({ label: 'Select' }, '')).toBe(false)
}) |
||
| }) | ||
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.
According to the repository's general rules, loose equality (
== null) should be used as a standard idiom for a 'nullish' check that covers bothnullandundefinedin JavaScript/TypeScript.References
== null) as a standard idiom for a 'nullish' check that covers bothnullandundefined.