Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/hooks/useCombobox/__tests__/getInputProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ afterAll(jest.restoreAllMocks)

describe('getInputProps', () => {
describe('hook props', () => {
test('returns an object with some default props', () => {
const {result} = renderUseCombobox()
const inputProps = result.current.getInputProps()

expect(Object.keys(inputProps)).toEqual([
'ref',
'aria-activedescendant',
'aria-autocomplete',
'aria-controls',
'aria-expanded',
'aria-labelledby',
'aria-label',
'autoComplete',
'id',
'role',
'value',
'onChange',
'onKeyDown',
'onBlur',
'onClick',
])
Comment thread
silviuaavram marked this conversation as resolved.
})
Comment thread
silviuaavram marked this conversation as resolved.

test("assign 'combobox' to role", () => {
const {result} = renderUseCombobox()
const inputProps = result.current.getInputProps()
Expand Down
14 changes: 14 additions & 0 deletions src/hooks/useCombobox/__tests__/getToggleButtonProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ afterAll(jest.restoreAllMocks)

describe('getToggleButtonProps', () => {
describe('hook props', () => {
test('returns an object with some default props', () => {
const {result} = renderUseCombobox()
const toggleButtonProps = result.current.getToggleButtonProps()

expect(Object.keys(toggleButtonProps)).toEqual([
'ref',
'aria-controls',
'aria-expanded',
'id',
'tabIndex',
'onClick',
])
Comment thread
silviuaavram marked this conversation as resolved.
})
Comment thread
silviuaavram marked this conversation as resolved.

test('assign default value to id', () => {
const {result} = renderUseCombobox()
const toggleButtonProps = result.current.getToggleButtonProps()
Expand Down
18 changes: 10 additions & 8 deletions src/hooks/useCombobox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ import {
dropdownDefaultProps,
useScrollIntoView,
} from '../utils'
import {type GetPropsCommonOptions} from '../../downshift.types'
import {getInitialState, useControlledReducer, propTypes} from './utils'
import downshiftUseComboboxReducer from './reducer'
import * as stateChangeTypes from './stateChangeTypes'
import {
UseComboboxGetInputProps,
UseComboboxGetInputPropsOptions,
UseComboboxGetInputPropsReturnValue,
UseComboboxGetItemProps,
UseComboboxGetLabelProps,
UseComboboxGetMenuProps,
UseComboboxGetToggleButtonProps,
UseComboboxGetToggleButtonPropsOptions,
UseComboboxMergedProps,
UseComboboxProps,
UseComboboxReturnValue,
Expand Down Expand Up @@ -366,13 +369,12 @@ function useCombobox<Item>(
) as UseComboboxGetItemProps<Item>

const getToggleButtonProps = useCallback(
toggleButtonProps => {
(toggleButtonProps?: UseComboboxGetToggleButtonPropsOptions) => {
const {
onClick,
onPress,
refKey = 'ref',
ref,
disabled,
...rest
} = toggleButtonProps ?? {}
const latestState = latest.current.state
Expand All @@ -390,7 +392,7 @@ function useCombobox<Item>(
'aria-expanded': latestState.isOpen,
id: elementIds.toggleButtonId,
tabIndex: -1,
...(!disabled && {
...(!rest.disabled && {
...(isReactNative || isReactNativeWeb
? /* istanbul ignore next (react-native) */ {
onPress: callAllEventHandlers(onPress, toggleButtonHandleClick),
Expand All @@ -399,18 +401,19 @@ function useCombobox<Item>(
onClick: callAllEventHandlers(onClick, toggleButtonHandleClick),
}),
}),
disabled,
...rest,
}
},
[dispatch, latest, elementIds],
) as UseComboboxGetToggleButtonProps

const getInputProps = useCallback(
(inputProps, otherProps) => {
(
inputProps?: UseComboboxGetInputPropsOptions,
otherProps?: GetPropsCommonOptions,
) => {
const {
'aria-label': ariaLabel,
disabled,
onKeyDown,
onChange,
onInput,
Expand Down Expand Up @@ -484,7 +487,7 @@ function useCombobox<Item>(
| 'onChangeText'
>

if (!disabled) {
if (!rest.disabled) {
eventHandlers = {
[onChangeKey]: callAllEventHandlers(
onChange,
Expand Down Expand Up @@ -531,7 +534,6 @@ function useCombobox<Item>(
// https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
// revert back since autocomplete="nope" is ignored on latest Chrome and Opera
autoComplete: 'off',
disabled,
id: elementIds.inputId,
role: 'combobox',
value: latestState.inputValue,
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useCombobox/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,6 @@ export interface UseComboboxGetInputPropsReturnValue {
'aria-labelledby': string | undefined
'aria-label': string | undefined
autoComplete: 'off'
disabled: boolean | undefined
id: string
role: 'combobox'
value: string
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useSelect/__tests__/getToggleButtonProps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ afterAll(jest.restoreAllMocks)

describe('getToggleButtonProps', () => {
describe('hook props', () => {
test('returns an object with some default props', () => {
const {result} = renderUseSelect()
const toggleButtonProps = result.current.getToggleButtonProps()

expect(Object.keys(toggleButtonProps)).toEqual([
'ref',
'aria-activedescendant',
'aria-controls',
'aria-expanded',
'aria-haspopup',
'aria-labelledby',
'id',
'role',
'tabIndex',
'onBlur',
'onClick',
'onKeyDown',
])
Comment thread
silviuaavram marked this conversation as resolved.
})
Comment thread
silviuaavram marked this conversation as resolved.

test('assign default value to aria-labelledby', () => {
const {result} = renderUseSelect()
const toggleButtonProps = result.current.getToggleButtonProps()
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/useSelect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
dropdownDefaultProps,
useScrollIntoView,
} from '../utils'
import {GetPropsCommonOptions} from '../../downshift.types'
Comment thread
silviuaavram marked this conversation as resolved.
Comment thread
silviuaavram marked this conversation as resolved.
import {isReactNative, isReactNativeWeb} from '../../is.macro'
import downshiftSelectReducer from './reducer'
import {propTypes} from './utils'
Expand All @@ -31,6 +32,7 @@ import {
UseSelectGetLabelProps,
UseSelectGetMenuProps,
UseSelectGetToggleButtonProps,
UseSelectGetToggleButtonPropsOptions,
UseSelectMergedProps,
UseSelectProps,
UseSelectReducerAction,
Expand Down Expand Up @@ -301,8 +303,8 @@ function useSelect<Item>(

const getToggleButtonProps = useCallback(
(
toggleButtonProps?: Parameters<UseSelectGetToggleButtonProps>[0],
otherProps?: Parameters<UseSelectGetToggleButtonProps>[1],
toggleButtonProps?: UseSelectGetToggleButtonPropsOptions,
otherProps?: GetPropsCommonOptions,
) => {
const {
onBlur,
Expand All @@ -311,7 +313,6 @@ function useSelect<Item>(
onKeyDown,
refKey = 'ref',
ref,
disabled,
...rest
} = toggleButtonProps ?? {}
const {suppressRefError = false} = otherProps ?? {}
Expand Down Expand Up @@ -364,11 +365,10 @@ function useSelect<Item>(
role: 'combobox',
tabIndex: 0,
onBlur: callAllEventHandlers(onBlur, toggleButtonHandleBlur),
disabled,
...rest,
}

if (!disabled) {
if (!rest.disabled) {
/* istanbul ignore if (react-native) */
if (isReactNative || isReactNativeWeb) {
Object.assign(toggleProps, {
Expand Down
Loading