Skip to content

Commit

Permalink
Allow user to define a ref function on the internal input
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalatero committed Feb 8, 2018
1 parent fe93c48 commit 52dee0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -59,5 +59,14 @@ class PhoneInput extends React.Component {
}
```

### `userRef` : `function`

Attaches a `ref` handler to the `<input>` tag. In case you need to save a reference to manage focus, etc.

## Example
```jsx
<InputMask userRef={ref => (this.input = ref)} />
```

## Thanks
Thanks to [BrowserStack](https://www.browserstack.com/) for the help with testing on real devices
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -514,7 +514,15 @@ class InputElement extends React.Component {
}

render() {
var { mask, alwaysShowMask, maskChar, formatChars, ...props } = this.props;
var { mask, alwaysShowMask, maskChar, formatChars, userRef, ...props } = this.props;

var assignRef = ref => {
if (typeof userRef === 'function') {
userRef(ref);
}

this.input = ref;
}

if (this.maskOptions.mask) {
if (!props.disabled && !props.readOnly) {
Expand All @@ -529,7 +537,7 @@ class InputElement extends React.Component {
}
}

return <input ref={ref => this.input = ref} {...props} onFocus={this.onFocus} onBlur={this.onBlur} />;
return <input ref={assignRef} {...props} onFocus={this.onFocus} onBlur={this.onBlur} />;
}
}

Expand Down

0 comments on commit 52dee0d

Please sign in to comment.