Skip to content

Commit

Permalink
feat: update password input (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
bocembocem authored Nov 9, 2023
1 parent 57586c2 commit faf3e8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 89 deletions.
9 changes: 0 additions & 9 deletions src/lib/kit/components/Inputs/Text/Text.scss

This file was deleted.

105 changes: 25 additions & 80 deletions src/lib/kit/components/Inputs/Text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,87 +1,32 @@
import React from 'react';

import {Copy, CopyCheck, Eye, EyeSlash} from '@gravity-ui/icons';
import {Button, CopyToClipboard, CopyToClipboardStatus, Icon, TextInput} from '@gravity-ui/uikit';
import {PasswordInput} from '@gravity-ui/components';
import {TextInput} from '@gravity-ui/uikit';
import _ from 'lodash';

import {FieldRenderProps, NumberInputProps, StringInputProps} from '../../../../core';
import {block} from '../../../utils';

import './Text.scss';

const b = block('text');

export const Text = <T extends NumberInputProps | StringInputProps>({name, input, spec}: T) => {
const {value, onBlur, onChange, onFocus} = input;
const [hideValue, setHideValue] = React.useState(spec.viewSpec.type === 'password');

const handleChange = React.useCallback(
(value: string) => {
(onChange as FieldRenderProps<string>['input']['onChange'])(value);
},
[onChange, spec],
);

const type = React.useMemo(() => {
if (spec.viewSpec.type === 'password') {
return 'password';
}

return 'text';
}, [spec.viewSpec.type]);

const additionalRightContent = React.useMemo(() => {
if (type === 'password') {
const onClick = () => {
setHideValue((hideValue) => !hideValue);
};

return (
<div className={b()}>
{input.value ? (
<CopyToClipboard text={String(value)} timeout={500}>
{(state) => (
<Button view="flat-secondary" className={b('button')} size="s">
<Icon
size={14}
data={
state === CopyToClipboardStatus.Pending
? Copy
: CopyCheck
}
/>
</Button>
)}
</CopyToClipboard>
) : null}
<Button
view="flat-secondary"
onClick={onClick}
className={b('button')}
size="s"
>
<Icon data={hideValue ? Eye : EyeSlash} size={14} />
</Button>
</div>
);
}

return undefined;
}, [hideValue, input.value, type, value]);

return (
<TextInput
type={hideValue ? 'password' : 'text'}
value={_.isNil(value) ? '' : `${value}`}
hasClear={true}
onBlur={onBlur}
onFocus={onFocus}
onUpdate={handleChange}
disabled={spec.viewSpec.disabled}
placeholder={spec.viewSpec.placeholder}
autoComplete={type === 'password' ? 'new-password' : undefined}
qa={name}
rightContent={additionalRightContent}
/>
);
export const Text = <T extends NumberInputProps | StringInputProps>({
name,
input: {value, onBlur, onChange, onFocus},
spec,
}: T) => {
const props = {
value: _.isNil(value) ? '' : `${value}`,
hasClear: true,
onBlur: onBlur,
onFocus: onFocus,
onUpdate: onChange as FieldRenderProps<string>['input']['onChange'],
disabled: spec.viewSpec.disabled,
placeholder: spec.viewSpec.placeholder,
qa: name,
};

if (spec.viewSpec.type === 'password') {
return (
<PasswordInput {...props} autoComplete="new-password" showCopyButton showRevealButton />
);
}

return <TextInput {...props} type="text" />;
};

0 comments on commit faf3e8c

Please sign in to comment.