简体   繁体   English

如果通过钩子签入状态,如何访问自动完成的复选框和存储标题?

[英]How can I access the Autocomplete's checked boxes and store title if checked in state via hook?

I am using the React Material-UI Autocomplete, which contains checkboxes (here is code so far, see demo.js): https://codesandbox.io/s/material-demo-rxbhz?fontsize=14&hidenavigation=1&theme=dark我正在使用 React Material-UI Autocomplete,其中包含复选框(目前为止的代码,请参阅 demo.js): https ://codesandbox.io/s/material-demo-rxbhz ? fontsize =14& hidenavigation =1& theme = dark

I would like store all of the food names that are checked into a hook, like so:我想存储所有检查到钩子的食物名称,如下所示:

const [item, setItem] = React.useState([{title: "Food_Item_1"}, {title: "Food_Item_2"), ...]); const [item, setItem] = React.useState([{title: "Food_Item_1"}, {title: "Food_Item_2"), ...]);

The code currently does not save the checked food names into the item const.该代码目前不会将检查的食物名称保存到项目 const 中。 I need these in a usable format to filter out a list of food reviews (in the data const) by putting into filteredData only the reviews that have the title matching one of the food names in the hook, and rendering the filteredData.我需要这些以可用格式来过滤食物评论列表(在数据常量中),方法是将标题与钩子中的一个食物名称匹配的评论放入过滤数据中,并呈现过滤数据。 The filteredData function seems to be working, but I also need help adapting it to use the hook data (ie right now it only checks one item, how do I make it check against everything in item)? FilteredData 函数似乎正在工作,但我还需要帮助调整它以使用钩子数据(即现在它只检查一个项目,我如何让它检查项目中的所有内容)?

Thank you.谢谢你。

To store data like:存储数据,如:

const [item, setItem] = React.useState([{title: "Food_Item_1"}, {title: "Food_Item_2"), ...]);

you need these below changes:您需要以下更改:

export default function CheckboxesTags() {
  const [item, setItem] = React.useState([]);

  const handleChange = (evt, options) => {
    evt.persist();
    const lastAddedItem = options[options.length -1];
    setItem([...item, {title: lastAddedItem.item}]);
  };


  const filteredData = data.filter(menuitem => {
    return menuitem.title === item.title;
  });

  return (
    <Autocomplete
      multiple
      id="checkboxes-tags-demo"
      options={MENU}
      disableCloseOnSelect
      getOptionLabel={option => option.item}
      renderOption={(option, { selected }) => (
        <React.Fragment>
          <Checkbox
            icon={icon}
            checkedIcon={checkedIcon}
            style={{ marginRight: 8 }}
            checked={selected}
          />
          {option.item}
        </React.Fragment>
      )}
      style={{ width: 500 }}
      onChange={(e, options)=>{handleChange(e, options)}}
      renderInput={params => (
        <TextField
          {...params}
          variant="outlined"
          label="Menu"
          placeholder="Item"
          fullWidth
        />
      )}
    />
  );
}

Here options is passed as an argument to handleChange function which is an array of all the items checked.这里的options作为参数传递给 handleChange 函数,它是一个包含所有检查项目的数组。 So we will fetch the last selected item from that array and append that to the items array managed by setItem hook.因此,我们将从该数组中获取最后一个选定的项目,并将其附加到由 setItem 钩子管理的 items 数组中。

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

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