Skip to content

Commit

Permalink
fix: make InputItem clear icon behave properly in Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
jadertao committed Jan 3, 2019
1 parent 0430de8 commit ce113ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
1 change: 1 addition & 0 deletions components/input-item/demo/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class BasicInputItemExample extends React.Component<any, any> {
automaticallyAdjustContentInsets={false}
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
keyboardShouldPersistTaps={'handled'}
>
<List renderHeader={'基本'}>
<InputItem
Expand Down
50 changes: 38 additions & 12 deletions components/input-item/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import React from 'react';
import { GestureResponderEvent, Platform, StyleSheet, Text, TextInputProperties, TouchableOpacity, TouchableWithoutFeedback, View } from 'react-native';
import {
GestureResponderEvent,
Platform,
StyleSheet,
Text,
TextInputProperties,
TouchableOpacity,
TouchableWithoutFeedback,
View,
} from 'react-native';
import { Omit } from 'utility-types';
import Icon from '../icon';
import { WithTheme, WithThemeStyles } from '../style';
Expand All @@ -24,6 +33,10 @@ export interface InputItemProps
disabled?: boolean;
}

interface InputItemState {
focus: boolean;
}

const noop = () => {};

function normalizeValue(value?: string) {
Expand All @@ -33,7 +46,10 @@ function normalizeValue(value?: string) {
return value;
}

export default class InputItem extends React.Component<InputItemProps, any> {
export default class InputItem extends React.Component<
InputItemProps,
InputItemState
> {
static defaultProps = {
type: 'text',
editable: true,
Expand All @@ -51,6 +67,9 @@ export default class InputItem extends React.Component<InputItemProps, any> {
last: false,
};

state: InputItemState = {
focus: false,
};
inputRef: Input | null;

onChange = (text: string) => {
Expand Down Expand Up @@ -84,15 +103,19 @@ export default class InputItem extends React.Component<InputItemProps, any> {
};

onInputBlur = () => {
if (this.props.onBlur) {
this.props.onBlur(this.props.value);
}
this.setState({ focus: false }, () => {
if (this.props.onBlur) {
this.props.onBlur(this.props.value);
}
});
};

onInputFocus = () => {
if (this.props.onFocus) {
this.props.onFocus(this.props.value);
}
this.setState({ focus: true }, () => {
if (this.props.onFocus) {
this.props.onFocus(this.props.value);
}
});
};

onInputClear = () => {
Expand All @@ -110,6 +133,7 @@ export default class InputItem extends React.Component<InputItemProps, any> {
};

render() {
const android = Platform.OS === 'android';
const {
type,
editable,
Expand All @@ -125,8 +149,8 @@ export default class InputItem extends React.Component<InputItemProps, any> {
disabled,
...restProps
} = this.props;
const { focus } = this.state;
const { value, defaultValue, style } = restProps;

let valueProps: any;
if ('value' in this.props) {
valueProps = {
Expand Down Expand Up @@ -203,21 +227,23 @@ export default class InputItem extends React.Component<InputItemProps, any> {
s.input,
error ? s.inputErrorColor : null,
disabledStyle,
// 支持自定义样式
restProps.style || null,
]}
keyboardType={keyboardType}
onChange={event => this.onChange(event.nativeEvent.text)}
secureTextEntry={type === 'password'}
onBlur={this.onInputBlur}
onFocus={this.onInputFocus}
/>
{/* 只在有 value 的 受控模式 下展示 自定义的 安卓 clear 按钮 */}
{editable && clear && value && Platform.OS === 'android' ? (
{/* 只在有 value 的受控模式下且在编辑状态时展示自定义的安卓 clear 按钮 */}
{editable && clear && value && focus && android ? (
<TouchableOpacity
style={[s.clear]}
onPress={this.onInputClear}
hitSlop={{ top: 5, left: 5, bottom: 5, right: 5 }}
>
<Icon name="close" />
<Icon name="close" color={android ? 'white' : undefined} />
</TouchableOpacity>
) : null}
{extra ? (
Expand Down
2 changes: 1 addition & 1 deletion components/input-item/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ subtitle: 文本输入
| placeholder | placeholder | String | '' |
| editable | 是否可编辑 | bool | true |
| disabled | 是否禁用 | bool | true |
| clear | 是否带清除功能(仅`editable``true`,`disabled``false`才生效) | bool | false |
| clear | 是否带清除功能(仅`editable``true`,`disabled``false`才生效)。在 Android 中, 处于编辑状态(focus)时 icon 才会出现, 且此组件被`ScrollView`包裹时, 设置`ScrollView``keyboardShouldPersistTaps`属性为`handled``always`时, icon才会正确响应点击事件 | bool | false |
| maxLength | 最大长度 | number ||
| onChange | change 事件触发的回调函数 | (val: string): void | - |
| onBlur | blur 事件触发的回调函数 | (val: string): void | - |
Expand Down

0 comments on commit ce113ad

Please sign in to comment.