Created
December 6, 2017 10:56
-
-
Save prontiol/7784ef8bf9b8b407039447bb98664d1a to your computer and use it in GitHub Desktop.
react-text-mask focus workaround
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
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