Skip to content

Commit ac47377

Browse files
committed
Example of wrapping the TextValidator with masking state handler:
Uses IconButton rather than CSS for cursor styling.
1 parent 22605bf commit ac47377

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import { TextValidator } from 'react-material-ui-form-validator';
3+
import { InputAdornment } from '@material-ui/core';
4+
import Visibility from '@material-ui/icons/Visibility';
5+
import VisibilityOff from '@material-ui/icons/VisibilityOff';
6+
import IconButton from '@material-ui/core/IconButton';
7+
8+
export default class PasswordValidator extends React.Component {
9+
10+
state = {
11+
showPassword: false
12+
};
13+
14+
toggleShowPassword = () => {
15+
this.setState({
16+
showPassword: !this.state.showPassword
17+
});
18+
}
19+
20+
render() {
21+
return (
22+
<TextValidator
23+
{...this.props}
24+
type={this.state.showPassword ? 'text' : 'password'}
25+
InputProps={{
26+
endAdornment:
27+
<InputAdornment position="end">
28+
<IconButton
29+
aria-label="Toggle password visibility"
30+
onClick={this.toggleShowPassword}
31+
>
32+
{this.state.showPassword ? <Visibility /> : <VisibilityOff />}
33+
</IconButton>
34+
</InputAdornment>
35+
}}
36+
/>
37+
);
38+
}
39+
40+
}

interface/src/forms/WiFiSettingsForm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { isNetworkOpen, networkSecurityMode } from '../constants/WiFiSecurityMod
2525
import isIP from '../validators/isIP';
2626
import isHostname from '../validators/isHostname';
2727
import optional from '../validators/optional';
28+
import PasswordValidator from '../components/PasswordValidator';
2829

2930
const styles = theme => ({
3031
loadingSettings: {
@@ -110,7 +111,7 @@ class WiFiSettingsForm extends React.Component {
110111
}
111112
{
112113
!isNetworkOpen(selectedNetwork) &&
113-
<TextValidator
114+
<PasswordValidator
114115
validators={['matchRegexp:^.{0,64}$']}
115116
errorMessages={['Password must be 64 characters or less']}
116117
name="password"

0 commit comments

Comments
 (0)