-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathCustomFieldsAdditionalForm.js
More file actions
120 lines (101 loc) · 3.56 KB
/
CustomFieldsAdditionalForm.js
File metadata and controls
120 lines (101 loc) · 3.56 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { useRef, useCallback, useMemo, useEffect } from 'react';
import { Box, Field, TextInput, ToggleSwitch, Select } from '@rocket.chat/fuselage';
import { useTranslation } from '../../../../client/contexts/TranslationContext';
import { useForm } from '../../../../client/hooks/useForm';
const getInitialValues = (data) => ({
type: data.type || 'input',
required: !!data.required,
defaultValue: data.defaultValue ?? '',
options: data.options || '',
public: !!data.public,
});
const checkInvalidOptions = (value) => value.trim() !== '' && !/^([a-zA-Z0-9-_ ]+)(,\s*[a-zA-Z0-9-_ ]+)*$/i.test(value);
const CustomFieldsAdditionalFormContainer = ({ data = {}, state, onChange, className }) => {
const saveData = useRef({});
const t = useTranslation();
const onChangeValue = useCallback(({ initialValue, value, key }) => {
const { current } = saveData;
if (JSON.stringify(initialValue) !== JSON.stringify(value)) {
current[key] = value;
} else {
delete current[key];
}
}, []);
const { values, handlers, hasUnsavedChanges } = useForm(getInitialValues(data), onChangeValue);
const errors = useMemo(() => ({
optionsError: checkInvalidOptions(values.options) ? t('error-invalid-value') : undefined,
}), [t, values.options]);
const hasError = useMemo(() => !!Object.values(errors).filter(Boolean).length, [errors]);
useEffect(() => {
onChange({ data: saveData.current, hasError, hasUnsavedChanges });
}, [hasError, hasUnsavedChanges, onChange]);
return <CustomFieldsAdditionalForm values={values} handlers={handlers} state={state} className={className} errors={errors}/>;
};
const CustomFieldsAdditionalForm = ({ values = {}, handlers = {}, state, className, errors }) => {
const t = useTranslation();
const {
type,
required,
defaultValue,
options,
public: isPublic,
} = values;
const {
handleType,
handleRequired,
handleDefaultValue,
handleOptions,
handlePublic,
} = handlers;
const { optionsError } = errors;
const typeOptions = useMemo(() => [
['input', t('Input')],
['select', t('Select')],
], [t]);
return <>
<Field className={className}>
<Box display='flex' flexDirection='row'>
<Field.Label htmlFor='required'>{t('Required')}</Field.Label>
<Field.Row>
<ToggleSwitch id='required' checked={required} onChange={handleRequired}/>
</Field.Row>
</Box>
</Field>
<Field className={className}>
<Field.Label>{t('Type')}</Field.Label>
<Field.Row>
<Select options={typeOptions} value={type} onChange={handleType}/>
</Field.Row>
</Field>
<Field className={className}>
<Field.Label>{t('Default_value')}</Field.Label>
<Field.Row>
<TextInput value={defaultValue} onChange={handleDefaultValue} placeholder={t('Default_value')}/>
</Field.Row>
</Field>
<Field className={className}>
<Field.Label>{t('Options')}</Field.Label>
<Field.Row>
<TextInput value={options} onChange={handleOptions} error={optionsError} disabled={type === 'input'} placeholder={t('Options')}/>
</Field.Row>
<Field.Hint>
{t('Livechat_custom_fields_options_placeholder')}
</Field.Hint>
{optionsError && <Field.Error>
{optionsError}
</Field.Error>}
</Field>
<Field className={className}>
<Box display='flex' flexDirection='row'>
<Field.Label htmlFor='public'>{t('Public')}</Field.Label>
<Field.Row>
<ToggleSwitch disabled={!state.visibility} id='public' checked={isPublic} onChange={handlePublic}/>
</Field.Row>
</Box>
<Field.Hint>
{t('Livechat_custom_fields_public_description')}
</Field.Hint>
</Field>
</>;
};
export default CustomFieldsAdditionalFormContainer;