Debian Codegraph @

install/freeipa-webui/src/components/layouts/PasswordInput/PasswordInput.tsx

raw
1 import React from "react";
2 // PatternFly
3 import {
4 Button,
5 InputGroup,
6 TextInput,
7 InputGroupItem,
8 } from "@patternfly/react-core";
9 import { EyeSlashIcon } from "@patternfly/react-icons";
10 import { EyeIcon } from "@patternfly/react-icons";
11 // Layout helpers
12 import InputRequiredText from "src/components/layouts/InputRequiredText";
13 import InputWithValidation, {
14 RuleProps,
15 } from "src/components/layouts/InputWithValidation";
16
17 export interface PropsToPasswordInput {
18 dataCy: string;
19 ariaLabel?: string;
20 name?: string;
21 id?: string;
22 value?: string;
23 passwordHidden?: boolean;
24 onFocus?: React.MouseEventHandler<HTMLButtonElement>;
25 onChange: (value: string) => void;
26 onRevealHandler: (value: boolean) => void;
27 validated?: "success" | "warning" | "error" | "default";
28 isRequired?: boolean;
29 isDisabled?: boolean;
30 // Optional validation rules to enable helper messages rendering
31 rules?: Array<RuleProps>;
32 requiredHelperText?: string;
33 }
34
35 // Note - onChange function should trigger validation check (validated prop)
36
37 const RevealButton = (props: {
38 dataCy: string;
39 passwordHidden?: boolean;
40 onRevealHandler: (value: boolean) => void;
41 }) => (
42 <InputGroupItem>
43 <Button
44 data-cy={props.dataCy + "-reveal-button"}
45 variant="control"
46 onClick={() => props.onRevealHandler(!props.passwordHidden)}
47 aria-label={props.passwordHidden ? "Show password" : "Hide password"}
48 >
49 {props.passwordHidden ? <EyeIcon /> : <EyeSlashIcon />}
50 </Button>
51 </InputGroupItem>
52 );
53
54 const PasswordInput = (props: PropsToPasswordInput) => {
55 if (props.rules && props.rules.length > 0) {
56 return (
57 <InputGroup>
58 <div className="pf-v6-u-display-block">
59 <InputWithValidation
60 dataCy={props.dataCy}
61 id={props.id as string}
62 name={props.name as string}
63 value={props.value || ""}
64 onChange={props.onChange}
65 isRequired={props.isRequired}
66 isDisabled={props.isDisabled}
67 rules={props.rules!}
68 type={props.passwordHidden ? "password" : "text"}
69 />
70 </div>
71 <RevealButton
72 dataCy={props.dataCy}
73 passwordHidden={props.passwordHidden}
74 onRevealHandler={props.onRevealHandler}
75 />
76 </InputGroup>
77 );
78 }
79
80 if (props.isRequired) {
81 return (
82 <InputGroup>
83 <div className="pf-v6-u-display-block">
84 <InputRequiredText
85 dataCy={props.dataCy}
86 id={props.id as string}
87 name={props.name as string}
88 value={props.value || ""}
89 onChange={props.onChange}
90 isDisabled={props.isDisabled}
91 requiredHelperText={props.requiredHelperText}
92 type={props.passwordHidden ? "password" : "text"}
93 />
94 </div>
95 <RevealButton
96 dataCy={props.dataCy}
97 passwordHidden={props.passwordHidden}
98 onRevealHandler={props.onRevealHandler}
99 />
100 </InputGroup>
101 );
102 }
103
104 return (
105 <InputGroup>
106 <InputGroupItem>
107 <TextInput
108 data-cy={props.dataCy}
109 aria-label={props.ariaLabel ?? props.name}
110 type={props.passwordHidden ? "password" : "text"}
111 id={props.id}
112 name={props.name}
113 value={props.value}
114 onFocus={props.onFocus}
115 onChange={(_event, value) => props.onChange(value)}
116 validated={props.validated}
117 isRequired={props.isRequired || false}
118 isDisabled={props.isDisabled || false}
119 />
120 </InputGroupItem>
121 <RevealButton
122 dataCy={props.dataCy}
123 passwordHidden={props.passwordHidden}
124 onRevealHandler={props.onRevealHandler}
125 />
126 </InputGroup>
127 );
128 };
129
130 export default PasswordInput;