mirror of
https://github.com/fergalmoran/turnstone.git
synced 2025-12-22 09:49:56 +00:00
Use autofocus HTML attribute instead of a useEffect hook
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
### 1.1.2 (15 Mar 2022)
|
### 1.1.2 (15 Mar 2022)
|
||||||
|
|
||||||
|
- Use autofocus HTML attribute instead of a useEffect hook
|
||||||
|
- Add MIT License
|
||||||
- Add precommit hooks for test and lint
|
- Add precommit hooks for test and lint
|
||||||
- Ensure a no-items message is displayed if a query is entered but no items have ever been loaded
|
- Ensure a no-items message is displayed if a query is entered but no items have ever been loaded
|
||||||
- Do not do any text matching in default listbox
|
- Do not do any text matching in default listbox
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ exports[`Container > Component renders correctly 1`] = `
|
|||||||
autoCapitalize="off"
|
autoCapitalize="off"
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
autoCorrect="off"
|
autoCorrect="off"
|
||||||
|
autoFocus={true}
|
||||||
className="query-class"
|
className="query-class"
|
||||||
id="autocomplete"
|
id="autocomplete"
|
||||||
onBlur={[Function]}
|
onBlur={[Function]}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import defaultStyles from './styles/container.styles.js'
|
|||||||
import {
|
import {
|
||||||
useItemsState,
|
useItemsState,
|
||||||
useItemsError,
|
useItemsError,
|
||||||
useAutoFocus,
|
|
||||||
useQueryChange,
|
useQueryChange,
|
||||||
useHighlight,
|
useHighlight,
|
||||||
useSelected
|
useSelected
|
||||||
@@ -119,9 +118,6 @@ export default function Container(props) {
|
|||||||
// Store retrieved error if there is one
|
// Store retrieved error if there is one
|
||||||
useItemsError(swrResult.error)
|
useItemsError(swrResult.error)
|
||||||
|
|
||||||
// Autofocus on render if prop is true
|
|
||||||
useAutoFocus(queryInput, autoFocus)
|
|
||||||
|
|
||||||
// As soon as the query state changes (ignoring debounce) update the
|
// As soon as the query state changes (ignoring debounce) update the
|
||||||
// typeahead value and the query value and fire onChange
|
// typeahead value and the query value and fire onChange
|
||||||
useQueryChange(state.query, queryInput, typeaheadInput, onChange)
|
useQueryChange(state.query, queryInput, typeaheadInput, onChange)
|
||||||
@@ -230,6 +226,7 @@ export default function Container(props) {
|
|||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
placeholder={placeholder}
|
placeholder={placeholder}
|
||||||
type='text'
|
type='text'
|
||||||
|
autoFocus={autoFocus}
|
||||||
autoComplete='off'
|
autoComplete='off'
|
||||||
autoCorrect='off'
|
autoCorrect='off'
|
||||||
autoCapitalize='off'
|
autoCapitalize='off'
|
||||||
|
|||||||
@@ -22,12 +22,6 @@ export const useItemsError = (error) => {
|
|||||||
}, [error, dispatch])
|
}, [error, dispatch])
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useAutoFocus = (queryInput, autoFocus) => { // TODO: might be able to use autofocus property of input for this - https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-autofocus
|
|
||||||
useEffect(() => {
|
|
||||||
if (autoFocus) queryInput.current.focus()
|
|
||||||
}, [autoFocus, queryInput])
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useQueryChange = (query, queryInput, typeaheadInput, onChange) => {
|
export const useQueryChange = (query, queryInput, typeaheadInput, onChange) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const hasTypeahead = !!typeaheadInput.current
|
const hasTypeahead = !!typeaheadInput.current
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { vi, describe, test, expect } from 'vitest'
|
|||||||
import { renderHook } from '@testing-library/react-hooks'
|
import { renderHook } from '@testing-library/react-hooks'
|
||||||
import undef from '../../utils/undef'
|
import undef from '../../utils/undef'
|
||||||
import {
|
import {
|
||||||
useAutoFocus,
|
|
||||||
useQueryChange,
|
useQueryChange,
|
||||||
useHighlight,
|
useHighlight,
|
||||||
useSelected,
|
useSelected,
|
||||||
@@ -19,22 +18,6 @@ let inputRef = (value = '') => ( //TODO: Put in a beforeEach when blogging
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
describe('useAutoFocus', () => {
|
|
||||||
test('Focus is set when autofocus is true', () => {
|
|
||||||
const ref = inputRef()
|
|
||||||
const autoFocus = true
|
|
||||||
renderHook(() => useAutoFocus(ref, autoFocus))
|
|
||||||
expect(ref.current.focus).toHaveBeenCalledTimes(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('Focus is not set when autofocus is false', () => {
|
|
||||||
const ref = inputRef()
|
|
||||||
const autoFocus = false
|
|
||||||
renderHook(() => useAutoFocus(ref, autoFocus))
|
|
||||||
expect(ref.current.focus).toHaveBeenCalledTimes(0)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe('useQueryChange', () => {
|
describe('useQueryChange', () => {
|
||||||
const typeaheadValue = 'Chicago, Illinois, United States'
|
const typeaheadValue = 'Chicago, Illinois, United States'
|
||||||
const queryValue = 'Chi'
|
const queryValue = 'Chi'
|
||||||
|
|||||||
Reference in New Issue
Block a user