Skip to content

Commit d504cc2

Browse files
authored
Merge pull request #63 from input-output-hk/feat/convert-password-box-to-uncontrolled-component
Feat/convert password box to uncontrolled component
2 parents 562852f + effffb7 commit d504cc2

File tree

6 files changed

+47
-170
lines changed

6 files changed

+47
-170
lines changed

src/design-system/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ export { ToastBar } from './toast-bar';
2424
export * from './tooltip';
2525
export { Message } from './message';
2626
export { Metadata, MetadataLink } from './metadata';
27-
export { PasswordBox, UncontrolledPasswordBox } from './password-box';
27+
export { PasswordBox } from './password-box';
2828
export type {
29-
UncontrolledPasswordBoxProps,
3029
PasswordBoxProps,
3130
OnPasswordChange,
3231
Password,
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
export { PasswordBox } from './password-box.component';
22
export type { PasswordBoxProps } from './password-box.component';
3-
export { UncontrolledPasswordBox } from './uncontrolled-password-box.component';
4-
export type { UncontrolledPasswordBoxProps } from './uncontrolled-password-box.component';
53
export type {
64
OnPasswordChange,
75
Password,
8-
} from './uncontrolled-password-box-input.component';
6+
} from './password-box-input.component';

src/design-system/password-box/password-box-input.component.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@ import { Text } from '../text';
99
import { PasswordInputButton } from './password-box-button.component';
1010
import * as cx from './password-box-input.css';
1111

12-
export interface PasswordInputProps extends Form.FormControlProps {
12+
export type Password = {
13+
input: HTMLInputElement;
14+
// TODO: convert this to UInt8Array
15+
value: string;
16+
};
17+
18+
export type OnPasswordChange = (event: Readonly<Password>) => void;
19+
20+
export interface PasswordInputProps
21+
extends Omit<Form.FormControlProps, 'value' | 'onChange'> {
1322
required?: boolean;
1423
disabled?: boolean;
1524
id?: string;
1625
label: string;
1726
name?: string;
18-
value: string;
1927
errorMessage?: string;
20-
onChange?: (event: Readonly<React.ChangeEvent<HTMLInputElement>>) => void;
28+
onChange: OnPasswordChange;
2129
defaultIsPasswordVisible?: boolean;
2230
containerClassName?: string;
2331
containerStyle?: React.CSSProperties;
@@ -30,10 +38,10 @@ export const PasswordInput = ({
3038
id,
3139
label,
3240
name,
33-
value,
3441
errorMessage = '',
3542
containerClassName = '',
3643
onChange,
44+
autoFocus,
3745
defaultIsPasswordVisible = false,
3846
containerStyle,
3947
testId,
@@ -42,8 +50,11 @@ export const PasswordInput = ({
4250
defaultIsPasswordVisible,
4351
);
4452

53+
const onChangeHandler: React.ChangeEventHandler<HTMLInputElement> = event =>
54+
onChange({ input: event.target, value: event.target.value });
55+
4556
return (
46-
<div className={cx.root}>
57+
<div className={cx.root} data-testid={testId && `${testId}-container`}>
4758
<Form.Field
4859
name="field"
4960
className={cn(cx.container, {
@@ -58,11 +69,11 @@ export const PasswordInput = ({
5869
type={isPasswordVisible ? 'text' : 'password'}
5970
required={required}
6071
placeholder=""
72+
autoFocus={autoFocus}
6173
className={cn(cx.input, { [cx.largeDots]: !isPasswordVisible })}
6274
disabled={disabled}
6375
name={name}
64-
value={value}
65-
onChange={onChange}
76+
onChange={onChangeHandler}
6677
id={id}
6778
data-testid={testId}
6879
/>
@@ -73,7 +84,12 @@ export const PasswordInput = ({
7384
{label}
7485
</Form.Label>
7586
<PasswordInputButton
76-
testId={testId && `${testId}-toggle`}
87+
testId={
88+
testId &&
89+
(isPasswordVisible
90+
? `${testId}-hide-icon`
91+
: `${testId}-show-icon`)
92+
}
7793
onClick={(event): void => {
7894
event.preventDefault();
7995
setIsPasswordVisible(!isPasswordVisible);
@@ -84,7 +100,11 @@ export const PasswordInput = ({
84100
</Flex>
85101
</Form.Field>
86102
{errorMessage && (
87-
<Text.Label color="error" className={cx.errorMessage}>
103+
<Text.Label
104+
color="error"
105+
className={cx.errorMessage}
106+
data-testid={testId && `${testId}-error`}
107+
>
88108
{errorMessage}
89109
</Text.Label>
90110
)}

src/design-system/password-box/password-box.stories.tsx

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ const VariantsTable = (): JSX.Element => (
2929
<Variants.Row>
3030
<Variants.Cell>
3131
<PasswordBox
32-
value="Input Text"
32+
placeholder="Input Text"
3333
label="Password"
34+
onChange={v => console.log(v.value)}
3435
defaultIsPasswordVisible
3536
onSubmit={action('onSubmit')}
3637
/>
3738
</Variants.Cell>
3839
<Variants.Cell>
3940
<PasswordBox
40-
value="Input Text"
41+
onChange={v => console.log(v.value)}
42+
placeholder="Input Text"
4143
label="Password"
4244
onSubmit={action('onSubmit')}
4345
/>
@@ -49,11 +51,10 @@ const VariantsTable = (): JSX.Element => (
4951

5052
const PasswordBoxComponent = ({
5153
label = 'Password',
52-
value = '',
5354
...props
5455
}: Readonly<Partial<PasswordBoxProps>>): JSX.Element => (
5556
<PasswordBox
56-
value={value}
57+
onChange={v => console.log(v.value)}
5758
label={label}
5859
rootStyle={{ width: '200px' }}
5960
onSubmit={action('onSubmit')}
@@ -85,12 +86,15 @@ const MainComponents = (): JSX.Element => (
8586
<HoverPasswordBoxComponent />
8687
</Variants.Cell>
8788
<Variants.Cell>
88-
<PasswordBoxComponent value="Input Text" defaultIsPasswordVisible />
89+
<PasswordBoxComponent
90+
placeholder="Input Text"
91+
defaultIsPasswordVisible
92+
/>
8993
</Variants.Cell>
9094
<Variants.Cell>
91-
<PasswordBox
95+
<PasswordBoxComponent
9296
label="Password"
93-
value="Input Text"
97+
placeholder="Input Text"
9498
defaultIsPasswordVisible
9599
errorMessage="Error"
96100
rootStyle={{ width: '200px' }}
@@ -106,18 +110,18 @@ const MainComponents = (): JSX.Element => (
106110
</Variants.Row>
107111
<Variants.Row>
108112
<Variants.Cell>
109-
<PasswordBoxComponent value="" />
113+
<PasswordBoxComponent placeholder="" />
110114
</Variants.Cell>
111115
<Variants.Cell>
112116
<HoverPasswordBoxComponent />
113117
</Variants.Cell>
114118
<Variants.Cell>
115-
<PasswordBoxComponent value="Input Text" />
119+
<PasswordBoxComponent placeholder="Input Text" />
116120
</Variants.Cell>
117121
<Variants.Cell>
118-
<PasswordBox
122+
<PasswordBoxComponent
119123
label="Password"
120-
value="Input Text"
124+
placeholder="Input Text"
121125
defaultIsPasswordVisible
122126
errorMessage="Error"
123127
rootStyle={{ width: '200px', marginTop: '20px' }}
@@ -135,19 +139,13 @@ const MainComponents = (): JSX.Element => (
135139
);
136140

137141
export const Overview = (): JSX.Element => {
138-
const [value, setValue] = useState('');
139-
140142
return (
141143
<Grid>
142144
<Cell>
143145
<Section title="Copy for use">
144146
<Flex flexDirection="column" alignItems="center" w="$fill" my="$32">
145-
<PasswordBox
146-
value={value}
147+
<PasswordBoxComponent
147148
label="Password"
148-
onChange={(event): void => {
149-
setValue(event.target.value);
150-
}}
151149
rootStyle={{ width: '500px' }}
152150
onSubmit={action('onSubmit')}
153151
/>

src/design-system/password-box/uncontrolled-password-box-input.component.tsx

Lines changed: 0 additions & 113 deletions
This file was deleted.

src/design-system/password-box/uncontrolled-password-box.component.tsx

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)