@@ -9,6 +9,13 @@ import React, {
99} from 'react' ;
1010
1111import TypeaheadManager from './TypeaheadManager' ;
12+ import {
13+ clearTypeahead ,
14+ clickOrFocusInput ,
15+ getInitialState ,
16+ hideMenu ,
17+ toggleMenu ,
18+ } from './TypeaheadState' ;
1219
1320import {
1421 caseSensitiveType ,
@@ -208,78 +215,6 @@ const defaultProps = {
208215
209216type 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
0 commit comments