skyouc
2 天以前 29b87cafae3e36a43978b16dd170749bbd2a0a6e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { TextField, useUpdate, useNotify, useRefresh, useRecordContext, useTranslate, useGetResourceLabel } from 'react-admin';
import { TextField as MuiTextField } from '@mui/material';
import { useState } from 'react';
 
const EditableTextField = ({ source, label, resource, isBlur }) => {
    const record = useRecordContext();
    const translate = useTranslate();
    const [value, setValue] = useState(record[source]);
    const [update] = useUpdate();
    const notify = useNotify();
    const refresh = useRefresh();
    label = label ? label : ""
    isBlur = isBlur == undefined ? false : isBlur
 
    const handleBlur = () => {
        isBlur ?
        update(
            resource,
            { id: record.id, data: { [source]: value } },
            {
                onSuccess: () => {
                    const succ = translate('common.action.updateSucc')
                    notify(succ, { type: 'success' });
                    refresh();
                },
                onError: () => {
                    const failed = translate('common.action.updateSucc')
                    notify(failed, { type: 'error' });
                    setValue(record[source]);
                }
            }
        ) :
        {}
    };
 
    return (
        <MuiTextField
            value={value}
            onChange={(e) => setValue(e.target.value)}
            label={translate(label)}
            onBlur={handleBlur}
            fullWidth
        />
    );
};
 
 
export default EditableTextField;