【问题标题】:Material UI - How to ListItem link to open a dialogMaterial UI - 如何通过 ListItem 链接打开对话框
【发布时间】:2020-05-09 18:45:54
【问题描述】:

我先说一下我在 React 上很年轻的事实。

作为仪表板的一部分,我在 Card 组件中有一个员工项目的动态列表。 我想添加为每个员工创建链接的功能,单击链接将打开一个模式对话框以启用在同一页面中的编辑。

我不太确定如何在 ListItem->Link->EmployeeEdit 功能之间建立连接,以确保我在每一步都传递了正确的道具。

所以到目前为止,我有这个在链接列表中显示员工,但不确定如何创建关系以呈现 EmployeeEdit 组件:

列表:

    <List dense={true}>
        {(Array.isArray(employees) && employees.length != 0 )  ? employees.map(employee => {
            const LinkToEdit = ({props}) => <Link {...props} record={employee}/>;
            return(
            <ListItem
                key={employee.id}
                button
                component={LinkToEdit}
            >
                <ListItemText
                    primary={
                        `${employee.firstName} ${employee.lastName}`
                    }
                    secondary={translate('tequiti.entity.joined_date', {
                        createdAt: new Date(employee.createdAt).toLocaleString('en-US')
                    })}

                />
                <ListItemSecondaryAction/>
            </ListItem>
            )
        }) : <p>No employees for this company</p>
        }
    </List>

编辑员工:

const EmployeeEdit = (props) => {
    const translate = useTranslate();
    const [state, setState] = useState({showEditDialog: true});
    let {showEditDialog} = state;
    const handleCloseEditClick = () => {
        setState({
            showEditDialog: false
        });
    };
    return(
        <Dialog
            fullWidth
            open={showEditDialog}
            modal={ true }
            onClose={handleCloseEditClick}
            aria-label="Edit Employee"
            {...props}
        >
            <DialogTitle>Edit Employee</DialogTitle>
            <DialogContent>
                <TabbedForm resource="employee" toolbar={<UpdateActions {...props} />}>
                    <FormTab label={translate('root.label.identity')}>
                        <PersonInput />
                    </FormTab>
                    <FormTab label={translate('root.label.address')}>
                        <AddressInput />
                    </FormTab>
                </TabbedForm>
            </DialogContent>
            <DialogActions>
                <Button label="ra.action.cancel" onClick={() => handleCloseEditClick()}>
                    <IconCancel />
                </Button>
            </DialogActions>
        </Dialog>
)};

任何建议/帮助都会很棒!

【问题讨论】:

  • 我已回滚您的编辑。在您的问题中添加(已解决)或编辑答案是不可接受的。如果您发现收到的答案有助于解决您的问题,您可以通过单击问题旁边的复选框来做到这一点,它会告诉所有人您已经找到了解决方案。如果您自己找到了答案并想分享,请在下面为此目的提供的空白处写下答案。您可以在help center 中找到更多信息。

标签: reactjs material-ui


【解决方案1】:

如果我没看错的话,你有一个Dialog 用于每个员工的模态(即EmployeeEdit 组件)。我可能会将其简化为在页面上有一个 EmployeeEdit 组件,并有一个它接受的 employee 属性。如果有员工属性,则显示该员工的对话框。如果employee 属性为假,则隐藏对话框。然后,您可以为设置当前员工的列表项设置一个onClick。所以像这样(为简洁起见):

const EmployeeEdit = ({employee, onClose, onChange, ...props}) => {
  const showDialog = Boolean(employee)
  //use onClose and onChange as appropriate
  ...
}
const EmployeeList = ({employees}) => {
  const [currentEmployee, setCurrentEmployee] = useState(null);
  const clearEmployee = () => {
    setCurrentEmployee(null)
  }
  const handleChange = (employee) => {
    //do something with the results, probably update employees array,
    //then close dialog by clearing the current employee
    setCurrentEmployee(null)
  }

  return (
    <>
      <EmployeeEdit employee={currentEmployee} onClose={clearEmployee} onChange={handleChange}/>
      {employees.map(e => {
        <ListItem onClick={() => setCurrentEmloyee(e)} />
      })}
    </>
  )
}

【讨论】:

  • 谢谢。我看到你在那里做了什么,我更喜欢这种模式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 2017-05-18
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
相关资源
最近更新 更多