forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigurableFormItemSetting.tsx
More file actions
93 lines (87 loc) · 3.15 KB
/
configurableFormItemSetting.tsx
File metadata and controls
93 lines (87 loc) · 3.15 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import React, { FC, cloneElement } from 'react';
import { Form, FormItemProps } from 'antd';
import { getFieldNameFromExpression } from '@/providers/form/utils';
import { getPropertySettingsFromData } from '@/designer-components/_settings/utils';
import { SettingsControl, useShaFormInstance } from '@/index';
import { IConfigurableFormItemChildFunc, IConfigurableFormItemProps } from './model';
import { ConfigurableFormItemLive } from './configurableFormItemLive';
import { useStyles } from './styles';
import classNames from 'classnames';
export const ConfigurableFormItemSetting: FC<IConfigurableFormItemProps> = ({
children,
model,
valuePropName,
autoAlignLabel = true,
}) => {
const { formData } = useShaFormInstance();
const { styles } = useStyles({ autoAlignLabel });
if (model.hidden) return null;
const { _mode: mode } = getPropertySettingsFromData(formData, model.propertyName);
const formProps: FormItemProps = {
name: getFieldNameFromExpression(model.propertyName),
label: model.label,
// style: model.style,
required: model.validate?.required,
tooltip: model.description || undefined,
hidden: model.hidden,
};
if (typeof children === 'function') {
const childrenFunc = children as IConfigurableFormItemChildFunc;
return (
<Form.Item {...formProps}>
<SettingsControl propertyName={model.propertyName} mode={mode}>
{(value, onChange, propertyName) => childrenFunc(value, onChange, propertyName)}
</SettingsControl>
</Form.Item>
);
}
const childrenElement = children as React.ReactElement<any>;
const readOnly = model.readOnly || childrenElement.props.readOnly || childrenElement.props.disabled;
return (
<ConfigurableFormItemLive
model={{
propertyName: model.propertyName,
label: model.label,
type: '',
id: '',
description: model.description,
validate: { required: model.validate?.required },
hidden: model.hidden,
}}
className={classNames(styles.settingsFormItem, "sha-js-label")}
labelCol={{ span: 24 }}
wrapperCol={{ span: 24 }}
autoAlignLabel={autoAlignLabel}
>
{(value, onChange) => {
return (
<SettingsControl
propertyName={model.propertyName}
mode="value"
onChange={onChange}
value={value}
readOnly={readOnly}
>
{(value, onChange) => {
return cloneElement(
childrenElement,
{
...childrenElement?.props,
readOnly: readOnly,
disabled: readOnly,
onChange: (...args: any[]) => {
const event = args[0];
const data = event && event.target && typeof event.target === 'object' && valuePropName in event.target
? (event.target as HTMLInputElement)[valuePropName]
: event;
onChange(data);
},
[valuePropName]: value,
});
}}
</SettingsControl>
);
}}
</ConfigurableFormItemLive>
);
};