Skip to content

Instantly share code, notes, and snippets.

@prontiol
Created December 6, 2017 10:56
Show Gist options
  • Save prontiol/7784ef8bf9b8b407039447bb98664d1a to your computer and use it in GitHub Desktop.
Save prontiol/7784ef8bf9b8b407039447bb98664d1a to your computer and use it in GitHub Desktop.
react-text-mask focus workaround
import TextMask from 'react-text-mask';
// here goes quick and dirty hack, to activate text-mask on focus
class CustomTextMask extends TextMask {
public render() {
const { mask, guide, pipe, placeholderChar, keepCharPositions, value, onChange, showMask, ...props } = this.props;
return (
<input
{...props}
onFocus={this.onFocus}
onInput={(event: ChangeEvent<HTMLInputElement>) => this.onChange(event)}
defaultValue={this.props.value}
ref={(inputElement: HTMLInputElement) => (this.inputElement = inputElement)}
/>
);
}
private onFocus = () => {
setTimeout(() => {
this.inputElement.value = this.inputElement.value || ' ';
this.textMaskInputElement.update();
}, 0);
}
}
export default CustomTextMask;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment