Skip to content

Commit bc6bfb6

Browse files
committed
feat: add focus and blur handlers for encrypted text input
1 parent 2b7d89a commit bc6bfb6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

ui/src/components/TextComponent/TextComponent.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@ export interface TextComponentProps {
1515
}
1616

1717
class TextComponent extends Component<TextComponentProps> {
18+
private wasMasked: boolean = false;
19+
1820
handleChange = (e: unknown, { value }: { value: string | number }) => {
1921
this.props.handleChange(this.props.field, value);
2022
};
2123

24+
handleFocus = (e: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
25+
if (this.props.encrypted && e.target.value === '******') {
26+
this.wasMasked = true;
27+
this.props.handleChange(this.props.field, '');
28+
}
29+
};
30+
31+
handleBlur = (e: React.FocusEvent<HTMLInputElement | HTMLTextAreaElement>) => {
32+
if (this.props.encrypted && e.target.value === '' && this.wasMasked) {
33+
this.props.handleChange(this.props.field, '******');
34+
this.wasMasked = false;
35+
}
36+
};
37+
2238
render() {
2339
const { id, field, disabled, value, encrypted, ...restProps } = this.props;
2440
const restSuiProps = excludeControlWrapperProps(restProps);
@@ -30,6 +46,8 @@ class TextComponent extends Component<TextComponentProps> {
3046
disabled={disabled && 'dimmed'}
3147
value={value === null || typeof value === 'undefined' ? '' : value.toString()}
3248
onChange={this.handleChange}
49+
onFocus={this.handleFocus}
50+
onBlur={this.handleBlur}
3351
type={encrypted ? 'password' : 'text'}
3452
/>
3553
);

0 commit comments

Comments
 (0)