UNPKG

2.23 kBMarkdownView Raw
1---
2title: Password Input
3storybookPath: forms-passwordinput--default
4isExperimentalPackage: true
5---
6
7The password input component allows accepting of password text values. The
8component builds off its underlying [TextInput](/package/text-input) component
9and toggles the input mode between `password` and `text` depending on whether
10the show password icon button is toggled to show or hide the password value.
11
12## Usage
13
14The 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
21You can use the parent [Field](/package/field) to set appropriate field level
22labels, 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
44Similar to [TextInput](/package/text-input), the component can be uncontrolled
45and managing its own internal state.
46
47```jsx live
48const passwordRef = React.useRef(null);
49const [value, setValue] = React.useState('');
50const showValueHandler = React.useCallback(() => {
51 setValue(passwordRef.current?.value);
52}, [setValue]);
53
54return (
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
67Similar to [TextInput](/package/text-input), the component can be controlled by
68passing in a `value` and `onChange` handler and have its state managed in a
69parent component.
70
71```jsx live
72const [value, setValue] = React.useState(``);
73
74return (
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
86The component passes props into its underlying [TextInput](/package/text-input)
87component and are not listed here.