【问题标题】:MaterialUI Compoent does not AutoComplete when data is available数据可用时,材质 UI 组件不会自动完成
【发布时间】:2020-08-06 13:47:20
【问题描述】:

我无法让我的组件显示我的自动建议。 在控制台中观察到我的数据可用,我使用建议道具将其发送到此组件,使用 Material UI AutoComplete 组件功能here 我正在尝试设置我的选项,这些在我输入时会发生变化在父组件中,但设置值似乎并没有反映也没有提出我的建议。我很迷茫。我的代码如下。

import React, { FunctionComponent, FormEvent, ChangeEvent } from "react";
import { Grid, TextField, Typography } from "@material-ui/core";
import { CreateProjectModel, JobModel } from "~/Models/Projects";
import ErrorModel from "~/Models/ErrorModel";
import Autocomplete from "@material-ui/lab/Autocomplete";

type CreateProjectFormProps = {
    model: CreateProjectModel;
    errors: ErrorModel<CreateProjectModel>;
    onChange: (changes: Partial<CreateProjectModel>) => void;
    onSubmit?: () => Promise<void>;
    suggestions: JobModel[];
};

const CreateProjectForm: FunctionComponent<CreateProjectFormProps> = ({
    model,
    errors,
    onChange,
    onSubmit,
    suggestions,
}) => {
    const [open, setOpen] = React.useState(false);
    const [options, setOptions] = React.useState<JobModel[]>([]);
    const loading = open && options.length === 0;
    const [inputValue, setInputValue] = React.useState('');
    React.useEffect(() => {
        let active = true;

        if (!loading) {
            return undefined;
        }

        (async () => {
            if (active) {
                setOptions(suggestions);
            }
        })();

        return () => {
            active = false;
        };
    }, [loading]);

    React.useEffect(() => {
        if (!open) {
            setOptions([]);
        }
    }, [open]);

    const submit = async (event: FormEvent) => {
        event.preventDefault();
        event.stopPropagation();

        await onSubmit();
    };

    const change = (name: string) => (event: ChangeEvent<HTMLInputElement>) => {
        setInputValue(event.target.value);

        onChange({
            [name]: event.target.value,
        });
    };

    const getFieldProps = (id: string, label: string) => {
        return {
            id,
            label,
            helperText: errors[id],
            error: Boolean(errors[id]),
            value: model[id],
            onChange: change(id),
        };
    };

    return (
        <Autocomplete
            {...getFieldProps}
            open={open}
            onOpen={() => {
                setOpen(true);
            }}
            onClose={() => {
                setOpen(false);
            }}
            getOptionSelected={(option, value) => option.id === value.id}
            getOptionLabel={(option) => option.id}
            options={options}
            loading={loading}
            autoComplete
            includeInputInList            
            renderInput={(params) => (
                <TextField
                    {...getFieldProps("jobNumber", "Job number")}
                    required
                    fullWidth
                    autoFocus
                    margin="normal"
                />
            )}
            renderOption={(option) => {        
                return (
                  <Grid container alignItems="center">

                    <Grid item xs>
                      {options.map((part, index) => (
                        <span key={index}>
                          {part.id}
                        </span>
                      ))}
                      <Typography variant="body2" color="textSecondary">
                        {option.name}
                      </Typography>
                    </Grid>
                  </Grid>
                );
              }}            
        />
    );
};

export default CreateProjectForm;

建议中的数据示例如下所示:

[{"id":"BR00001","name":"Aircrew - Standby at home base"},{"id":"BR00695","name":"National Waste"},{"id":"BR00777B","name":"Airly Monitor Site 2018"},{"id":"BR00852A","name":"Cracow Mine"},{"id":"BR00972","name":"Toowoomba Updated"},{"id":"BR01023A","name":"TMRGT Mackay Bee Creek"},{"id":"BR01081","name":"Newman Pilot Job (WA)"},{"id":"BR01147","name":"Lake Vermont Monthly 2019"},{"id":"BR01158","name":"Callide Mine Monthly Survey 2019"},{"id":"BR01182","name":"Lake Vermont Quarterly 2019 April"}]

