2018-01-26 22:11:52 +00:00
|
|
|
// Either self contained, or controlled component for having a password field
|
|
|
|
// with a toggle to turn it into a visible text field.
|
|
|
|
// Pass `isVisible` and `handleToggleVisibility` to control the visibility
|
|
|
|
// yourself, otherwise all visibiility changes are managed in internal state.
|
|
|
|
import React from 'react';
|
2018-06-18 01:53:00 +00:00
|
|
|
|
2018-03-01 17:53:29 +00:00
|
|
|
import { Input, TextArea } from 'components/ui';
|
2018-06-18 01:53:00 +00:00
|
|
|
import './TogglablePassword.scss';
|
2018-01-26 22:11:52 +00:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
// Shared props
|
2018-03-01 17:53:29 +00:00
|
|
|
className?: string;
|
2018-01-26 22:11:52 +00:00
|
|
|
value: string;
|
|
|
|
placeholder?: string;
|
|
|
|
name?: string;
|
|
|
|
disabled?: boolean;
|
|
|
|
ariaLabel?: string;
|
|
|
|
toggleAriaLabel?: string;
|
|
|
|
isValid?: boolean;
|
|
|
|
isVisible?: boolean;
|
2018-03-08 19:28:43 +00:00
|
|
|
readOnly?: boolean;
|
2018-01-26 22:11:52 +00:00
|
|
|
|
|
|
|
// Textarea-only props
|
|
|
|
isTextareaWhenVisible?: boolean;
|
|
|
|
rows?: number;
|
|
|
|
onEnter?(): void;
|
|
|
|
|
|
|
|
// Shared callbacks
|
|
|
|
onChange?(ev: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>): void;
|
2018-02-02 06:01:30 +00:00
|
|
|
onFocus?(ev: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>): void;
|
|
|
|
onBlur?(ev: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>): void;
|
2018-01-26 22:11:52 +00:00
|
|
|
handleToggleVisibility?(): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
isVisible: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class TogglablePassword extends React.PureComponent<Props, State> {
|
|
|
|
public state: State = {
|
|
|
|
isVisible: !!this.props.isVisible
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:34:27 +00:00
|
|
|
public UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2018-01-26 22:11:52 +00:00
|
|
|
if (this.props.isVisible !== nextProps.isVisible) {
|
|
|
|
this.setState({ isVisible: !!nextProps.isVisible });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const {
|
2018-03-01 17:53:29 +00:00
|
|
|
className,
|
2018-01-26 22:11:52 +00:00
|
|
|
value,
|
|
|
|
placeholder,
|
|
|
|
name,
|
|
|
|
disabled,
|
|
|
|
ariaLabel,
|
2018-02-02 06:01:30 +00:00
|
|
|
toggleAriaLabel,
|
2018-01-26 22:11:52 +00:00
|
|
|
isTextareaWhenVisible,
|
|
|
|
isValid,
|
|
|
|
onChange,
|
2018-02-02 06:01:30 +00:00
|
|
|
onFocus,
|
|
|
|
onBlur,
|
2018-03-08 19:28:43 +00:00
|
|
|
handleToggleVisibility,
|
|
|
|
readOnly
|
2018-01-26 22:11:52 +00:00
|
|
|
} = this.props;
|
|
|
|
const { isVisible } = this.state;
|
|
|
|
|
|
|
|
return (
|
2018-03-22 03:50:25 +00:00
|
|
|
<div className={`TogglablePassword input-group input-group-inline`}>
|
2018-01-26 22:11:52 +00:00
|
|
|
{isTextareaWhenVisible && isVisible ? (
|
2018-03-01 17:53:29 +00:00
|
|
|
<TextArea
|
2018-05-13 19:24:50 +00:00
|
|
|
isValid={!!isValid}
|
|
|
|
className={className}
|
2018-01-26 22:11:52 +00:00
|
|
|
value={value}
|
|
|
|
name={name}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={onChange}
|
|
|
|
onKeyDown={this.handleTextareaKeyDown}
|
2018-02-02 06:01:30 +00:00
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
2018-01-26 22:11:52 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
rows={this.props.rows || 3}
|
|
|
|
aria-label={ariaLabel}
|
2018-03-08 19:28:43 +00:00
|
|
|
readOnly={readOnly}
|
2018-01-26 22:11:52 +00:00
|
|
|
/>
|
|
|
|
) : (
|
2018-03-01 17:53:29 +00:00
|
|
|
<Input
|
2018-05-13 19:24:50 +00:00
|
|
|
isValid={!!isValid}
|
2018-01-26 22:11:52 +00:00
|
|
|
value={value}
|
|
|
|
name={name}
|
|
|
|
disabled={disabled}
|
|
|
|
type={isVisible ? 'text' : 'password'}
|
2018-05-13 19:24:50 +00:00
|
|
|
className={`${className} border-rad-right-0`}
|
2018-01-26 22:11:52 +00:00
|
|
|
placeholder={placeholder}
|
|
|
|
onChange={onChange}
|
2018-02-02 06:01:30 +00:00
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
2018-01-26 22:11:52 +00:00
|
|
|
aria-label={ariaLabel}
|
2018-03-08 19:28:43 +00:00
|
|
|
readOnly={readOnly}
|
2018-01-26 22:11:52 +00:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<span
|
|
|
|
onClick={handleToggleVisibility || this.toggleVisibility}
|
2018-02-02 06:01:30 +00:00
|
|
|
aria-label={toggleAriaLabel}
|
2018-01-26 22:11:52 +00:00
|
|
|
role="button"
|
|
|
|
className="TogglablePassword-toggle input-group-addon"
|
|
|
|
>
|
|
|
|
<i className={`fa fa-${isVisible ? 'eye-slash' : 'eye'}`} />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private toggleVisibility = () => {
|
|
|
|
this.setState({ isVisible: !this.state.isVisible });
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleTextareaKeyDown = (ev: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
|
|
if (this.props.onEnter && ev.keyCode === 13) {
|
|
|
|
ev.preventDefault();
|
|
|
|
this.props.onEnter();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|