Home > Mobile >  How can I get the input selected on a Autocomplete field when onKeyPress
How can I get the input selected on a Autocomplete field when onKeyPress

Time:12-15

How do I get the value of the autocomplete field when the user press the "Enter" key ?

Here's the code for the Autocomplete :

<Autocomplete
              freeSolo
              id="streetName"
              getOptionLabel={(option) => option.name}
              options={streetsName}
              onChange={(event, newValue) => {
                if (newValue !== null) {
                  setSelectedStreetName({
                    generique: newValue.generique,
                    specifique: newValue.specifique,
                    particule: newValue.particule,
                  });
                }
              }}
              onKeyDown={(e, value) => {
                if (e.key === 'Enter') handleClick(value);
              }}
              renderInput={(params) => (
                <TextField
                  {...params}
                  error={error}
                  fullWidth
                  label="Rue"
                  required
                  size="small"
                  variant="outlined"
                />
              )}
            />

The value doesn't hold anything here!

Thank you !

CodePudding user response:

<Autocomplete
              onKeyDown={(e) => {
                const code = e.keyCode || e.which;

                if(code === 13) { // or e.key === 'Enter'
                  handleClick(e.target.value);
                }
              }}
              ...
/>

CodePudding user response:

This is likely available at:

              onKeyDown={(e) => {
                if (e.key === 'Enter') handleClick(e.target.value);
              }}
  • Related