Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: split input from input-item #1225

Merged
merged 2 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions components/input-item/Input.web.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React from 'react';
import omit from 'omit.js';

class Input extends React.Component<any, any> {
debounceTimeout: any;
scrollIntoViewTimeout: any;

constructor(props) {
super(props);
this.state = {
focused: props.focused || false,
};
}

componentWillReceiveProps(nextProps) {
if ('focused' in nextProps) {
this.setState({
focused: nextProps.focused,
});
}
}

componentDidMount() {
if ((this.props.autoFocus || this.state.focused) && navigator.userAgent.indexOf('AlipayClient') > 0) {
(this.refs as any).input.focus();
}
}

componentWillUnmount() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = null;
}
if (this.scrollIntoViewTimeout) {
clearTimeout(this.scrollIntoViewTimeout);
this.scrollIntoViewTimeout = null;
}
}

componentDidUpdate() {
if (this.state.focused) {
(this.refs as any).input.focus();
}
}

onInputBlur = (e) => {
this.debounceTimeout = setTimeout(() => {
this.setState({
focus: false,
});
}, 200);
if (!('focused' in this.props)) {
this.setState({
focused: false,
});
}
const value = e.target.value;
if (this.props.onBlur) {
this.props.onBlur(value);
}
}

onInputFocus = (e) => {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = null;
}
if (!('focused' in this.props)) {
this.setState({
focused: true,
});
}

this.setState({
focus: true,
});

const value = e.target.value;
if (this.props.onFocus) {
this.props.onFocus(value);
}
if (document.activeElement.tagName.toLowerCase() === 'input') {
this.scrollIntoViewTimeout = setTimeout(() => {
try {
(document.activeElement as any).scrollIntoViewIfNeeded();
} catch (e) { }
}, 100);
}
}

render() {
const otherProps = omit(this.props, ['onBlur', 'onFocus', 'focused', 'autoFocus']);
return (
<input ref="input" onBlur={this.onInputBlur} onFocus={this.onInputFocus} {...otherProps} />
);
}
}

export default Input;
92 changes: 6 additions & 86 deletions components/input-item/index.web.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* tslint:disable:jsx-no-multiline-js */
import React from 'react';
import classNames from 'classnames';
import InputItemProps from './PropsType';
import omit from 'omit.js';
import InputItemProps from './PropsType';
import Input from './Input.web';

function noop() {
}
function noop() { }

function fixControlledValue(value) {
if (typeof value === 'undefined' || value === null) {
Expand Down Expand Up @@ -34,13 +34,9 @@ class InputItem extends React.Component<InputItemProps, any> {
updatePlaceholder: false,
};

debounceTimeout: any;
scrollIntoViewTimeout: any;

constructor(props) {
super(props);
this.state = {
focused: props.focused || false,
placeholder: props.placeholder,
};
}
Expand All @@ -51,34 +47,6 @@ class InputItem extends React.Component<InputItemProps, any> {
placeholder: nextProps.placeholder,
});
}
if ('focused' in nextProps) {
this.setState({
focused: nextProps.focused,
});
}
}

componentWillUnmount() {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = null;
}
if (this.scrollIntoViewTimeout) {
clearTimeout(this.scrollIntoViewTimeout);
this.scrollIntoViewTimeout = null;
}
}

componentDidMount() {
if ((this.props.autoFocus || this.state.focused) && navigator.userAgent.indexOf('AlipayClient') > 0) {
(this.refs as any).input.focus();
}
}

componentDidUpdate() {
if (this.state.focused) {
(this.refs as any).input.focus();
}
}

onInputChange = (e) => {
Expand Down Expand Up @@ -113,51 +81,6 @@ class InputItem extends React.Component<InputItemProps, any> {
}
}

onInputBlur = (e) => {
this.debounceTimeout = setTimeout(() => {
this.setState({
focus: false,
});
}, 200);
if (!('focused' in this.props)) {
this.setState({
focused: false,
});
}
const value = e.target.value;
if (this.props.onBlur) {
this.props.onBlur(value);
}
}

onInputFocus = (e) => {
if (this.debounceTimeout) {
clearTimeout(this.debounceTimeout);
this.debounceTimeout = null;
}
if (!('focused' in this.props)) {
this.setState({
focused: true,
});
}

this.setState({
focus: true,
});

const value = e.target.value;
if (this.props.onFocus) {
this.props.onFocus(value);
}
if (document.activeElement.tagName.toLowerCase() === 'input') {
this.scrollIntoViewTimeout = setTimeout(() => {
try {
(document.activeElement as any).scrollIntoViewIfNeeded();
} catch (e) { }
}, 100);
}
}

onExtraClick = (e) => {
if (this.props.onExtraClick) {
this.props.onExtraClick(e);
Expand Down Expand Up @@ -189,9 +112,9 @@ class InputItem extends React.Component<InputItemProps, any> {
} = this.props;

// note: remove `placeholderTextColor` prop for rn TextInput supports placeholderTextColor
const otherProps = omit(this.props, ['prefixCls', 'prefixListCls', 'editable', 'style', 'focused',
const otherProps = omit(this.props, ['prefixCls', 'prefixListCls', 'editable', 'style',
'clear', 'children', 'error', 'className', 'extra', 'labelNumber', 'onExtraClick', 'onErrorClick',
'updatePlaceholder', 'placeholderTextColor', 'autoFocus', 'type',
'updatePlaceholder', 'placeholderTextColor', 'type',
]);

const { placeholder, focus } = this.state;
Expand Down Expand Up @@ -250,8 +173,7 @@ class InputItem extends React.Component<InputItemProps, any> {
<div className={wrapCls} style={style}>
{children ? (<div className={labelCls}>{children}</div>) : null}
<div className={controlCls}>
<input
ref="input"
<Input
{...patternProps}
{...otherProps}
{...valueProps}
Expand All @@ -260,8 +182,6 @@ class InputItem extends React.Component<InputItemProps, any> {
name={name}
placeholder={placeholder}
onChange={this.onInputChange}
onBlur={this.onInputBlur}
onFocus={this.onInputFocus}
readOnly={!editable}
disabled={disabled}
/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#189行clear无法触发。

Expand Down