【问题讨论】:

  • 嗨,院长,用户输入是否应该从您的建议中的 name 属性中提供相关匹配项,还是仅提供 id?例如。如果我输入“lake”,你想显示 BRO1182, Lake Vermont Quarterly 2019 April 作为匹配项吗?
  • 第二季度。加载状态的原因是什么? Q3。您提供的代码中未使用提交;为什么会在那里?

标签: javascript reactjs typescript material-ui


【解决方案1】:

您的代码中的问题是您使用的 useEffects。

在下面的 useEffect 中,您实际上是在最初将选项设置为一个空数组。那是因为您的自动完成功能未打开,并且效果也在初始安装时运行。此外,由于您在另一个 useEffect 中设置选项,因此您的代码应该工作的唯一时间是在加载状态更新并且您尚未打开自动完成下拉菜单时。

即使关闭一次,状态也会更新为空,您将不会再看到建议。

React.useEffect(() => {
    if (!open) {
        setOptions([]);
    }
}, [open]);

解决方案很简单。您不需要为选项保留本地状态,而是使用来自道具的值 suggestions

你只需要保持一个打开状态

const CreateProjectForm: FunctionComponent<CreateProjectFormProps> = ({
    model,
    errors,
    onChange,
    onSubmit,
    suggestions,
}) => {
    const [open, setOpen] = React.useState(false);
    const loading = open && suggestions.length === 0;
    const [inputValue, setInputValue] = React.useState('');

    const submit = async (event: FormEvent) => {
        event.preventDefault();
        event.stopPropagation();

        await onSubmit();
    };

    const change = (name: string) => (event: ChangeEvent<HTMLInputElement>) => {
        setInputValue(event.target.value);

        onChange({
            [name]: event.target.value,
        });
    };

    const getFieldProps = (id: string, label: string) => {
        return {
            id,
            label,
            helperText: errors[id],
            error: Boolean(errors[id]),
            value: model[id],
            onChange: change(id),
        };
    };

    return (
        <Autocomplete
            {...getFieldProps}
            open={open}
            onOpen={() => {
                setOpen(true);
            }}
            onClose={() => {
                setOpen(false);
            }}
            getOptionSelected={(option, value) => option.id === value.id}
            getOptionLabel={(option) => option.id}
            options={suggestions}
            loading={loading}
            autoComplete
            includeInputInList            
            renderInput={(params) => (
                <TextField
                    {...getFieldProps("jobNumber", "Job number")}
                    required
                    fullWidth
                    autoFocus
                    margin="normal"
                />
            )}
            renderOption={(option) => {        
                return (
                  <Grid container alignItems="center">

                    <Grid item xs>
                      {options.map((part, index) => (
                        <span key={index}>
                          {part.id}
                        </span>
                      ))}
                      <Typography variant="body2" color="textSecondary">
                        {option.name}
                      </Typography>
                    </Grid>
                  </Grid>
                );
              }}            
        />
    );
};

export default CreateProjectForm;

