|
1 | 1 | import React from 'react'; |
2 | 2 |
|
3 | | -import {Copy, CopyCheck, Eye, EyeSlash} from '@gravity-ui/icons'; |
4 | | -import {Button, CopyToClipboard, CopyToClipboardStatus, Icon, TextInput} from '@gravity-ui/uikit'; |
| 3 | +import {PasswordInput} from '@gravity-ui/components'; |
| 4 | +import {TextInput} from '@gravity-ui/uikit'; |
5 | 5 | import _ from 'lodash'; |
6 | 6 |
|
7 | 7 | import {FieldRenderProps, NumberInputProps, StringInputProps} from '../../../../core'; |
8 | | -import {block} from '../../../utils'; |
9 | 8 |
|
10 | | -import './Text.scss'; |
11 | | - |
12 | | -const b = block('text'); |
13 | | - |
14 | | -export const Text = <T extends NumberInputProps | StringInputProps>({name, input, spec}: T) => { |
15 | | - const {value, onBlur, onChange, onFocus} = input; |
16 | | - const [hideValue, setHideValue] = React.useState(spec.viewSpec.type === 'password'); |
17 | | - |
18 | | - const handleChange = React.useCallback( |
19 | | - (value: string) => { |
20 | | - (onChange as FieldRenderProps<string>['input']['onChange'])(value); |
21 | | - }, |
22 | | - [onChange, spec], |
23 | | - ); |
24 | | - |
25 | | - const type = React.useMemo(() => { |
26 | | - if (spec.viewSpec.type === 'password') { |
27 | | - return 'password'; |
28 | | - } |
29 | | - |
30 | | - return 'text'; |
31 | | - }, [spec.viewSpec.type]); |
32 | | - |
33 | | - const additionalRightContent = React.useMemo(() => { |
34 | | - if (type === 'password') { |
35 | | - const onClick = () => { |
36 | | - setHideValue((hideValue) => !hideValue); |
37 | | - }; |
38 | | - |
39 | | - return ( |
40 | | - <div className={b()}> |
41 | | - {input.value ? ( |
42 | | - <CopyToClipboard text={String(value)} timeout={500}> |
43 | | - {(state) => ( |
44 | | - <Button view="flat-secondary" className={b('button')} size="s"> |
45 | | - <Icon |
46 | | - size={14} |
47 | | - data={ |
48 | | - state === CopyToClipboardStatus.Pending |
49 | | - ? Copy |
50 | | - : CopyCheck |
51 | | - } |
52 | | - /> |
53 | | - </Button> |
54 | | - )} |
55 | | - </CopyToClipboard> |
56 | | - ) : null} |
57 | | - <Button |
58 | | - view="flat-secondary" |
59 | | - onClick={onClick} |
60 | | - className={b('button')} |
61 | | - size="s" |
62 | | - > |
63 | | - <Icon data={hideValue ? Eye : EyeSlash} size={14} /> |
64 | | - </Button> |
65 | | - </div> |
66 | | - ); |
67 | | - } |
68 | | - |
69 | | - return undefined; |
70 | | - }, [hideValue, input.value, type, value]); |
71 | | - |
72 | | - return ( |
73 | | - <TextInput |
74 | | - type={hideValue ? 'password' : 'text'} |
75 | | - value={_.isNil(value) ? '' : `${value}`} |
76 | | - hasClear={true} |
77 | | - onBlur={onBlur} |
78 | | - onFocus={onFocus} |
79 | | - onUpdate={handleChange} |
80 | | - disabled={spec.viewSpec.disabled} |
81 | | - placeholder={spec.viewSpec.placeholder} |
82 | | - autoComplete={type === 'password' ? 'new-password' : undefined} |
83 | | - qa={name} |
84 | | - rightContent={additionalRightContent} |
85 | | - /> |
86 | | - ); |
| 9 | +export const Text = <T extends NumberInputProps | StringInputProps>({ |
| 10 | + name, |
| 11 | + input: {value, onBlur, onChange, onFocus}, |
| 12 | + spec, |
| 13 | +}: T) => { |
| 14 | + const props = { |
| 15 | + value: _.isNil(value) ? '' : `${value}`, |
| 16 | + hasClear: true, |
| 17 | + onBlur: onBlur, |
| 18 | + onFocus: onFocus, |
| 19 | + onUpdate: onChange as FieldRenderProps<string>['input']['onChange'], |
| 20 | + disabled: spec.viewSpec.disabled, |
| 21 | + placeholder: spec.viewSpec.placeholder, |
| 22 | + qa: name, |
| 23 | + }; |
| 24 | + |
| 25 | + if (spec.viewSpec.type === 'password') { |
| 26 | + return ( |
| 27 | + <PasswordInput {...props} autoComplete="new-password" showCopyButton showRevealButton /> |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + return <TextInput {...props} type="text" />; |
87 | 32 | }; |
0 commit comments