You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.5 KiB
95 lines
3.5 KiB
import React, { FC, ReactNode } from 'react'
|
|
import classNames from 'classnames'
|
|
import noop from 'lodash/noop'
|
|
import zxcvbn from 'zxcvbn'
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types'
|
|
import { faKey, faExclamationTriangle, faCheckCircle } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { notificationTypeToClassName } from 'src/utils'
|
|
import { FormNotification, ClassDictionary } from 'src/types'
|
|
|
|
export interface Props {
|
|
placeholder?: string
|
|
userInputs?: string[]
|
|
value?: string
|
|
notification?: FormNotification
|
|
setValue?: (value: string) => void
|
|
}
|
|
|
|
const PasswordField: FC<Props> = ({
|
|
placeholder,
|
|
userInputs = [],
|
|
value = '',
|
|
notification,
|
|
setValue = noop,
|
|
}) => {
|
|
const inputClassDictionary: ClassDictionary = { input: true }
|
|
const controlClassDictionary: ClassDictionary = { control: true, 'has-icons-left': true }
|
|
const helpClassDictionary: ClassDictionary = { help: true }
|
|
let icon: IconDefinition | undefined
|
|
let passwordMessage: ReactNode | undefined
|
|
|
|
if (value) {
|
|
const { score } = zxcvbn(value, userInputs)
|
|
|
|
switch (score) {
|
|
case 0:
|
|
inputClassDictionary['is-danger'] = true
|
|
controlClassDictionary['has-icons-right'] = true
|
|
icon = faExclamationTriangle
|
|
passwordMessage = <span>Strength: <span className="has-text-danger">Unusable</span></span>
|
|
break
|
|
case 1:
|
|
inputClassDictionary['is-danger'] = true
|
|
passwordMessage = <span>Strength: <span className="has-text-danger">Not good</span></span>
|
|
break
|
|
case 2:
|
|
inputClassDictionary['is-warning'] = true
|
|
passwordMessage = <span>Strength: <span className="has-text-warning">OK</span></span>
|
|
break
|
|
case 3:
|
|
inputClassDictionary['is-success'] = true
|
|
passwordMessage = <span>Strength: <span className="has-text-success">Good!</span></span>
|
|
break
|
|
case 4:
|
|
inputClassDictionary['is-success'] = true
|
|
controlClassDictionary['has-icons-right'] = true
|
|
icon = faCheckCircle
|
|
passwordMessage = <span>Strength: <span className="has-text-success">🔥</span></span>
|
|
break
|
|
}
|
|
}
|
|
|
|
if (notification) {
|
|
const ncn = notificationTypeToClassName(notification.type)
|
|
|
|
helpClassDictionary[ncn] = true
|
|
inputClassDictionary[ncn] = true
|
|
}
|
|
|
|
const helpText = () => {
|
|
if (notification) return <p className={classNames(helpClassDictionary)}>{notification.message}</p>
|
|
if (passwordMessage) return <p className="help">{passwordMessage}</p>
|
|
}
|
|
|
|
return (
|
|
<div className="field">
|
|
<label className="label">Password</label>
|
|
<div className={classNames(controlClassDictionary)}>
|
|
<input className={classNames(inputClassDictionary)} type="password" placeholder={placeholder} value={value} onChange={(e) => setValue(e.target.value)} />
|
|
<span className="icon is-small is-left">
|
|
<FontAwesomeIcon icon={faKey} />
|
|
</span>
|
|
{icon &&
|
|
<span className="icon is-small is-right">
|
|
<FontAwesomeIcon icon={icon} />
|
|
</span>
|
|
}
|
|
</div>
|
|
{helpText()}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default PasswordField
|