【讨论】:

    【解决方案2】:

    我注意到您的代码存在一些问题,getFieldProps 在没有 id 或 name 参数的情况下被调用,导致页面无法加载。更重要的是,在传递和使用道具时,您应该考虑关注autocomplete docs。例如:

    renderInput={(params) => <TextField {...params} label="Controllable" variant="outlined" />}
    

    我问了几个问题,请告诉我你什么时候能得到这些答案,这样我就可以解决所有可能出现的问题。

    第一季度。用户输入应该从您的建议中的 name 属性中提供相关匹配项还是仅提供 id?例如。如果我输入“lake”,你想显示 BRO1182, Lake Vermont Quarterly 2019 April 作为匹配项吗?

    第二季度。您想如何解决错误情况?我看到您有一个错误模型,但不确定您希望如何在发生错误时使用它来设置自动完成样式

    第三季度。我们缺少提交按钮吗?我看到了onSubmit 函数,但它没有在我们的代码中使用。

    第四季度。您需要打开和加载状态有什么特别的原因吗?

    以下是迄今为止我尝试显示来自用户输入的相关匹配项

    import React, { FunctionComponent, FormEvent, ChangeEvent } from "react";
    import { Grid, TextField, Typography } from "@material-ui/core";
    import { CreateProjectModel, JobModel } from "~/Models/Projects";
    import ErrorModel from "~/Models/ErrorModel";
    import Autocomplete from "@material-ui/lab/Autocomplete";
    
    type CreateProjectFormProps = {
      model: CreateProjectModel;
      errors: ErrorModel<CreateProjectModel>;
      onChange: (changes: Partial<CreateProjectModel>) => void;
      onSubmit?: () => Promise<void>;
      suggestions: JobModel[];
    };
    
    const CreateProjectForm: FunctionComponent<CreateProjectFormProps> = ({
      model,
      errors,
      // mock function for testing
      // consider a better name like selectChangeHandler
      onChange = val => console.log(val),
      // consider a better name like submitJobFormHandler
      onSubmit,
      suggestions: options = [
        { id: "BR00001", name: "Aircrew - Standby at home base" },
        { id: "BR00695", name: "National Waste" },
        { id: "BR00777B", name: "Airly Monitor Site 2018" },
        { id: "BR00852A", name: "Cracow Mine" },
        { id: "BR00972", name: "Toowoomba Updated" },
        { id: "BR01023A", name: "TMRGT Mackay Bee Creek" },
        { id: "BR01081", name: "Newman Pilot Job (WA)" },
        { id: "BR01147", name: "Lake Vermont Monthly 2019" },
        { id: "BR01158", name: "Callide Mine Monthly Survey 2019" },
        { id: "BR01182", name: "Lake Vermont Quarterly 2019 April" }
      ]
    }) => {
      const [value, setValue] = React.useState<JobModel>({});
      const loading = open && options.length === 0;
    
      // this pc of code is not used, why?
      const submit = async (event: FormEvent) => {
        event.preventDefault();
        event.stopPropagation();
    
        await onSubmit();
      };
    
      const handleChange = (_: any, value: JobModel | null) => {
        setValue(value);
    
        onChange({
          [value.name]: value.id
        });
      };
    
      // consider passing in props instead
      const getFieldProps = (id: string, label: string) => {
        return {
          id,
          label,
          // not sure what this is
          helperText: errors[id],
          // not sure what this is
          error: Boolean(errors[id]),
          value: model[id],
          onChange: change(id)
        };
      };
    
      return (
        <Autocomplete
          id="placeholder-autocomplete-input-id"
          // for selection, use value see docs for more detail
          value={value}
          onChange={handleChange}
          getOptionSelected={(option, value) => option.id === value.id}
          getOptionLabel={option => option.id}
          options={options}
          loading={loading}
          autoComplete
          includeInputInList
          renderInput={params => (
            // spreading the params here will transfer native input attributes from autocomplete
            <TextField
              {...params}
              label="placeholder"
              required
              fullWidth
              autoFocus
              margin="normal"
            />
          )}
          renderOption={option => (
            <Grid container alignItems="center">
              <Grid item xs>
                <span key={option}>{option.id}</span>
                <Typography variant="body2" color="textSecondary">
                  {option.name}
                </Typography>
              </Grid>
            </Grid>
          )}
        />
      );
    };
    
    export default CreateProjectForm;
    

    您可以通过点击下面的按钮查看在我的代码沙箱中运行的代码

    【讨论】:

      【解决方案3】:

      如果我理解你的代码并且问题正确,你想要 -


          React.useEffect(() => {
            let active = true;
      
            if (!loading) {
              return undefined;
            }
      
            (async () => {
               if (active) {
                   setOptions(suggestions);
                }
             })(); 
      
            return () => {
               active = false;
            };
         }, [loading]);
      

      每次运行并更新options,但问题是,[loading] 依赖设置为


           const loading = open && suggestions.length === 0;
      

      并且不会触发更改。

      考虑这样做 -


       const loading = useLoading({open, suggestions})
      
      
       const useLoading = ({open, suggestions}) => open && suggestions.length === 0;
      

      【讨论】:

        猜你喜欢
        • 2020-04-09
        • 2020-08-04
        • 1970-01-01
        • 2021-12-18
        • 2021-04-27
        • 2019-11-25
        • 2021-11-10
        • 2021-05-19
        • 2020-03-19
        相关资源
        最近更新 更多