How to use custom 'editComponent' in material-table?

若如初见. 提交于 2021-02-19 10:38:54

问题


I'm trying to use 'material-table' in my project. My 'icon' column contains icon name. I need change this icon by selecting it from external dialog. I have problem with updating table data from external dialog. When I use 'input' element and I change icon name, it works correctly.

editComponent: props => (
  <input
    type="text"
    value={props.value}
    onChange={e => props.onChange(e.target.value)}
  />
)

I don't know, how to achive this result with my dialog. I've created the following snipped project to show in details what I need: https://codesandbox.io/embed/gifted-liskov-ih72m

When I change icon name by text and save changes - is OK. Changes are saved. When I change icon by selecting it from external dialog and save changes - it doesn't work.

editComponent: props => (
  <SelectIconDialog
    value={props.value}
    onChange={e => props.onChange(e.target.value)}
  />
)

I don't know, how to invoke 'onChange' given by props inside the 'SelectIconDilog'.

export default function SelectIconDialog(props) {
    const { value, onChange } = props;
    const [open, setOpen] = React.useState(false);
    const [selectedValue, setSelectedValue] = React.useState(value);

    function handleClickOpen() {
        setOpen(true);
    }

    const handleClose = value => {
        setOpen(false);
        setSelectedValue(value);
        //onChange(value); // it doesn't work
    };

    return (
        <div>
            <Tooltip title={selectedValue}>
                <IconButton
                    onClick={handleClickOpen}
                    color="default"
                >
                    <DynamicIcon 
                        iconName={selectedValue} 
                        // onChange={onChange} // it doesn't work
                    />
                </IconButton>
            </Tooltip>
            <SimpleDialog selectedValue={selectedValue} open={open} onClose={handleClose} />
        </div>
    );
}

回答1:


In SelectIconDialog use onChange={e => props.onChange(e)}, because e is the icon name and you want to assign it to your input.

  const [state, setState] = React.useState({
    columns: [
      {
       ...
        editComponent: props => (
          <SelectIconDialog value={props.value} onChange={props.onChange} />
        )
      },
      ...
}

Moreover, in SimpleDialog you getting an error because you didn't assign unique keys to your iconNames, try:

  <div>
    {iconsNames.map((iName, key) => (
      <Tooltip title={iName} key={key}>
        <IconButton onClick={() => handleItemClick(iName)}>
          <DynamicIcon iconName={iName} />
        </IconButton>
      </Tooltip>
    ))}
  </div>;

Demo:




回答2:


  title: "title",
  field: 'fieldname',
  render: rowData => <span>{findStock(rowData.fieldname)}</span>,
  editComponent: props =>
    <Select
      value={{ value: props.value, label: findStock(props.value) }}
      onChange={e => props.onChange(e.value)}
      name='fieldname'
      autoFocus
      textFieldProps={{
        label: "fieldname" ,
        InputLabelProps: {
          shrink: true,
        },
        variant: 'outlined',
      }}
      options={stocks.map((item) => ({
        value: item.id,
        label: item.name,
      }))}
     />


来源:https://stackoverflow.com/questions/56576286/how-to-use-custom-editcomponent-in-material-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!