-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(PasswordStrength): demo conversion to TS (#9518)
- Loading branch information
1 parent
95ecf33
commit 173a799
Showing
2 changed files
with
154 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
packages/react-core/src/demos/examples/PasswordStrength/PasswordStrengthDemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import React from 'react'; | ||
import { | ||
Form, | ||
FormGroup, | ||
FormHelperText, | ||
HelperText, | ||
Popover, | ||
HelperTextItem, | ||
TextInput | ||
} from '@patternfly/react-core'; | ||
import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon'; | ||
import ExclamationCircleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon'; | ||
import ExclamationTriangleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-triangle-icon'; | ||
import CheckCircleIcon from '@patternfly/react-icons/dist/esm/icons/check-circle-icon'; | ||
|
||
export const PasswordStrengthDemo: React.FunctionComponent = () => { | ||
type HelperTextItemVariant = 'default' | 'indeterminate' | 'warning' | 'success' | 'error'; | ||
interface PassStrength { | ||
variant: HelperTextItemVariant; | ||
icon: JSX.Element; | ||
text: string; | ||
} | ||
|
||
const [password, setPassword] = React.useState(''); | ||
const [ruleLength, setRuleLength] = React.useState<HelperTextItemVariant>('indeterminate'); | ||
const [ruleContent, setRuleContent] = React.useState<HelperTextItemVariant>('indeterminate'); | ||
const [ruleCharacters, setRuleCharacters] = React.useState<HelperTextItemVariant>('indeterminate'); | ||
const [passStrength, setPassStrength] = React.useState<PassStrength>({ | ||
variant: 'error', | ||
icon: <ExclamationCircleIcon />, | ||
text: 'Weak' | ||
}); | ||
|
||
const handlePasswordInput = (_event: any, password: string) => { | ||
setPassword(password); | ||
validate(password); | ||
}; | ||
|
||
const validate = (password: string) => { | ||
if (password === '') { | ||
setRuleLength('indeterminate'); | ||
setRuleContent('indeterminate'); | ||
setRuleCharacters('indeterminate'); | ||
return; | ||
} | ||
|
||
if (password.length < 14) { | ||
setRuleLength('error'); | ||
} else { | ||
setRuleLength('success'); | ||
} | ||
|
||
if (/redhat/gi.test(password)) { | ||
setRuleContent('error'); | ||
} else { | ||
setRuleContent('success'); | ||
} | ||
|
||
let rulesCount = 0; | ||
let strCount = 0; | ||
if (/[a-z]/g.test(password)) { | ||
rulesCount++; | ||
} | ||
if (/[A-Z]/g.test(password)) { | ||
strCount += password.match(/[A-Z]/g).length; | ||
rulesCount++; | ||
} | ||
if (/\d/g.test(password)) { | ||
strCount += password.match(/\d/g).length; | ||
rulesCount++; | ||
} | ||
if (/\W/g.test(password)) { | ||
strCount += password.match(/\W/g).length; | ||
rulesCount++; | ||
} | ||
|
||
if (rulesCount < 3) { | ||
setRuleCharacters('error'); | ||
} else { | ||
setRuleCharacters('success'); | ||
} | ||
|
||
if (strCount < 3) { | ||
setPassStrength({ variant: 'error', icon: <ExclamationCircleIcon />, text: 'Weak' }); | ||
} else if (strCount < 5) { | ||
setPassStrength({ variant: 'warning', icon: <ExclamationTriangleIcon />, text: 'Medium' }); | ||
} else { | ||
setPassStrength({ variant: 'success', icon: <CheckCircleIcon />, text: 'Strong' }); | ||
} | ||
}; | ||
|
||
const iconPopover = ( | ||
<Popover headerContent={<div>Password Requirements</div>} bodyContent={<div>Password rules</div>}> | ||
<button | ||
type="button" | ||
aria-label="More info for name field" | ||
onClick={(e) => e.preventDefault()} | ||
aria-describedby="password-field" | ||
className="pf-v5-c-form__group-label-help" | ||
> | ||
<HelpIcon /> | ||
</button> | ||
</Popover> | ||
); | ||
|
||
const passStrLabel = ( | ||
<HelperText> | ||
<HelperTextItem variant={passStrength.variant} icon={passStrength.icon}> | ||
{passStrength.text} | ||
</HelperTextItem> | ||
</HelperText> | ||
); | ||
|
||
return ( | ||
<Form> | ||
<FormGroup | ||
label="Password" | ||
labelIcon={iconPopover} | ||
isRequired | ||
fieldId="password-field" | ||
{...(ruleLength === 'success' && | ||
ruleContent === 'success' && | ||
ruleCharacters === 'success' && { | ||
labelInfo: passStrLabel | ||
})} | ||
> | ||
<TextInput | ||
isRequired | ||
type="text" | ||
id="password-field" | ||
name="password-field" | ||
aria-describedby="password-field-helper" | ||
aria-invalid={ruleLength === 'error' || ruleContent === 'error' || ruleCharacters === 'error'} | ||
value={password} | ||
onChange={handlePasswordInput} | ||
/> | ||
<FormHelperText> | ||
<HelperText component="ul" aria-live="polite" id="password-field-helper"> | ||
<HelperTextItem isDynamic variant={ruleLength} component="li"> | ||
Must be at least 14 characters | ||
</HelperTextItem> | ||
<HelperTextItem isDynamic variant={ruleContent} component="li"> | ||
Cannot contain the word "redhat" | ||
</HelperTextItem> | ||
<HelperTextItem isDynamic variant={ruleCharacters} component="li"> | ||
Must include at least 3 of the following: lowercase letter, uppercase letters, numbers, symbols | ||
</HelperTextItem> | ||
</HelperText> | ||
</FormHelperText> | ||
</FormGroup> | ||
</Form> | ||
); | ||
}; |