mirror of
https://github.com/fergalmoran/turnstone.git
synced 2025-12-30 13:51:20 +00:00
Do not show typeahead unless there is more than one item in the listbox
This commit is contained in:
@@ -83,7 +83,7 @@ const App = () => {
|
|||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
plugins={[[recentSearchesPlugin, {ratio: 2, name: 'Recent', limit: maxItems}]]}
|
plugins={[[recentSearchesPlugin, {ratio: 2, name: 'Recent', limit: maxItems}]]}
|
||||||
styles={autocompleteStyles}
|
styles={autocompleteStyles}
|
||||||
typeahead={false}
|
typeahead={true}
|
||||||
Cancel={CancelButton}
|
Cancel={CancelButton}
|
||||||
Clear={ClearButton}
|
Clear={ClearButton}
|
||||||
GroupName={GroupName}
|
GroupName={GroupName}
|
||||||
|
|||||||
@@ -91,14 +91,13 @@ describe('Integration tests', () => {
|
|||||||
|
|
||||||
const container = screen.getByRole('combobox')
|
const container = screen.getByRole('combobox')
|
||||||
const input = screen.getByPlaceholderText(placeholder)
|
const input = screen.getByPlaceholderText(placeholder)
|
||||||
const typeahead = container.children[1]
|
|
||||||
|
|
||||||
expect(input).toBeDefined()
|
expect(input).toBeDefined()
|
||||||
expect(typeahead).toBeDefined()
|
|
||||||
expect(screen.queryByRole('listbox')).toBeNull()
|
expect(screen.queryByRole('listbox')).toBeNull()
|
||||||
userEvent.type(screen.getByRole('textbox'), 'p')
|
userEvent.type(screen.getByRole('textbox'), 'p')
|
||||||
expect(await screen.findByRole('listbox')).toBeDefined()
|
expect(await screen.findByRole('listbox')).toBeDefined()
|
||||||
expect(typeahead.value).toBe('Peach')
|
expect(container.children[1]).toBeDefined()
|
||||||
|
expect(container.children[1].value).toBe('Peach')
|
||||||
expect(input.value).toBe('P')
|
expect(input.value).toBe('P')
|
||||||
expect(screen.getByRole('option', { name: /peach/i }).getAttribute('aria-selected')).toBe('true')
|
expect(screen.getByRole('option', { name: /peach/i }).getAttribute('aria-selected')).toBe('true')
|
||||||
userEvent.type(screen.getByRole('textbox'), 'i', { skipClick: true })
|
userEvent.type(screen.getByRole('textbox'), 'i', { skipClick: true })
|
||||||
@@ -123,6 +122,6 @@ describe('Integration tests', () => {
|
|||||||
userEvent.type(screen.getByRole('textbox'), '{backspace}', { skipClick: true })
|
userEvent.type(screen.getByRole('textbox'), '{backspace}', { skipClick: true })
|
||||||
expect(screen.queryByRole('listbox')).toBeNull()
|
expect(screen.queryByRole('listbox')).toBeNull()
|
||||||
expect(input.value).toBe('')
|
expect(input.value).toBe('')
|
||||||
expect(typeahead.value).toBe('')
|
expect(container.children[1].value).toBe('')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -31,24 +31,5 @@ exports[`Container > Component renders correctly 1`] = `
|
|||||||
}
|
}
|
||||||
type="text"
|
type="text"
|
||||||
/>
|
/>
|
||||||
<input
|
|
||||||
aria-hidden="true"
|
|
||||||
autoCapitalize="off"
|
|
||||||
autoComplete="off"
|
|
||||||
autoCorrect="off"
|
|
||||||
className="typeahead-class"
|
|
||||||
readOnly="readonly"
|
|
||||||
spellCheck="false"
|
|
||||||
style={
|
|
||||||
{
|
|
||||||
"left": 0,
|
|
||||||
"position": "absolute",
|
|
||||||
"top": 0,
|
|
||||||
"zIndex": 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
tabIndex="-1"
|
|
||||||
type="text"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -78,6 +78,8 @@ export default function Container(props) {
|
|||||||
const typeaheadInput = useRef(null)
|
const typeaheadInput = useRef(null)
|
||||||
|
|
||||||
// Calculated states
|
// Calculated states
|
||||||
|
console.log('hasTypeahead', typeahead, state.items.length > 1)
|
||||||
|
const hasTypeahead = typeahead && state.items.length > 1
|
||||||
const hasClearButton = clearButton && !!state.query
|
const hasClearButton = clearButton && !!state.query
|
||||||
const hasCancelButton = cancelButton && hasFocus
|
const hasCancelButton = cancelButton && hasFocus
|
||||||
const isExpanded = hasFocus && state.itemsLoaded
|
const isExpanded = hasFocus && state.itemsLoaded
|
||||||
@@ -241,7 +243,7 @@ export default function Container(props) {
|
|||||||
aria-controls={listboxId}
|
aria-controls={listboxId}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{typeahead && (
|
{hasTypeahead && (
|
||||||
<input
|
<input
|
||||||
className={`${inputStyles || ''} ${customStyles.typeahead || ''}`.trim()}
|
className={`${inputStyles || ''} ${customStyles.typeahead || ''}`.trim()}
|
||||||
style={defaultStyles.typeahead}
|
style={defaultStyles.typeahead}
|
||||||
|
|||||||
Reference in New Issue
Block a user