简体   繁体   English

重置通知状态时,材料 UI 自动完成组件不会清除输入

[英]Material UI Autocomplete component won't clear input when Informed state is reset

I'm using Material UI version 4 (the latest), and the Informed form library .我正在使用 Material UI 版本 4(最新版本)和Informed form library I have a custom component (custom to integrate with Informed) that wraps the Material UI TextField which I'm rendering using the Autocomplete component.我有一个自定义组件(自定义与 Informed 集成),它包装了我使用 Autocomplete 组件渲染的 Material UI TextField。

App component应用组件

    <Form getApi={(api) => setFormApi(api)}>
      {formApi && (
        <>
          <label>
            First name:
            <Autocomplete
              freeSolo
              options={autoOptions}
              renderInput={(params) => (
                <CustomTextField field="name" {...params} />
              )}
            />
          </label>
          <button type="submit">Submit</button>
          <button type="button" onClick={() => formApi.reset()}>
            Reset
          </button>
          <FormState />
        </>
      )}
    </Form>

The issue问题

When the reset button is clicked you can see the Informed "form state" is cleared, but the input still has a value.单击重置按钮时,您可以看到 Informed “表单状态”已清除,但输入仍具有值。 Any ideas on how to solve this?关于如何解决这个问题的任何想法?

Example - Codesandbox 示例 - 代码沙盒

The inputProps are getting overriden by the ones provided by Autocomplete component, change the order you pass ...rest props and included the ...rest.inputProps in your custom inputProps with the correct value inputProps正在被Autocomplete组件提供的那些覆盖,更改您传递的顺序...rest props 并将...rest.inputProps包含在具有正确值的自定义inputProps

      <TextField
        {...rest} // should go first to allow overriding
        // only add value props for select fields
        // value={value}
        onChange={(event) => {
          setValue(event.target.value);
          if (onChange) {
            onChange(event);
          }
        }}
        onBlur={(event) => {
          setTouched(true);
          if (onBlur) {
            onBlur(event);
          }
        }}
        error={!!error}
        helperText={error ? error : helperText ? helperText : false}
        variant="outlined"
        margin="none"
        fullWidth
        inputProps={{
          ...rest.inputProps, // must include otherwise it breaks
          value:
            !select && !maskedValue && maskedValue !== 0 ? "" : maskedValue,
          maxLength: maxLength || undefined
        }}
        // eslint-disable-next-line
        InputProps={{
          style: sensitive && {
            color: "rgba(0,0,0,0)",
            caretColor: "#000"
          },
          startAdornment
        }}
        InputLabelProps={{
          shrink: true
        }}
        autoComplete="off"
        disabled={disabled}
      />

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

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