forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradioGroup.tsx
More file actions
75 lines (63 loc) · 2.5 KB
/
radioGroup.tsx
File metadata and controls
75 lines (63 loc) · 2.5 KB
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
72
73
74
75
import { Radio, Space } from 'antd';
import React, { FC, ReactElement, useEffect, useMemo } from 'react';
import { useGet } from '@/hooks';
import { useReferenceList } from '@/providers/referenceListDispatcher';
import { getDataSourceList } from './utils';
import { IAjaxResponse } from '@/interfaces';
import { isAjaxSuccessResponse } from '@/interfaces/ajaxResponse';
import { ILabelValue } from '../dropdown/model';
import { executeScriptSync } from '@/providers/form/utils';
import { IRadioProps } from './interfaces';
import { DEFAULT_MARGINS } from '@/components/formDesigner/utils/designerConstants';
type FetchResponse = ILabelValue<any>[] | {
items?: ILabelValue<any>[];
};
const RadioGroup: FC<IRadioProps> = (model) => {
const { referenceListId, items = [], value, onChange } = model;
const { data: refListItems } = useReferenceList(referenceListId);
//#region Data source is url
const { refetch, data } = useGet<IAjaxResponse<FetchResponse>>({ path: model.dataSourceUrl, lazy: true });
useEffect(() => {
if (model.dataSourceType === 'url' && model.dataSourceUrl) {
refetch();
}
}, [model.dataSourceType, model.dataSourceUrl]);
const fetchedData = isAjaxSuccessResponse(data) ? data.result : undefined;
const reducedData = useMemo<ILabelValue<any>[]>(() => {
const list = fetchedData
? Array.isArray(fetchedData)
? fetchedData
: fetchedData.items ?? []
: undefined;
if (Array.isArray(list) && model.reducerFunc) {
return executeScriptSync(model.reducerFunc, { data: list });
}
return list;
}, [fetchedData, model.reducerFunc]);
//#endregion
const options = useMemo(
() => getDataSourceList(model.dataSourceType, items, refListItems?.items, reducedData) || [],
[model.dataSourceType, items, refListItems?.items, reducedData],
);
const renderCheckGroup = (): ReactElement => (
<Radio.Group
className="sha-radio-group"
disabled={model.readOnly}
value={value != null ? `${value}` : undefined}
onBlur={model.onBlur}
onFocus={model.onFocus}
onChange={onChange}
style={model.style}
>
<Space direction={model.direction} style={{ margin: `${DEFAULT_MARGINS.vertical} ${DEFAULT_MARGINS.horizontal}` }}>
{options?.map((checkItem, index) => (
<Radio key={index} value={`${checkItem.value}`} disabled={model.readOnly}>
{checkItem.label}
</Radio>
))}
</Space>
</Radio.Group>
);
return renderCheckGroup();
};
export default RadioGroup;