Skip to content

Commit 6c6abc7

Browse files
authored
Combobox: input value reflects option label change (#1304)
1 parent b1dc956 commit 6c6abc7

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Fixed
1818

19+
- `Select` display value now properly updates when the option label for the current value changes
1920
- `InputChips` and `SelectMulti` overflow when a fixed height is used
2021
- `InputChips` and `SelectMulti` long values breaking out of the input
2122
- `InputSearch`, `Select`, `InputChips` and `SelectMulti` x button not clickable with `autoResize` and a max-width reached

packages/components/src/Form/Inputs/Combobox/Combobox.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
TypographyProps,
3535
SpaceProps,
3636
} from '@looker/design-tokens'
37+
import isMatch from 'lodash/isMatch'
3738
import React, { forwardRef, Ref } from 'react'
3839
import styled from 'styled-components'
3940
import { useID } from '../../../utils'
@@ -122,7 +123,7 @@ export const ComboboxInternal = forwardRef(
122123
})
123124
const { lastActionType, option } = data
124125

125-
if (value !== undefined && (!option || option.value !== value.value)) {
126+
if (value !== undefined && (!option || !isMatch(option, value))) {
126127
transition &&
127128
transition(ComboboxActionType.SELECT_SILENT, {
128129
option: value,

packages/components/src/Form/Inputs/Select/Select.test.tsx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
2525
*/
2626

27-
import { renderWithTheme } from '@looker/components-test-utils'
27+
import {
28+
getAllComboboxOptionText,
29+
renderWithTheme,
30+
} from '@looker/components-test-utils'
2831
import { cleanup, fireEvent, screen } from '@testing-library/react'
2932
import React, { useState } from 'react'
3033

34+
import { Button } from '../../../Button'
3135
import { ComboboxOptionIndicatorFunction } from '../Combobox'
3236
import { Select } from './Select'
3337
import { SelectMulti } from './SelectMulti'
@@ -628,4 +632,48 @@ describe('Select', () => {
628632
// should default to first option
629633
expect(input).toHaveValue('Foo')
630634
})
635+
636+
test('displayed value changes when option label changes', () => {
637+
const Component = () => {
638+
const [label, setLabel] = useState('Original Label')
639+
const options = [
640+
{ label, value: 'value_stays_the_same' },
641+
{ label: 'Another Option', value: 'other' },
642+
]
643+
return (
644+
<>
645+
<Button onClick={() => setLabel('Updated label')}>
646+
Update label
647+
</Button>
648+
<Select value="value_stays_the_same" options={options} />
649+
</>
650+
)
651+
}
652+
renderWithTheme(<Component />)
653+
654+
const input = screen.getByDisplayValue('Original Label')
655+
fireEvent.click(input)
656+
expect(getAllComboboxOptionText()).toMatchInlineSnapshot(`
657+
Array [
658+
"Original Label",
659+
"Another Option",
660+
]
661+
`)
662+
// Close list
663+
fireEvent.click(document)
664+
665+
fireEvent.click(screen.getByText('Update label'))
666+
expect(input).toHaveDisplayValue('Updated label')
667+
668+
fireEvent.click(input)
669+
expect(getAllComboboxOptionText()).toMatchInlineSnapshot(`
670+
Array [
671+
"Updated label",
672+
"Another Option",
673+
]
674+
`)
675+
676+
// Close popover to silence act() warning
677+
fireEvent.click(document)
678+
})
631679
})

storybook/src/Forms/Select.stories.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ import {
4343
ComboboxOptionObject,
4444
SelectOptionProps,
4545
SelectOptionGroupProps,
46+
Space,
4647
SpaceVertical,
4748
Text,
4849
Flex,
50+
useToggle,
4951
} from '@looker/components'
5052
import { options1k } from './options1k'
5153

@@ -59,6 +61,7 @@ export const All = () => (
5961
<Inline />
6062
<InlineError />
6163
<SelectDemo />
64+
<UpdateOptions />
6265
</Fieldset>
6366
)
6467

@@ -363,3 +366,22 @@ export const SelectDemo = () => {
363366
</>
364367
)
365368
}
369+
370+
export const UpdateOptions = () => {
371+
const [value, setValue] = useState('second')
372+
const { value: isPlural, toggle } = useToggle()
373+
const s = isPlural ? 's' : ''
374+
const options = useMemo(
375+
() => [
376+
{ label: `Second${s}`, value: 'second' },
377+
{ label: `Hour${s}`, value: 'hour' },
378+
],
379+
[s]
380+
)
381+
return (
382+
<Space>
383+
<Button onClick={toggle}>Use {isPlural ? 'singular' : 'plural'}</Button>
384+
<Select autoResize options={options} value={value} onChange={setValue} />
385+
</Space>
386+
)
387+
}

0 commit comments

Comments
 (0)