11import React from 'react' ;
22
3- import { TextInput } from '@gravity-ui/uikit' ;
3+ import { Copy , CopyCheck , Eye , EyeSlash } from '@gravity-ui/icons' ;
4+ import { Button , CopyToClipboard , CopyToClipboardStatus , Icon , TextInput } from '@gravity-ui/uikit' ;
45import _ from 'lodash' ;
56
6- import { FieldRenderProps , NumberInputProps , StringInputProps , isStringSpec } from '../../../../core' ;
7+ import { FieldRenderProps , NumberInputProps , StringInputProps } from '../../../../core' ;
78import { block } from '../../../utils' ;
8- import { GenerateRandomValueButton } from '../../GenerateRandomValueButton' ;
99
1010import './Text.scss' ;
1111
1212const b = block ( 'text' ) ;
1313
1414export const Text = < T extends NumberInputProps | StringInputProps > ( { name, input, spec} : T ) => {
1515 const { value, onBlur, onChange, onFocus} = input ;
16+ const [ hideValue , setHideValue ] = React . useState ( spec . viewSpec . type === 'password' ) ;
1617
1718 const handleChange = React . useCallback (
1819 ( value : string ) => {
@@ -29,45 +30,58 @@ export const Text = <T extends NumberInputProps | StringInputProps>({name, input
2930 return 'text' ;
3031 } , [ spec . viewSpec . type ] ) ;
3132
32- const textInput = React . useMemo (
33- ( ) => (
34- < TextInput
35- type = { type }
36- value = { _ . isNil ( value ) ? '' : `${ value } ` }
37- hasClear = { true }
38- onBlur = { onBlur }
39- onFocus = { onFocus }
40- onUpdate = { handleChange }
41- disabled = { spec . viewSpec . disabled }
42- placeholder = { spec . viewSpec . placeholder }
43- autoComplete = { type === 'password' ? 'new-password' : undefined }
44- qa = { name }
45- />
46- ) ,
47- [
48- handleChange ,
49- name ,
50- onBlur ,
51- onFocus ,
52- spec . viewSpec . disabled ,
53- spec . viewSpec . placeholder ,
54- type ,
55- value ,
56- ] ,
57- ) ;
33+ const additionalRightContent = React . useMemo ( ( ) => {
34+ if ( type === 'password' ) {
35+ const onClick = ( ) => {
36+ setHideValue ( ( hideValue ) => ! hideValue ) ;
37+ } ;
5838
59- const content = React . useMemo ( ( ) => {
60- if ( isStringSpec ( spec ) ) {
6139 return (
6240 < div className = { b ( ) } >
63- { textInput }
64- < GenerateRandomValueButton spec = { spec } onChange = { handleChange } />
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 >
6565 </ div >
6666 ) ;
6767 }
6868
69- return < React . Fragment > { textInput } </ React . Fragment > ;
70- } , [ handleChange , spec , textInput ] ) ;
69+ return undefined ;
70+ } , [ hideValue , input . value , type , value ] ) ;
7171
72- return < React . Fragment > { content } </ React . Fragment > ;
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+ ) ;
7387} ;
0 commit comments