Skip to content

Commit 1216691

Browse files
committed
Added molecules examples
1 parent ae02fbb commit 1216691

File tree

8 files changed

+255
-1
lines changed

8 files changed

+255
-1
lines changed

config/componentData.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import ProgressBar from "../ProgressBar";
4+
import EyeIcon from "../EyeIcon";
5+
import TextInput from "../TextInput";
6+
7+
/** Password input with integrated label, quality tips, and show password toggle. */
8+
class PasswordInput extends React.Component {
9+
constructor(props) {
10+
super(props);
11+
this.state = {
12+
showPassword: false
13+
};
14+
}
15+
16+
toggleShowPassword = event => {
17+
this.setState(prevState => {
18+
return { showPassword: !prevState.showPassword };
19+
});
20+
if (event) event.preventDefault();
21+
};
22+
23+
render() {
24+
const {
25+
htmlId,
26+
value,
27+
label,
28+
error,
29+
onChange,
30+
placeholder,
31+
maxLength,
32+
showVisibilityToggle,
33+
quality,
34+
...props
35+
} = this.props;
36+
const { showPassword } = this.state;
37+
38+
return (
39+
<TextInput
40+
htmlId={htmlId}
41+
label={label}
42+
placeholder={placeholder}
43+
type={showPassword ? "text" : "password"}
44+
onChange={onChange}
45+
value={value}
46+
maxLength={maxLength}
47+
error={error}
48+
required
49+
{...props}
50+
>
51+
{showVisibilityToggle && (
52+
<a
53+
href=""
54+
onClick={this.toggleShowPassword}
55+
style={{ marginLeft: 5 }}
56+
>
57+
<EyeIcon />
58+
</a>
59+
)}
60+
{value.length > 0 && quality && (
61+
<ProgressBar percent={quality} width={130} />
62+
)}
63+
</TextInput>
64+
);
65+
}
66+
}
67+
68+
PasswordInput.propTypes = {
69+
/** Unique HTML ID. Used for tying label to HTML input. Handy hook for automated testing. */
70+
htmlId: PropTypes.string.isRequired,
71+
72+
/** Input name. Recommend setting this to match object's property so a single change handler can be used by convention.*/
73+
name: PropTypes.string.isRequired,
74+
75+
/** Password value */
76+
value: PropTypes.any,
77+
78+
/** Input label */
79+
label: PropTypes.string,
80+
81+
/** Function called when password input value changes */
82+
onChange: PropTypes.func.isRequired,
83+
84+
/** Max password length accepted */
85+
maxLength: PropTypes.number,
86+
87+
/** Placeholder displayed when no password is entered */
88+
placeholder: PropTypes.string,
89+
90+
/** Set to true to show the toggle for displaying the currently entered password */
91+
showVisibilityToggle: PropTypes.bool,
92+
93+
/** Display password quality visually via ProgressBar, accepts a number between 0 and 100 */
94+
quality: PropTypes.number,
95+
96+
/** Validation error to display */
97+
error: PropTypes.string
98+
};
99+
100+
PasswordInput.defaultProps = {
101+
maxLength: 50,
102+
showVisibilityToggle: false,
103+
label: "Password"
104+
};
105+
106+
export default PasswordInput;

src/components/PasswordInput/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './PasswordInput';

src/components/TextInput/TextInput.js

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import Label from "../Label";
4+
5+
/** Text input with integrated label to enforce consistency in layout, error display, label placement, and required field marker. */
6+
function TextInput({
7+
htmlId,
8+
name,
9+
label,
10+
type = "text",
11+
required = false,
12+
onChange,
13+
placeholder,
14+
value,
15+
error,
16+
children,
17+
...props
18+
}) {
19+
return (
20+
<div style={{ marginBottom: 16 }}>
21+
<Label htmlFor={htmlId} label={label} required={required} />
22+
<input
23+
id={htmlId}
24+
type={type}
25+
name={name}
26+
placeholder={placeholder}
27+
value={value}
28+
onChange={onChange}
29+
style={error && { border: "solid 1px red" }}
30+
{...props}
31+
/>
32+
{children}
33+
{error && (
34+
<div className="error" style={{ color: "red" }}>
35+
{error}
36+
</div>
37+
)}
38+
</div>
39+
);
40+
}
41+
42+
TextInput.propTypes = {
43+
/** Unique HTML ID. Used for tying label to HTML input. Handy hook for automated testing. */
44+
htmlId: PropTypes.string.isRequired,
45+
46+
/** Input name. Recommend setting this to match object's property so a single change handler can be used. */
47+
name: PropTypes.string.isRequired,
48+
49+
/** Input label */
50+
label: PropTypes.string.isRequired,
51+
52+
/** Input type */
53+
type: PropTypes.oneOf(["text", "number", "password"]),
54+
55+
/** Mark label with asterisk if set to true */
56+
required: PropTypes.bool,
57+
58+
/** Function to call onChange */
59+
onChange: PropTypes.func.isRequired,
60+
61+
/** Placeholder to display when empty */
62+
placeholder: PropTypes.string,
63+
64+
/** Value */
65+
value: PropTypes.any,
66+
67+
/** String to display when error occurs */
68+
error: PropTypes.string,
69+
70+
/** Child component to display next to the input */
71+
children: PropTypes.node
72+
};
73+
74+
export default TextInput;

src/components/TextInput/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './TextInput';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React from "react";
2+
import PasswordInput from "comp/PasswordInput";
3+
4+
/** All features enabled */
5+
class ExampleAllFeatures extends React.Component {
6+
constructor(props) {
7+
super(props);
8+
9+
this.state = {
10+
password: ""
11+
};
12+
}
13+
14+
getQuality() {
15+
const length = this.state.password.length;
16+
return length > 10 ? 100 : length * 10;
17+
}
18+
19+
render() {
20+
return (
21+
<div>
22+
<PasswordInput
23+
htmlId="password-input-example-all-features"
24+
name="password"
25+
onChange={event => this.setState({ password: event.target.value })}
26+
value={this.state.password}
27+
minLength={8}
28+
placeholder="Enter password"
29+
showVisibilityToggle
30+
quality={this.getQuality()}
31+
{...this.props}
32+
/>
33+
</div>
34+
);
35+
}
36+
}
37+
38+
export default ExampleAllFeatures;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from "react";
2+
import TextInput from "comp/TextInput";
3+
4+
/** Required TextBox with error */
5+
export default class ExampleError extends React.Component {
6+
render() {
7+
return (
8+
<TextInput
9+
htmlId="example-optional"
10+
label="First Name"
11+
name="firstname"
12+
onChange={() => {}}
13+
required
14+
error="First name is required."
15+
/>
16+
);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from "react";
2+
import TextInput from "comp/TextInput";
3+
4+
/** Optional TextBox */
5+
export default class ExampleOptional extends React.Component {
6+
render() {
7+
return (
8+
<TextInput
9+
htmlId="example-optional"
10+
label="First Name"
11+
name="firstname"
12+
onChange={() => {}}
13+
/>
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)