Skip to content

Commit b36bb7a

Browse files
authored
Fix ComboboxOption highlighting current value (#692)
1 parent 3cc8a6e commit b36bb7a

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,27 @@ describe('<Combobox/> with children', () => {
143143
)
144144
})
145145

146+
test('Does not highlight current selected value', () => {
147+
const { getByText, getByPlaceholderText } = renderWithTheme(
148+
<Combobox key="combobox" value={{ label: 'Foo', value: '101' }}>
149+
<ComboboxInput placeholder="Type here" />
150+
<ComboboxList>
151+
<ComboboxOption label="Foo" value="101" />
152+
<ComboboxOption label="FooBar" value="102" />
153+
</ComboboxList>
154+
</Combobox>
155+
)
156+
157+
const input = getByPlaceholderText('Type here')
158+
fireEvent.click(input)
159+
expect(getByText('Foo')).not.toHaveStyle(
160+
'font-weight: 600; text-decoration: underline'
161+
)
162+
expect(getByText('FooBar')).not.toHaveStyle(
163+
'font-weight: 600; text-decoration: underline'
164+
)
165+
})
166+
146167
test.each([
147168
[
148169
'Combobox',

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import { getComboboxText } from './utils/getComboboxText'
5757
import { useOptionEvents } from './utils/useOptionEvents'
5858
import { useOptionStatus } from './utils/useOptionStatus'
5959
import { useAddOptionToContext } from './utils/useAddOptionToContext'
60-
import { ComboboxData, ComboboxMultiData } from './utils/state'
60+
import { ComboboxData } from './utils/state'
6161

6262
export interface ComboboxOptionObject {
6363
/**
@@ -217,10 +217,6 @@ export function ComboboxOptionTextInternal({
217217
const { data } = contextToUse
218218
const { inputValue } = data
219219
const contextOption = (data as ComboboxData).option
220-
const options = contextOption
221-
? [contextOption]
222-
: (data as ComboboxMultiData).options
223-
const optionTexts = options ? options.map(opt => getComboboxText(opt)) : []
224220

225221
const option = useContext(OptionContext)
226222
const text = getComboboxText(option)
@@ -229,9 +225,9 @@ export function ComboboxOptionTextInternal({
229225
!highlightText ||
230226
!inputValue ||
231227
inputValue === '' ||
232-
// inputValue is reflecting a currently selected option
228+
// inputValue is reflecting the currently selected option
233229
// highlighting it would be weird
234-
(text === inputValue && optionTexts.includes(text))
230+
inputValue === getComboboxText(contextOption)
235231
) {
236232
return <span {...props}>{text}</span>
237233
}

packages/playground/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import { GlobalStyle } from '@looker/components'
2424
import { theme } from '@looker/design-tokens'
2525
import { ThemeProvider } from 'styled-components'
2626

27-
import { MenuDemo } from './Menu/MenuDemo'
27+
import { ComboboxDemo } from './Select/ComboboxDemo'
2828

2929
const App: React.FC = () => {
3030
return (
3131
<ThemeProvider theme={theme}>
3232
<GlobalStyle />
33-
<MenuDemo />
33+
<ComboboxDemo />
3434
</ThemeProvider>
3535
)
3636
}

0 commit comments

Comments
 (0)