2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as ReactDOM from 'react-dom';
|
2017-03-28 15:10:07 +08:00
|
|
|
|
2017-11-01 11:47:22 +08:00
|
|
|
export interface InputElementProps {
|
|
|
|
children: React.ReactElement<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class InputElement extends React.Component<InputElementProps, any> {
|
2017-03-28 15:10:07 +08:00
|
|
|
private ele: HTMLInputElement;
|
|
|
|
|
|
|
|
focus = () => {
|
2017-11-17 14:38:54 +08:00
|
|
|
this.ele.focus ? this.ele.focus() : (ReactDOM.findDOMNode(this.ele) as HTMLInputElement).focus();
|
2017-03-28 15:10:07 +08:00
|
|
|
}
|
|
|
|
blur = () => {
|
2017-11-17 14:38:54 +08:00
|
|
|
this.ele.blur ? this.ele.blur() : (ReactDOM.findDOMNode(this.ele) as HTMLInputElement).blur();
|
2017-03-28 15:10:07 +08:00
|
|
|
}
|
2017-08-17 13:09:18 +08:00
|
|
|
saveRef = (ele: HTMLInputElement) => {
|
|
|
|
this.ele = ele;
|
2017-11-01 11:47:22 +08:00
|
|
|
const { ref: childRef } = this.props.children as any;
|
2017-08-17 13:09:18 +08:00
|
|
|
if (typeof childRef === 'function') {
|
|
|
|
childRef(ele);
|
|
|
|
}
|
|
|
|
}
|
2017-03-28 15:10:07 +08:00
|
|
|
render() {
|
|
|
|
return React.cloneElement(this.props.children, {
|
|
|
|
...this.props,
|
2017-08-17 13:09:18 +08:00
|
|
|
ref: this.saveRef,
|
2017-03-28 15:10:07 +08:00
|
|
|
}, null);
|
|
|
|
}
|
|
|
|
}
|