Skip to content

Instantly share code, notes, and snippets.

@Amareis
Forked from prontiol/MaskedInput.tsx
Last active December 6, 2017 11:58
Show Gist options
  • Save Amareis/5f77ecc56178e89798235f406462873c to your computer and use it in GitHub Desktop.
Save Amareis/5f77ecc56178e89798235f406462873c to your computer and use it in GitHub Desktop.
react-text-mask focus workaround
import React from 'react'
import TextMask from 'react-text-mask'
export default class CustomTextMask extends TextMask {
render() {
const {value, ...props} = this.props
return (
<input
{...props}
onFocus={this.onFocus}
onBlur={this.onBlur}
onInput={this.onChange}
defaultValue={value}
ref={inputElement => this.inputElement = inputElement}
/>
)
}
onChange = (e) => {
super.onChange(e)
this.onFocus(e)
}
onFocus = () => {
if (this.inputElement.value)
return
setTimeout(() => {
this.inputElement.value = ' '
this.textMaskInputElement.update()
}, 0)
}
onBlur = () => {
if (this.props.value)
return
setTimeout(() => {
this.inputElement.value = ''
this.textMaskInputElement.update()
}, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment