1 | ---
|
2 | title: Password Input
|
3 | storybookPath: forms-passwordinput--default
|
4 | isExperimentalPackage: true
|
5 | ---
|
6 |
|
7 | The password input component allows accepting of password text values. The
|
8 | component builds off its underlying [TextInput](/package/text-input) component
|
9 | and toggles the input mode between `password` and `text` depending on whether
|
10 | the show password icon button is toggled to show or hide the password value.
|
11 |
|
12 | ## Usage
|
13 |
|
14 | The component must be nested within a [`Field`](/package/field). See
|
15 | [`Field`](/package/field) for more details.
|
16 |
|
17 | ## Examples
|
18 |
|
19 | ### Field utilities
|
20 |
|
21 | You can use the parent [Field](/package/field) to set appropriate field level
|
22 | labels, description adornments and messages.
|
23 |
|
24 | ```jsx live
|
25 | <Stack gap="large">
|
26 | <Field
|
27 | label="Password"
|
28 | adornment={
|
29 | <Text>
|
30 | <TextLink href="#">Forgot password?</TextLink>
|
31 | </Text>
|
32 | }
|
33 | description={
|
34 | 'Make sure your chosen password is at least 10 characters long.'
|
35 | }
|
36 | >
|
37 | <PasswordInput />
|
38 | </Field>
|
39 | </Stack>
|
40 | ```
|
41 |
|
42 | ### Uncontrolled
|
43 |
|
44 | Similar to [TextInput](/package/text-input), the component can be uncontrolled
|
45 | and managing its own internal state.
|
46 |
|
47 | ```jsx live
|
48 | const passwordRef = React.useRef(null);
|
49 | const [value, setValue] = React.useState('');
|
50 | const showValueHandler = React.useCallback(() => {
|
51 | setValue(passwordRef.current?.value);
|
52 | }, [setValue]);
|
53 |
|
54 | return (
|
55 | <Stack gap="large">
|
56 | <Field label="Example uncontrolled">
|
57 | <PasswordInput ref={passwordRef} />
|
58 | </Field>
|
59 | <Button onClick={showValueHandler}>Display password value</Button>
|
60 | <Text>The password value is: {value}</Text>
|
61 | </Stack>
|
62 | );
|
63 | ```
|
64 |
|
65 | ### Controlled
|
66 |
|
67 | Similar to [TextInput](/package/text-input), the component can be controlled by
|
68 | passing in a `value` and `onChange` handler and have its state managed in a
|
69 | parent component.
|
70 |
|
71 | ```jsx live
|
72 | const [value, setValue] = React.useState(``);
|
73 |
|
74 | return (
|
75 | <Stack gap="large">
|
76 | <Field label="Example controlled">
|
77 | <PasswordInput value={value} onChange={e => setValue(e.target.value)} />
|
78 | </Field>
|
79 | <Text>The current password value is: {value}</Text>
|
80 | </Stack>
|
81 | );
|
82 | ```
|
83 |
|
84 | ## Props
|
85 |
|
86 | The component passes props into its underlying [TextInput](/package/text-input)
|
87 | component and are not listed here.
|