Skip to content

Commit 663618d

Browse files
committed
fix(SearchInputBar):修复loading中能取消搜索
1 parent 293a1ab commit 663618d

File tree

2 files changed

+17
-8
lines changed
  • example/examples/src/routes/SearchInputBar
  • packages/core/src/SearchInputBar

2 files changed

+17
-8
lines changed

example/examples/src/routes/SearchInputBar/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export default class Index extends Component<IndexProps, IndexState> {
4949
placeholder="请输入搜索关键字..."
5050
actionName="搜一下"
5151
value={this.state.value}
52+
onSearch={() => {
53+
this.setState({loading: true}, () => {
54+
setTimeout(() => {
55+
this.setState({loading: false});
56+
}, 3000);
57+
});
58+
}}
5259
touchProps={{
5360
onPress: () => {
5461
console.log('object', this.ref);
@@ -67,12 +74,12 @@ export default class Index extends Component<IndexProps, IndexState> {
6774
onClear={() => this.onClear('value2')}
6875
value={this.state.value2}
6976
placeholder="请输入搜索关键字"
77+
showActionButton={true}
7078
searchRender={
7179
<View style={styles.search}>
7280
<Text>搜索</Text>
7381
</View>
7482
}
75-
showActionButton
7683
touchProps={{
7784
onPress() {
7885
Toast.info('你点击了搜一下', 2, 'info');

packages/core/src/SearchInputBar/index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export interface SearchInputBarProps extends TextInputProps {
3535
/** 点击清除图标时触发事件 */
3636
onClear?: Function;
3737
/** 自定义搜索 */
38-
searchRender?: JSX.Element;
38+
searchRender?: React.ReactNode;
3939
/** 输入框TextInput样式 */
4040
inputStyle?: TextStyle;
4141
/** loading加载 */
@@ -49,6 +49,7 @@ interface SearchInputBarState {
4949

5050
export default class SearchInputBar extends React.Component<SearchInputBarProps, SearchInputBarState> {
5151
public inputRef = React.createRef<TextInput>();
52+
5253
constructor(props: SearchInputBarProps) {
5354
super(props);
5455
this.state = {
@@ -59,7 +60,7 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
5960
needFocus = (type: 'search' | 'close' | 'actived') => {
6061
if (type === 'close') {
6162
this.props.onClear?.();
62-
} else if (type === 'search' && this.props.value) {
63+
} else if (type === 'search') {
6364
this.props.onSearch?.();
6465
return;
6566
}
@@ -71,7 +72,7 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
7172

7273
// 右侧搜索
7374
renderSearch = () => {
74-
const { showActionButton, searchRender, touchProps, actionName = '搜索', loading } = this.props;
75+
const { showActionButton, searchRender, touchProps, actionName = '搜索' } = this.props;
7576
const { showIcon } = this.state;
7677
if (showActionButton || showIcon) {
7778
return searchRender ? (
@@ -94,14 +95,15 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
9495
containerStyle,
9596
searchIcon,
9697
closeIcon,
97-
loading,
98+
loading = false,
9899
...other
99100
} = this.props;
101+
100102
return (
101-
<Loader loading={loading ? loading : false} rounded={5} maskColor="transparent">
103+
<Loader loading={loading} rounded={5} maskColor="transparent">
102104
<View style={[styles.centerFlex]}>
103105
<View style={StyleSheet.flatten([styles.searchContainer, styles.centerFlex, containerStyle])}>
104-
<TouchableOpacity style={{}} onPress={() => this.needFocus('search')}>
106+
<TouchableOpacity onPress={() => this.needFocus('search')}>
105107
<Icon name="search" size={14} color={colors.colorsPalette.grey40} height={'100%'} {...searchIcon} />
106108
</TouchableOpacity>
107109
<TextInput
@@ -117,7 +119,7 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
117119
other?.onFocus?.(e);
118120
}}
119121
onBlur={(e: NativeSyntheticEvent<TextInputFocusEventData>) => {
120-
if (showActionButton !== null && !value) {
122+
if (showActionButton !== null && !value && !loading) {
121123
this.setState({ showIcon: false });
122124
}
123125
other?.onBlur?.(e);

0 commit comments

Comments
 (0)