简体   繁体   English

如何在材料表中使用自定义“editComponent”?

[英]How to use custom 'editComponent' in material-table?

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我创建了以下剪辑项目以详细显示我需要的内容: 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'.我不知道,如何调用“SelectIconDilog”中的道具给出的“onChange”。

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>
    );
}

In SelectIconDialog use onChange={e => props.onChange(e)} , because e is the icon name and you want to assign it to your input .SelectIconDialog使用onChange={e => props.onChange(e)} ,因为e是图标名称,您希望将其分配给您的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:此外,在SimpleDialog您会收到错误,因为您没有为您的iconNames分配唯一键,请尝试:

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

Demo:演示:

编辑 stupefied-voice-bf5hg

  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,
      }))}
     />

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

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