简体   繁体   中英

How to change selected value of React-Bootstrap-Typeahead from HOC

I have two Typeahead (react-bootstrap-typeahead) component and I want to reverse selected values when pressed a button. The data which stored in state changing correctly. But I can't set changed selections to a value of Typeahead.

Used selected prop to set selected object but got some errors:

Warning: [react-bootstrap-typeahead] `defaultInputValue` will be overridden by the value from `selected`.

Warning: Failed prop type: Invalid prop `selected` supplied to `TypeaheadContainer(WrappedTypeahead)`.

My HOC

<TypeaheadComponent
    onSearch={value => getOriginPlaces(value, index)}
    data={route.originResults}
    onInputChange={value => originPlaceInputOnChange(value, index)}
    onSelect={item => originPlaceOnSelect(item, index)}
    selected={route.originPlace} // object
    inputValue={route.originPlaceInputValue} // string from  originPlaceInputOnChange()
/>

And AsyncTypeahead (react-bootstrap-typeahead)

<AsyncTypeahead
    promptText={<PromptText />}
    searchText={<Loading />}
    emptyLabel={<NotFound />}
    inputProps={{...}}
    options={props.data}
    maxResults={10}
    paginationText={...}
    labelKey="name"
    filterBy={[...]}
    onSearch={props.onSearch}
    minLength={2}
    selectHintOnEnter={true}
    onInputChange={props.onInputChange}
    onChange={selected => props.onSelect(selected[0])} // set first object of array
    defaultInputValue={props.inputValue || ''}
    selected={props.selected} // object
    renderMenu={...}
/>

How can I do this? Or where I am wrong

Your main problem is that selected needs to be an array, not an object. Try passing an array containing the selection (eg: [props.selected] ) to the AsyncTypeahead. That should work.

For the second warning, defaultInputValue will set the input value only when the typeahead initially mounts (not subsequent renders), and any input value derived from selected will take precedence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM