Skip to content

Commit d3eaf18

Browse files
committed
fix types and usages
1 parent 8f0ede6 commit d3eaf18

File tree

22 files changed

+232
-161
lines changed

22 files changed

+232
-161
lines changed

src/hooks/__tests__/utils.test.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {renderHook} from '@testing-library/react'
22
import {useMouseAndTouchTracker, isDropdownsStateEqual} from '../utils'
3-
import {getInitialValue, getDefaultValue, getItemAndIndex} from '../utils-ts'
3+
import {getItemAndIndex} from '../utils-ts'
44
import {dropdownDefaultProps} from '../utils.dropdown'
55

66
describe('utils', () => {
@@ -43,39 +43,6 @@ describe('utils', () => {
4343
})
4444
})
4545

46-
test('getInitialValue will not return undefined as initial value', () => {
47-
const defaults = {bogusValue: 'hello'}
48-
const value = getInitialValue(
49-
{initialBogusValue: undefined},
50-
'bogusValue',
51-
defaults,
52-
)
53-
54-
expect(value).toEqual(defaults.bogusValue)
55-
})
56-
57-
test('getInitialValue will not return undefined as value', () => {
58-
const defaults = {bogusValue: 'hello'}
59-
const value = getInitialValue(
60-
{bogusValue: undefined},
61-
'bogusValue',
62-
defaults,
63-
)
64-
65-
expect(value).toEqual(defaults.bogusValue)
66-
})
67-
68-
test('getDefaultValue will not return undefined as value', () => {
69-
const defaults = {bogusValue: 'hello'}
70-
const value = getDefaultValue(
71-
{defaultBogusValue: undefined},
72-
'bogusValue',
73-
defaults,
74-
)
75-
76-
expect(value).toEqual(defaults.bogusValue)
77-
})
78-
7946
describe('useMouseAndTouchTracker', () => {
8047
test('renders without error', () => {
8148
expect(() => {

src/hooks/reducer.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ export default function downshiftCommonReducer(
6767
case stateChangeTypes.FunctionReset:
6868
changes = {
6969
highlightedIndex: getDefaultHighlightedIndex(props),
70-
isOpen: getDefaultValue(props, 'isOpen', dropdownDefaultStateValues),
70+
isOpen: getDefaultValue(
71+
props.defaultIsOpen,
72+
dropdownDefaultStateValues.isOpen,
73+
),
7174
selectedItem: getDefaultValue(
72-
props,
73-
'selectedItem',
74-
dropdownDefaultStateValues,
75+
props.defaultSelectedItem,
76+
dropdownDefaultStateValues.selectedItem,
7577
),
7678
inputValue: getDefaultValue(
77-
props,
78-
'inputValue',
79-
dropdownDefaultStateValues,
79+
props.defaultInputValue,
80+
dropdownDefaultStateValues.inputValue,
8081
),
8182
}
8283

src/hooks/testUtils.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,27 +114,27 @@ export async function tab(shiftKey = false) {
114114

115115
// format is: [initialIsOpen, defaultIsOpen, props.isOpen]
116116
export const initialFocusAndOpenTestCases = [
117-
[undefined, undefined, true, true],
118-
[true, true, true, true],
119-
[true, false, true, true],
120-
[false, true, true, true],
121-
[false, false, true, true],
122-
[true, undefined, undefined, true],
123-
[true, false, undefined, true],
124-
[true, true, undefined, true],
125-
[undefined, true, undefined, true],
117+
[undefined, undefined, true],
118+
[true, true, true],
119+
[true, false, true],
120+
[false, true, true],
121+
[false, false, true],
122+
[true, undefined, undefined],
123+
[true, false, undefined],
124+
[true, true, undefined],
125+
[undefined, true, undefined],
126126
]
127127

128128
// format is: [initialIsOpen, defaultIsOpen, props.isOpen]
129129
export const initialNoFocusOrOpenTestCases = [
130-
[undefined, undefined, undefined, false],
131-
[undefined, undefined, false, false],
132-
[true, true, false, false],
133-
[true, false, false, false],
134-
[false, true, false, false],
135-
[false, false, false, false],
136-
[false, undefined, undefined, false],
137-
[false, false, undefined, false],
138-
[false, true, undefined, false],
139-
[undefined, false, undefined, false],
130+
[undefined, undefined, undefined],
131+
[undefined, undefined, false],
132+
[true, true, false],
133+
[true, false, false],
134+
[false, true, false],
135+
[false, false, false],
136+
[false, undefined, undefined],
137+
[false, false, undefined],
138+
[false, true, undefined],
139+
[undefined, false, undefined],
140140
]

src/hooks/useCombobox/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
useA11yMessageStatus,
1717
} from '../utils-ts'
1818
import {defaultStateValues} from '../utils.dropdown/defaultStateValues'
19+
import {useElementIds} from '../utils.dropdown/useElementIds'
1920
import {
2021
getInitialState,
2122
defaultProps,
@@ -24,7 +25,6 @@ import {
2425
} from './utils'
2526
import downshiftUseComboboxReducer from './reducer'
2627
import * as stateChangeTypes from './stateChangeTypes'
27-
import {useElementIds} from '../utils.dropdown/useElementIds'
2828

2929
useCombobox.stateChangeTypes = stateChangeTypes
3030

@@ -87,7 +87,12 @@ function useCombobox(userProps = {}) {
8787
})
8888
// Focus the input on first render if required.
8989
useEffect(() => {
90-
const focusOnOpen = getInitialValue(props, 'isOpen', defaultStateValues)
90+
const focusOnOpen = getInitialValue(
91+
props.isOpen,
92+
props.initialIsOpen,
93+
props.defaultIsOpen,
94+
defaultStateValues.isOpen,
95+
)
9196

9297
if (focusOnOpen && inputRef.current) {
9398
inputRef.current.focus()

src/hooks/useCombobox/reducer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export default function downshiftUseComboboxReducer(state, action) {
1717
switch (type) {
1818
case stateChangeTypes.ItemClick:
1919
changes = {
20-
isOpen: getDefaultValue(props, 'isOpen', dropdownDefaultStateValues),
20+
isOpen: getDefaultValue(
21+
props.defaultIsOpen,
22+
dropdownDefaultStateValues.isOpen,
23+
),
2124
highlightedIndex: getDefaultHighlightedIndex(props),
2225
selectedItem: props.items[action.index],
2326
inputValue: props.itemToString(props.items[action.index]),

src/hooks/useMultipleSelection/reducer.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import {getDefaultValue} from './utils'
1+
import {getDefaultValue} from '../utils-ts'
2+
import {defaultStateValues} from './utils'
23
import * as stateChangeTypes from './stateChangeTypes'
34

45
/* eslint-disable complexity */
5-
export default function downshiftMultipleSelectionReducer(
6-
state,
7-
props,
8-
action,
9-
) {
10-
const {type, index, selectedItem} = action
6+
export default function downshiftMultipleSelectionReducer(state, action) {
7+
const {type, index, selectedItem, props} = action
118
const {activeIndex, selectedItems} = state
129
let changes
1310

@@ -118,8 +115,14 @@ export default function downshiftMultipleSelectionReducer(
118115
}
119116
case stateChangeTypes.FunctionReset:
120117
changes = {
121-
activeIndex: getDefaultValue(props, 'activeIndex'),
122-
selectedItems: getDefaultValue(props, 'selectedItems'),
118+
activeIndex: getDefaultValue(
119+
props.defaultActiveIndex,
120+
defaultStateValues.activeIndex,
121+
),
122+
selectedItems: getDefaultValue(
123+
props.defaultSelectedItems,
124+
defaultStateValues.selectedItems,
125+
),
123126
}
124127
break
125128
default:

src/hooks/useMultipleSelection/utils.js

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,14 @@
11
import PropTypes from 'prop-types'
22

33
import {noop} from '../../utils-ts'
4-
import {
5-
getInitialValue as getInitialValueCommon,
6-
getDefaultValue as getDefaultValueCommon,
7-
} from '../utils-ts'
4+
import {getInitialValue} from '../utils-ts'
85
import {dropdownDefaultProps, dropdownPropTypes} from '../utils.dropdown'
96

10-
const defaultStateValues = {
7+
export const defaultStateValues = {
118
activeIndex: -1,
129
selectedItems: [],
1310
}
1411

15-
/**
16-
* Returns the initial value for a state key in the following order:
17-
* 1. controlled prop, 2. initial prop, 3. default prop, 4. default
18-
* value from Downshift.
19-
*
20-
* @param {Object} props Props passed to the hook.
21-
* @param {string} propKey Props key to generate the value for.
22-
* @returns {any} The initial value for that prop.
23-
*/
24-
function getInitialValue(props, propKey) {
25-
return getInitialValueCommon(props, propKey, defaultStateValues)
26-
}
27-
28-
/**
29-
* Returns the default value for a state key in the following order:
30-
* 1. controlled prop, 2. default prop, 3. default value from Downshift.
31-
*
32-
* @param {Object} props Props passed to the hook.
33-
* @param {string} propKey Props key to generate the value for.
34-
* @returns {any} The initial value for that prop.
35-
*/
36-
function getDefaultValue(props, propKey) {
37-
return getDefaultValueCommon(props, propKey, defaultStateValues)
38-
}
39-
4012
/**
4113
* Gets the initial state based on the provided props. It uses initial, default
4214
* and controlled props related to state in order to compute the initial value.
@@ -45,8 +17,18 @@ function getDefaultValue(props, propKey) {
4517
* @returns {Object} The initial state.
4618
*/
4719
function getInitialState(props) {
48-
const activeIndex = getInitialValue(props, 'activeIndex')
49-
const selectedItems = getInitialValue(props, 'selectedItems')
20+
const activeIndex = getInitialValue(
21+
props.activeIndex,
22+
props.initialActiveIndex,
23+
props.defaultActiveIndex,
24+
defaultStateValues.activeIndex,
25+
)
26+
const selectedItems = getInitialValue(
27+
props.selectedItems,
28+
props.initialSelectedItems,
29+
props.defaultSelectedItems,
30+
defaultStateValues.selectedItems,
31+
)
5032

5133
return {
5234
activeIndex,
@@ -133,7 +115,6 @@ if (process.env.NODE_ENV !== 'production') {
133115

134116
export {
135117
validatePropTypes,
136-
getDefaultValue,
137118
getInitialState,
138119
isKeyDownOperationPermitted,
139120
isStateEqual,

src/hooks/useSelect/__tests__/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ describe('getItemIndexByCharacterKey', () => {
5959

6060
test('reducer throws error if called without proper action type', () => {
6161
expect(() => {
62-
reducer({}, {}, {type: 'super-bogus'})
62+
reducer({}, {type: 'super-bogus'})
6363
}).toThrow('Reducer called without proper action type.')
6464
})

src/hooks/useSelect/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import {
2222
useA11yMessageStatus,
2323
} from '../utils-ts'
2424
import {defaultStateValues} from '../utils.dropdown/defaultStateValues'
25+
import {useElementIds} from '../utils.dropdown/useElementIds'
2526
import {isReactNative, isReactNativeWeb} from '../../is.macro'
2627
import downshiftSelectReducer from './reducer'
2728
import {defaultProps, propTypes} from './utils'
2829
import * as stateChangeTypes from './stateChangeTypes'
29-
import { useElementIds } from '../utils.dropdown/useElementIds'
3030

3131
useSelect.stateChangeTypes = stateChangeTypes
3232

@@ -114,7 +114,12 @@ function useSelect(userProps = {}) {
114114
})
115115
// Focus the toggle button on first render if required.
116116
useEffect(() => {
117-
const focusOnOpen = getInitialValue(props, 'isOpen', defaultStateValues)
117+
const focusOnOpen = getInitialValue(
118+
props.isOpen,
119+
props.initialIsOpen,
120+
props.defaultIsOpen,
121+
defaultStateValues.isOpen,
122+
)
118123

119124
if (focusOnOpen && toggleButtonRef.current) {
120125
toggleButtonRef.current.focus()

src/hooks/useSelect/reducer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default function downshiftSelectReducer(state, action) {
1818
switch (type) {
1919
case stateChangeTypes.ItemClick:
2020
changes = {
21-
isOpen: getDefaultValue(props, 'isOpen', defaultStateValues),
21+
isOpen: getDefaultValue(props.defaultIsOpen, defaultStateValues.isOpen),
2222
highlightedIndex: getDefaultHighlightedIndex(props),
2323
selectedItem: props.items[action.index],
2424
}

0 commit comments

Comments
 (0)