简体   繁体   English

React:使用 MUI 表单自动完成 MUI

[英]React: MUI autocomplete with MUI form

I've been using a MUI form like this:我一直在使用这样的MUI表单:

<Box component="form" onSubmit={event => {
    return handleSubmit(event);
}} noValidate sx={{mt: 1}}>
    <TextField
        margin="normal"
        required
        fullWidth
        id="title"
        label="Titel"
        name="title"
        autoFocus
    />
    <TextField
        margin="normal"
        required
        multiline
        rows={10}
        fullWidth
        label="Inhalt"
        name="content"
        id="content"
        autoComplete="off"
    />
</Box>

This allowed me to extract the values from the MUI TextField components by using FormData :这使我可以使用FormDataMUI TextField组件中提取值:

const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    const data = new FormData(event.currentTarget);
    let newsResponse = await createNews({
        title: data.get('title'),
        content: data.get('content'),
    });
}

This works fine.这很好用。 Now I wanted to add a MUI Autocomplete component to the form:现在我想在表单中添加一个MUI Autocomplete组件:

<Autocomplete
    multiple
    id="tags"
    className={props.className}
    open={open}
    isOptionEqualToValue={(option, value) => option.name === value.name}
    getOptionLabel={(option) => option.name}
    options={tags}
    renderInput={(params) => (
        <TextField
            {...params}
            label="Tags"
            required
            InputProps={{
                ...params.InputProps,
                endAdornment: (
                    <React.Fragment>
                        {loading ? <CircularProgress color="inherit" size={20}/> : null}
                        {params.InputProps.endAdornment}
                    </React.Fragment>
                ),
            }}
        />
    )}
/>

However, I found no way to access the value of said Autocomplete component.但是,我发现无法访问所述Autocomplete组件的值。 It does not even have a name attribute and adding a name attribute to the inner TextField component does not work either.它甚至没有name属性,向内部TextField组件添加name属性也不起作用。

How can I access the value of it in manner like data.get('tags') ?如何以data.get('tags')之类的方式访问它的值? Considering that both are MUI components I would expect them to have the same API.考虑到两者都是MUI组件,我希望它们具有相同的 API。

The useState hook, something like this: useState 钩子,像这样:

function MyForm() {
  const [values, setValues] = useState('');
  const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    console.log(values);
  };

  return (
    <form onSubmit={handleSubmit}>
      <Autocomplete
        multiple
        id="tags"
        className={props.className}
        open={open}
        isOptionEqualToValue={(option, value) => option.name === value.name}
        getOptionLabel={(option) => option.name}
        options={tags}
        onChange={(event: any, newValues: string[] | null) => {
          setValues(newValues || '');
        }}
        renderInput={(params) => (
          <TextField
            {...params}
            label="Tags"
            required
            InputProps={{
              ...params.InputProps,
              endAdornment: (
                <>
                    {loading ? <CircularProgress color="inherit" size={20}/> : null}
                    {params.InputProps.endAdornment}
                </>
              ),
            }}
          />
        )}
      />
      <button type="submit">Submit</button>
    </form>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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