score:3
Accepted answer
#1, To prevent opening if there is no input value:
<input {
...getInputProps({
...inputProps,
onKeyDown: e => {
if(!value) {
return e.nativeEvent.preventDownshiftDefault = true
}
}
})
} />
#2 Can be tricky, because if you want to modify the state, the filter
will be activated. I would suggest some layout over their input, what
displays the inputItems[highlightedIndex]
if the highlightedIndex > -1
const completeValue =
highlightedIndex > -1 ? inputItems[highlightedIndex].name : null;
return (
<div className="app">
...
{
completeValue
? <input {...getInputProps(inputProps)} value={completeValue} />
: (
<input
{...getInputProps({
...inputProps,
onKeyDown: e => {
if (!value) {
return (e.nativeEvent.preventDownshiftDefault = true);
}
}
})}
/>
)
}
...
</div>
)
#3, To close the recommendations box:
const {
isOpen,
getComboboxProps,
getInputProps,
getMenuProps,
getItemProps,
highlightedIndex,
closeMenu, // <= use this inbuilt functionality
selectItem
} = useCombobox({
And at the button click just call it by manually:
<button
className="btn btn-clear"
onClick={() => {
selectItem(null);
setValue("");
closeMenu();
}}
>
Clear
</button>
Source: stackoverflow.com
Related Query
- Downshift: The menu should close until results not found
- Is there a way not to close the antd dropdown menu by clicking on it?
- The action menu is not getting closed even if I click somewhere on page, the menu remains open until I explicitly click on the Action button again
- Sidebar menu should NOT get collapse/close when i click outside the body
- ANTD Dropdown should not close when clicked on the Input field
- How do I update the state (using ReactJS) if I should not call setState in componentWillUpdate?
- Warning: You should not use <Route component> and <Route render> in the same route; <Route render> will be ignored
- "RNCSafeAreaView" was not found in the UIManager
- requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager when running android app
- Where should I store my JWT in 2019 and is the localStorage really not secure?
- Invariant Violation: requireNativeComponent: "FastImageView" was not found in the UIManager in react native
- getNode() method not found in gatsby-node.js with the latest version of Gatsby
- Cannot resolve module "firebase" from 'firebase.js' : Firebase could not be found within the project
- 'prop-types' should be listed in the project's dependencies, not devDependencies
- reactjs Route Not Found The requested URL
- Should I put RTK Query results into the store?
- material-ui menu not close when dialog open
- Is it a bad practice to use state in a React component if the data will not change? Should I use a property on the class instead?
- ReactStrap modal close icon is not getting show in the modal and How to use the header with some another tag
- React.js page not found on refresh or on click on the link directly
- Test of reducer returns the results which are not equal to expected
- how to solve the error that fs module is not found when used react and next.js
- I'm trying to close the offcanvas menu in React Bootstrap when I click a link
- How to automatically close react-select menu once the last item has been selected? (empty list)
- State not updating until 2nd time the form is submitted
- Regex for checking if a string should not contain certain words but those words are ok if they have a 'to' or 'for' preceeding the words
- React - Close dropdown menu when clicking on the window
- React native-env file could not be found within the project
- Webpack in React can't load the 3D model with a GLTF extension, shows 404 not found
- Why does my react app go to the custom 404 not found page when the link is clicked?
More Query from same tag
- react js send object to node js
- Can we add Html id to React code and make a condition
- why two routes calling same page in react js that has different path link
- How to put data in fields when updating?
- react router render component multiple times when refreshing
- How can I pass values from a functional component through a React Router Link to another functional component?
- Cannot post message on cross origin iframe
- React Native - how can I pass a function to a stack screen in a navigator?
- Client side state is not hydrated from server on page reload
- Rails not rendering public/index.html file; blank page in browser
- React On State Change, Change style
- How to solve Allow access origion problem
- How are boolean props used in React?
- Touching fields causes react-hook-form to become dirty
- React-dnd state not changing
- How to render a FontAwesome icon from a json file into a React Application?
- React native - pseudo elements equivalent
- ReactJS: how to trigger form submit event from another event
- How to pass children and type to classnames library in Nextjs
- calling React function-based component from vanilla JavaScript DOM
- What is wrong with my approach to redux-thunk dispatch? (React, typescript, TS-2345)
- React Redux - Pass data from state to another component to display list of results
- How can I empty the field of the form after submit?
- Async/await React: objects are not valid, [object promise]
- Component rendering before props are updated - React Redux
- Marmelab react-admin x-total-count how to add new headers
- Property 'target' does not exist on type error using {...props}
- How to resolve post request failure ReactJS and ExpressJS
- Gatsby How do I reset the app on a site reload
- Why can't I fetch my Metamask account address with Next.js's getInitialProps?