Skip to content

Commit 81ec599

Browse files
committed
Move state modifiers to separate file
1 parent 228d312 commit 81ec599

3 files changed

Lines changed: 85 additions & 74 deletions

File tree

src/core/Typeahead.tsx

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import React, {
99
} from 'react';
1010

1111
import TypeaheadManager from './TypeaheadManager';
12+
import {
13+
clearTypeahead,
14+
clickOrFocusInput,
15+
getInitialState,
16+
hideMenu,
17+
toggleMenu,
18+
} from './TypeaheadState';
1219

1320
import {
1421
caseSensitiveType,
@@ -208,78 +215,6 @@ const defaultProps = {
208215

209216
type Props = TypeaheadProps;
210217

211-
export function getInitialState(props: Props): TypeaheadState {
212-
const {
213-
defaultInputValue,
214-
defaultOpen,
215-
defaultSelected,
216-
maxResults,
217-
multiple,
218-
} = props;
219-
220-
let selected = props.selected
221-
? props.selected.slice()
222-
: defaultSelected.slice();
223-
224-
let text = defaultInputValue;
225-
226-
if (!multiple && selected.length) {
227-
// Set the text if an initial selection is passed in.
228-
text = getOptionLabel(selected[0], props.labelKey);
229-
230-
if (selected.length > 1) {
231-
// Limit to 1 selection in single-select mode.
232-
selected = selected.slice(0, 1);
233-
}
234-
}
235-
236-
return {
237-
activeIndex: -1,
238-
activeItem: undefined,
239-
initialItem: undefined,
240-
isFocused: false,
241-
selected,
242-
showMenu: defaultOpen,
243-
shownResults: maxResults,
244-
text,
245-
};
246-
}
247-
248-
export function clearTypeahead(state: TypeaheadState, props: Props) {
249-
return {
250-
...getInitialState(props),
251-
isFocused: state.isFocused,
252-
selected: [],
253-
text: '',
254-
};
255-
}
256-
257-
export function clickOrFocusInput(state: TypeaheadState) {
258-
return {
259-
...state,
260-
isFocused: true,
261-
showMenu: true,
262-
};
263-
}
264-
265-
export function hideMenu(state: TypeaheadState, props: Props) {
266-
const { activeIndex, activeItem, initialItem, shownResults } =
267-
getInitialState(props);
268-
269-
return {
270-
...state,
271-
activeIndex,
272-
activeItem,
273-
initialItem,
274-
showMenu: false,
275-
shownResults,
276-
};
277-
}
278-
279-
export function toggleMenu(state: TypeaheadState, props: Props) {
280-
return state.showMenu ? hideMenu(state, props) : { ...state, showMenu: true };
281-
}
282-
283218
/**
284219
* Manually trigger the input's change event.
285220
* https://stackoverflow.com/questions/23892547/what-is-the-best-way-to-trigger-onchange-event-in-react-js/46012210#46012210

src/components/Typeahead/TypeaheadState.test.tsx renamed to src/core/TypeaheadState.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
getInitialState,
55
hideMenu,
66
toggleMenu,
7-
} from '../../core/Typeahead';
7+
} from './TypeaheadState';
88

9-
import { defaultProps, defaultState } from '../../tests/data';
9+
import { defaultProps, defaultState } from '../tests/data';
1010

1111
describe('State modifiers', () => {
1212
it('calls the clearTypeahead modifier', () => {

src/core/TypeaheadState.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { TypeaheadProps, TypeaheadState } from '../types';
2+
import { getOptionLabel } from '../utils';
3+
4+
type Props = TypeaheadProps;
5+
6+
export function getInitialState(props: Props): TypeaheadState {
7+
const {
8+
defaultInputValue,
9+
defaultOpen,
10+
defaultSelected,
11+
maxResults,
12+
multiple,
13+
} = props;
14+
15+
let selected = props.selected
16+
? props.selected.slice()
17+
: defaultSelected.slice();
18+
19+
let text = defaultInputValue;
20+
21+
if (!multiple && selected.length) {
22+
// Set the text if an initial selection is passed in.
23+
text = getOptionLabel(selected[0], props.labelKey);
24+
25+
if (selected.length > 1) {
26+
// Limit to 1 selection in single-select mode.
27+
selected = selected.slice(0, 1);
28+
}
29+
}
30+
31+
return {
32+
activeIndex: -1,
33+
activeItem: undefined,
34+
initialItem: undefined,
35+
isFocused: false,
36+
selected,
37+
showMenu: defaultOpen,
38+
shownResults: maxResults,
39+
text,
40+
};
41+
}
42+
43+
export function clearTypeahead(state: TypeaheadState, props: Props) {
44+
return {
45+
...getInitialState(props),
46+
isFocused: state.isFocused,
47+
selected: [],
48+
text: '',
49+
};
50+
}
51+
52+
export function clickOrFocusInput(state: TypeaheadState) {
53+
return {
54+
...state,
55+
isFocused: true,
56+
showMenu: true,
57+
};
58+
}
59+
60+
export function hideMenu(state: TypeaheadState, props: Props) {
61+
const { activeIndex, activeItem, initialItem, shownResults } =
62+
getInitialState(props);
63+
64+
return {
65+
...state,
66+
activeIndex,
67+
activeItem,
68+
initialItem,
69+
showMenu: false,
70+
shownResults,
71+
};
72+
}
73+
74+
export function toggleMenu(state: TypeaheadState, props: Props) {
75+
return state.showMenu ? hideMenu(state, props) : { ...state, showMenu: true };
76+
}

0 commit comments

Comments
 (0)