-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathCustomControls.demo.tsx
More file actions
128 lines (117 loc) · 3.97 KB
/
Copy pathCustomControls.demo.tsx
File metadata and controls
128 lines (117 loc) · 3.97 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
121
122
123
124
125
126
127
128
import React from 'react';
import { Button, Gapped, Input, Select } from '@skbkontur/react-ui';
import type { ValidationBehaviour, ValidationInfo } from '../../../../src';
import { ValidationContainer, ValidationWrapper } from '../../../../src';
import { Form } from '../../../Common/Form';
interface CustomControlProps<Elem = HTMLElement> {
ref?: React.ForwardedRef<Elem>;
error?: boolean;
warning?: boolean;
onBlur?: React.FocusEventHandler<Elem>;
onChange?: React.ChangeEventHandler<Elem>;
}
interface CustomInputProps
extends CustomControlProps<HTMLInputElement>,
React.InputHTMLAttributes<HTMLInputElement> {
onValueChange?: unknown;
}
const CustomInput = React.forwardRef<HTMLInputElement, CustomInputProps>(function MyInput(
{ error, warning, onValueChange, ...props },
ref,
) {
const style: React.CSSProperties = {};
if (error) {
style.borderColor = 'red';
style.backgroundColor = '#ff000020';
}
if (warning) {
style.borderColor = 'orange';
style.backgroundColor = '#ffa50020';
}
return <input type="text" ref={ref} style={style} {...props} />;
});
const CustomControlsDemo = () => {
const container = React.useRef<ValidationContainer>(null);
const [value1, setValue1] = React.useState('error');
const [value2, setValue2] = React.useState('error');
const [isValid, setIsValid] = React.useState<boolean | null>(null);
const [type, setType] = React.useState<ValidationBehaviour>('submit');
const submitHandler = async () => {
setIsValid(Boolean(await container.current?.validate()));
};
const typeChangeHandler = (typeValue: ValidationBehaviour) => {
if (typeValue !== 'submit') {
setIsValid(null);
}
setType(typeValue);
};
const renderValidStatus = () => {
switch (isValid) {
case null:
return <b>Отправьте форму</b>;
case false:
return <b style={{ color: '#d70c17' }}>Форма невалидна</b>;
case true:
return <b style={{ color: '#5199db' }}>Форма валидна</b>;
default:
return null;
}
};
const validate = (value: string): ValidationInfo | null => {
switch (value) {
case 'error':
return { type, message: <b>Ошибка</b>, level: 'error' };
case 'warning':
return {
type,
message: <b>Предупреждение</b>,
level: 'warning',
};
}
return null;
};
return (
<Form>
<ValidationContainer ref={container}>
<Gapped gap={10} vertical>
<Form.Line title="Тип валидации">
<Gapped>
<Select
onValueChange={typeChangeHandler}
items={[
['lostfocus', 'lostfocus'],
['immediate', 'immediate'],
['submit', 'submit'],
]}
defaultValue={type}
/>
{type === 'submit' && (
<Gapped>
<Button use="primary" onClick={submitHandler}>
Submit
</Button>
{renderValidStatus()}
</Gapped>
)}
</Gapped>
</Form.Line>
<span>
Введите <code>«error»</code> или <code>«warning»</code> для вызова
соответствующих уровней валидации
</span>
<Form.Line title="Кастомный input">
<ValidationWrapper validationInfo={validate(value1)}>
<CustomInput value={value1} onChange={(e) => setValue1(e.target.value)} />
</ValidationWrapper>
</Form.Line>
<Form.Line title="Input из react-ui">
<ValidationWrapper validationInfo={validate(value2)}>
<Input value={value2} onValueChange={setValue2} />
</ValidationWrapper>
</Form.Line>
</Gapped>
</ValidationContainer>
</Form>
);
};
export default CustomControlsDemo;