javascript 我如何使用自定义编辑组件与材料React表

kiayqfof  于 4个月前  发布在  Java
关注(0)|答案(1)|浏览(92)

我遵循官方的material-react-table文档如何创建一个CRUD表。参考链接在这里https://www.material-react-table.com/docs/examples/editing-crud。然而,我遇到了一个问题,当我试图使用我自己的自定义模态组件的“创建新”基本上我有一个自定义对话框,见下文。当我查看句柄创建函数中的值时,值为空除非我使用internalEditComponents。我如何在handle create函数中填充values props而不创建一堆自定义逻辑?
//创建函数

const handleCreate: MRT_TableOptions<any>['onCreatingRowSave'] = props => {
    const { values, table, row } = props;

    console.log('handleDomainCorrectionCreate', values); // this is empty

  };

字符串
//自定义对话框

renderCreateRowDialogContent: props => {
       const { table, row, internalEditComponents } = props;
      <DialogTitle>Add correction entry</DialogTitle>
          <DialogContent>
            <Box sx={{ my: 2 }}>
              <DialogContentText variant="subtitle2">From</DialogContentText>
              <TextField
                autoFocus
                id="error_name"
                label="Error Name"
                type="text"
                fullWidth
                variant="standard"
                name="error_name"
                onChange={event => setErrorName(event.target.value)}
                value={errorName}
              />
            </Box>
            <Box sx={{ my: 2 }}>
              <DialogContentText variant="subtitle2">To</DialogContentText>
              <TextField
                id="corrected_name"
                label="Corrected Name"
                type="text"
                fullWidth
                variant="standard"
                name="corrected_name"
                onChange={event => setCorrectedName(event.target.value)}
                value={correctedName}
              />
            </Box>
          </DialogContent>
          <DialogActions>
            <MRT_EditActionButtons variant="text" table={table} row={row} />
          </DialogActions>
}

l3zydbqr

l3zydbqr1#

您可以从material-react-table V1 crud示例中获得帮助:

const columns = useMemo<MRT_ColumnDef<IPmsProgress>[]>(
      () => [
        {
          ...
      ],
      [ validationErrors],
    );    
    
    // use this state for insert values
    const [values, setValues] = useState<any>(() =>
      columns.reduce((acc, column) => {
        acc[column.accessorKey ?? ''] = '';
        return acc;
      }, {} as any),
    );

    const handleCreateNewRow: MRT_TableOptions<IPmsProgress>['onCreatingRowSave'] = ({
      table,
    }) => {        
      const newValidationErrors = validatePmsProgress(values);
      if (Object.values(newValidationErrors).some((error) => error)) {
        setValidationErrors(newValidationErrors);
        return;
      } 
      const request: IRequestPmsProgress = {
        contractid: currentContractId,
        dateid: currentReportDateId,
        item: values.item,
        lastplanprogress: values.lastplanprogress,
        lastplanvirtualprogress: values.lastplanvirtualprogress,
      }
      setValidationErrors({});
      addPmsProgress(request);
      table.setCreatingRow(null); //exit creating mode
    };
    
    ...
    const table = useMaterialReactTable({
      ...
      renderCreateRowDialogContent: ({ table, row }) => (  <>
          <DialogTitle variant="h6" fontFamily="sans serif" textAlign='center'>Add new item</DialogTitle>
          <DialogContent
            sx={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}
          >
            <form onSubmit={(e) => e.preventDefault()}>
              <Stack
                sx={{
                  width: '100%',
                  minWidth: { xs: '300px', sm: '360px', md: '400px' },
                  gap: '1.5rem',
                }}
              >
                {columns.filter(column => 
                    column.accessorKey === 'item' ||
                    column.accessorKey === 'lastplanprogress' || 
                    column.accessorKey === 'lastplanvirtualprogress').map((column) => (
                  <TextField
                    key={column.accessorKey}
                    variant='filled'
                    label={column.header}
                    name={column.accessorKey}
                    type={column.accessorKey === 'item' ? 'text' : 'number'}
                    onChange={(e) =>
                      setValues({ ...values, [e.target.name]: e.target.value })
                    }
                  />
                ))}
              </Stack>
            </form>
          </DialogContent>
          <DialogActions sx={{ p: '1.25rem' }}>
            <MRT_EditActionButtons variant="text" table={table} row={row}/>
          </DialogActions>                
        </>
      ),
      ...
    })
    ...

字符串

相关问题