Comments (7)
Hi @atandy
That functionality is controlled by restoreOnBlurIfEmpty
on the Autocomplete
component which is true by default. If you set it to false
, it will clear itself when you delete everything in the input element.
For the formik
example, you bring up a good point. If I specify one
as the initial value like the demo does and then use formik.resetForm()
on a button click the display doesn't seem to change back to one. If you submit the form, the value is one
, but what is displayed on the form itself doesn't reflect that.
from choc-autocomplete.
Below is an example formik component I was able to get working. Does this do what you're expecting?
import {
Box,
Button,
Center,
FormControl,
FormHelperText,
FormLabel,
Heading,
} from "@chakra-ui/react";
import * as React from "react";
import {
AutoComplete,
AutoCompleteInput,
AutoCompleteItem,
AutoCompleteList,
AutoCompleteGroup,
} from "@choc-ui/chakra-autocomplete";
import { Formik } from "formik";
const options = [
{ label: "apple", value: "one" },
{ label: "appoint", value: "two" },
{ label: "zap", value: "three" },
{ label: "cap", value: "four" },
{ label: "japan", value: "five" },
];
function FormikExample() {
const name = "team";
const onSubmit = data => console.log("data from form", data);
return (
<Box border="1px" borderRadius="1em" p={2}>
<Heading as="h6" align="center">
Formik
</Heading>
<Formik onSubmit={onSubmit} initialValues={{ team: "one"}}>
{({ handleSubmit, handleBlur, setFieldValue, ...other }) => (
<>
<Button onClick={() => setFieldValue("team", "four")}>
Change Value To four
</Button>
<Button onClick={() => other.resetForm() }>Reset Form</Button>
<form onSubmit={handleSubmit}>
<FormControl>
<FormLabel>Olympics Soccer Winner</FormLabel>
<AutoComplete
onChange={val => setFieldValue("team", val)}
openOnFocus
restoreOnBlurIfEmpty={false}
value={other.values.team}
>
<AutoCompleteInput
variant="filled"
name={name}
onBlur={handleBlur}
/>
<AutoCompleteList>
<AutoCompleteGroup>
{options.map(option => (
<AutoCompleteItem
key={`option-${option.value}`}
value={{
title: `${option.value}`,
}}
label={option.label}
textTransform="capitalize"
/>
))}
</AutoCompleteGroup>
</AutoCompleteList>
</AutoComplete>
<FormHelperText>Who do you support.</FormHelperText>
</FormControl>
<Center>
<Button type="submit">Submit</Button>
</Center>
</form>
</>
)}
</Formik>
</Box>
);
}
export default FormikExample;
from choc-autocomplete.
im actually trying to do a similar thing - im try to use this autocomplete as a way to select an option without actually using it as input, meaning after the user selects something i add his selection to a state, and then i want to reset the autocomplete element so the user can make another selection.
when i do
const onSelect = (params: {
item: Item;
selectMethod: "mouse" | "keyboard" | null;
isNewInput: boolean;
}) => {
// add selection to a state...
autoCompleteRef.current?.resetItems();
autoCompleteRef.current?.removeItem(params.item);
};
nothing happens, and the user input stays selected..
from choc-autocomplete.
Hi @arieb . I see what you mean. I was able to write up a quick demo on my end and will work on this.
from choc-autocomplete.
To try and give some more detail to what the issue is, in a non-controlled state the resetItems
and removeItem
methods on the passed to the AutoComplete
component don't seem to properly reset the input element's value. If you are using a controlled component (passing a value prop to AutoComplete
), this behavior works as expected without needing the resetItems
and removeItem
methods.
Hopefully a simple boiled down version:
- While passing value prop, resetting the value to an empty string after selecting something will correctly show nothing in the input
- Without passing value prop, resetting the component using
resetItems
andremoveItem
will incorrectly show the last selected value in the input
from choc-autocomplete.
A very simple repro of this
function App() {
const autoCompleteRef = React.useRef<AutoCompleteRefMethods>();
const onChange = React.useCallback((value: any) => {
// Do something else with value, then reset
if(value) {
autoCompleteRef.current?.resetItems();
autoCompleteRef.current?.removeItem(value);
}
}, []);
return (
<AutoComplete ref={autoCompleteRef} onChange={onChange} openOnFocus>
<AutoCompleteInput variant="filled" w="48" />
<AutoCompleteList>
{options.map(option => (
<AutoCompleteItem
key={`option-${option.value}`}
value={{ title: `${option.value}` }}
label={option.label}
/>
))}
</AutoCompleteList>
</AutoComplete>
);
}
from choc-autocomplete.
This should be resolved in v5.3.1. If you continue to have any problems, let me know and I'll re-open this issue or start a new one.
from choc-autocomplete.
Related Issues (20)
- Update package.json for types declaration ? HOT 1
- sslint: cannot find module umd.js HOT 5
- Why has react-nanny become a peer-dependency? HOT 1
- Contribution instructions should use npm HOT 1
- Reset items after submission HOT 5
- tabbing out of autocomplete input selects the focused item in multiple mode HOT 2
- Filling the component programmatically HOT 2
- Last selected item in multi-select autocomplete prefills input on external click HOT 4
- The `value` prop does nothing HOT 2
- bug: setting AutocompleteItems dynamically via state does not trigger a re-render, only after refocus rendered HOT 2
- Pressing enter when the autocomplete text box is closed selects the first entry, even when freeSolo is true HOT 2
- Named export error HOT 7
- Weird query behavior HOT 1
- How to remove Pop Over "No Results Found" HOT 4
- Help with custom filter HOT 1
- Autocomplete is showing a match if the 7 last char are equal even if the "x" other are not HOT 4
- Expose Chakra Popover 'matchWidth' prop HOT 1
- Keyboard navigation causes window to scroll in some configurations HOT 3
- Styling of 'No options found!' message HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from choc-autocomplete.