Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: made a new password input component #108

Merged
merged 10 commits into from
Nov 1, 2023
11 changes: 11 additions & 0 deletions src/components/Password/Password.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@use '../variables';

$block: '.#{variables.$ns}password';

#{$block} {
display: flex;

&__button {
--yc-button-background-color-hover: transparent;
}
}
65 changes: 65 additions & 0 deletions src/components/Password/Password.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';

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

import {block} from '../utils/cn';

import './Password.scss';

const b = block('password');

export type PasswordProps = Required<Pick<TextInputProps, 'onUpdate' | 'value'>> &
Omit<TextInputProps, 'rightContent' | 'type'> & {
amje marked this conversation as resolved.
Show resolved Hide resolved
showCopyButton?: boolean;
amje marked this conversation as resolved.
Show resolved Hide resolved
};

export const Password: React.FC<PasswordProps> = (props) => {
amje marked this conversation as resolved.
Show resolved Hide resolved
const {autoComplete, value, showCopyButton} = props;

const [hideValue, setHideValue] = React.useState(true);

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

return (
<div className={b()}>
amje marked this conversation as resolved.
Show resolved Hide resolved
{value && showCopyButton ? (
<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>
);
}, [hideValue, value, showCopyButton]);

return (
<TextInput
{...props}
type={hideValue ? 'password' : 'text'}
rightContent={additionalRightContent}
autoComplete={autoComplete ? autoComplete : 'new-password'}
/>
);
};
26 changes: 26 additions & 0 deletions src/components/Password/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Password

Password display component

### PropTypes

Same as [TextInput component](https://github.com/gravity-ui/uikit/blob/main/src/components/controls/TextInput/README.md), with some exceptions:

- `value` is required property;
- `onUpdate` is required property;
- `rightContent` is omitted;
- `type` is omitted;

| Property | Type | Required | Description |
| :------------- | :-------- | :------- | :--------------- |
| showCopyButton | `boolean` | | show copy button |

#### Usage example

```jsx harmony
function MyComponent() {
const [value, setValue] = React.useState('');

return <Password showCopyButton={true} onUpdate={setValue} value={value} />;
}
```
18 changes: 18 additions & 0 deletions src/components/Password/__stories__/Password.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';

import type {Meta, StoryFn} from '@storybook/react';

import {Password} from '../Password';

export default {
title: 'Components/Password',
component: Password,
} as Meta;

const DefaultTemplate: StoryFn<React.ComponentProps<typeof Password>> = () => {
const [value, setValue] = React.useState('');

return <Password showCopyButton={true} onUpdate={setValue} value={value} />;
};

export const Default = DefaultTemplate.bind({});
1 change: 1 addition & 0 deletions src/components/Password/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Password';
Loading