forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformItem.tsx
More file actions
74 lines (66 loc) · 2.53 KB
/
formItem.tsx
File metadata and controls
74 lines (66 loc) · 2.53 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
import React, { cloneElement, FC, ReactElement, useState } from 'react';
import { ConfigurableFormItem } from '@/components';
import SettingsControl from '../settingsControl';
import { ISettingsFormItemProps } from '../settingsFormItem';
import { useStyles } from '../styles/styles';
const FormItem: FC<ISettingsFormItemProps> = (props) => {
const { styles } = useStyles();
const { name, label, tooltip, required, hidden, jsSetting, children, valuePropName = 'value', layout } = props;
const [hasCode, setHasCode] = useState(false);
const childElement = children as ReactElement;
const readOnly = props.readOnly || childElement.props.readOnly || childElement.props.disabled;
const handleChange = (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);
};
const createClonedElement = (value, onChange) => cloneElement(
childElement,
{
...childElement?.props,
readOnly: readOnly,
size: 'small',
disabled: readOnly,
onChange: handleChange(onChange),
[valuePropName]: value
}
);
return (
<ConfigurableFormItem
model={{
hideLabel: props.hideLabel,
propertyName: name,
label: <div className={styles.label} style={{ top: '8px', fontSize: '12px' }}>{label}</div>,
type: '',
id: '',
description: tooltip,
validate: { required },
hidden,
layout,
size: 'small',
}}
className='sha-js-label'
>
{(value, onChange) =>
!jsSetting ? (
createClonedElement(value, onChange)
) : (
<SettingsControl
propertyName={name}
mode={'value'}
onChange={onChange}
value={value}
setHasCode={setHasCode}
hasCode={hasCode}
readOnly={readOnly}
>
{(value, onChange) => createClonedElement(value, onChange,)}
</SettingsControl>
)
}
</ConfigurableFormItem>
);
};
export default FormItem;