#
vincentlu
3 天以前 44153e246cc041180f8544e05d81f5432b14a808
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import React from 'react';
import { Stack, TextField } from '@mui/material';
import {
    AutocompleteInput,
    FormDataConsumer,
    ReferenceInput,
    SelectInput,
    required,
    useTranslate,
} from 'react-admin';
import { REFERENCE_INPUT_PAGESIZE } from '@/config/setting';
import { DEFAULT_SCOPE_TYPE, SCOPE_OPTIONS, getScopeConfig } from './guaranteeConstants';
 
const ScopeField = () => {
    const translate = useTranslate();
 
    return (
        <Stack spacing={2} sx={{ width: '100%' }}>
            <SelectInput
                label="table.field.guarantee.scopeType"
                source="scopeType"
                choices={SCOPE_OPTIONS}
                optionText={(choice) => translate(choice.labelKey)}
                optionValue="id"
                translateChoice={false}
                defaultValue={DEFAULT_SCOPE_TYPE}
                helperText={translate('page.guarantee.scope.helper')}
                validate={[required()]}
            />
            <FormDataConsumer>
                {({ formData, ...rest }) => {
                    const currentType = formData.scopeType || DEFAULT_SCOPE_TYPE;
                    const config = getScopeConfig(currentType);
 
                    if (config.valueType === 'reference') {
                        return (
                            <ReferenceInput
                                key={currentType}
                                source="scopeValue"
                                reference={config.reference}
                                perPage={REFERENCE_INPUT_PAGESIZE}
                                {...rest}
                            >
                                <AutocompleteInput
                                    label="table.field.guarantee.scopeValue"
                                    optionText={config.optionText}
                                    optionValue={config.optionValue || 'id'}
                                    filterToQuery={config.filterToQuery}
                                    validate={[required()]}
                                    fullWidth
                                />
                            </ReferenceInput>
                        );
                    }
 
                    return (
                        <TextField
                            label={translate('table.field.guarantee.scopeValue')}
                            value={translate('page.guarantee.scope.globalHelper')}
                            helperText={translate('page.guarantee.scope.globalTip')}
                            disabled
                            fullWidth
                        />
                    );
                }}
            </FormDataConsumer>
        </Stack>
    );
};
 
export default ScopeField;