Skip to content

Commit 06d8d1f

Browse files
committed
chore(SearchInputBar): Fix 失焦触发事件问题 Updater README
1 parent c750ff8 commit 06d8d1f

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ export default class Index extends Component<IndexProps, IndexState> {
3333
const description = route.params.description;
3434
const title = route.params.title;
3535
return (
36-
<Container>
36+
<Container keyboardShouldPersistTaps="always">
3737
<Layout>
3838
<Header title={title} description={description} />
39-
<Body>
39+
<Body keyboardShouldPersistTaps="always">
4040
<View style={styles.divider} />
4141
<SearchInputBar
4242
ref={this.ref}

packages/core/src/SearchInputBar/README.md

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ function Demo() {
5454
}
5555
```
5656

57+
## 键盘不收起来
58+
59+
**注意: 每层```ScrollView```都要写```keyboardShouldPersistTaps="always"```**
60+
61+
```jsx
62+
import { Fragment, useState } from 'react';
63+
import { SearchInputBar } from '@uiw/react-native';
64+
function Demo() {
65+
const [value, setValue] = useState('')
66+
return (
67+
<ScrollView keyboardShouldPersistTaps="always" >
68+
<SearchInputBar
69+
onChangeText={setValue}
70+
onClear={()=>setValue('')}
71+
value={value}
72+
showActionButton
73+
button={{
74+
onPress() {
75+
// 点击搜索按钮触法
76+
}
77+
}}
78+
/>
79+
</ScrollView>
80+
);
81+
}
82+
```
83+
5784
## 获取输入框 Ref
5885

5986
```jsx
@@ -90,19 +117,25 @@ function Demo() {
90117
## Props
91118

92119
```ts
93-
import { TextInputProps } from 'react-native';
94-
import { ButtonProps } from '@uiw/react-native'
120+
import { TextInputProps,StyleProp,ViewStyle } from 'react-native';
121+
import { ButtonProps,IconsProps } from '@uiw/react-native'
95122

96123
export interface SearchInputBarProps extends TextInputProps {
97-
/** 点击清除按钮时触发事件 */
98-
onClear?: Function,
124+
/** 容器样式 */
125+
containerStyle?: StyleProp<ViewStyle>;
99126
/** 右侧按钮 */
100-
button?: ButtonProps
127+
button?: ButtonProps;
101128
/** 右侧按钮文案 */
102-
actionName?: string,
103-
/** 右侧按钮宽度默认70 */
104-
buttonWidth?: number,
105-
/** 是否一直显示右侧按钮 */
106-
showActionButton?: boolean
129+
actionName?: string;
130+
/** 是否一直显示右侧按钮 null = 永不显示 */
131+
showActionButton?: boolean | null;
132+
/** 搜索图标 */
133+
searchIcon?: IconsProps;
134+
/** 点击搜索图标时触发事件 */
135+
onSearch?: Function;
136+
/** 清除图标 */
137+
closeIcon?: IconsProps;
138+
/** 点击清除图标时触发事件 */
139+
onClear?: Function;
107140
}
108141
```

packages/core/src/SearchInputBar/RightButton.tsx

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/core/src/SearchInputBar/index.tsx

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import {
44
StyleSheet,
55
StyleProp,
66
ViewStyle,
7-
Text,
87
TouchableOpacity,
9-
Animated,
108
NativeSyntheticEvent,
119
TextInput,
1210
TextInputFocusEventData,
1311
TextInputProps,
1412
} from 'react-native';
1513
import Icon, { IconsProps } from '../Icon';
1614
import Button, { ButtonProps } from '../Button';
17-
import RightButton from './RightButton';
1815

1916
export interface SearchInputBarProps extends TextInputProps {
2017
/** 容器样式 */
@@ -38,8 +35,6 @@ export interface SearchInputBarProps extends TextInputProps {
3835
interface SearchInputBarState {
3936
// isShow close icon
4037
showIcon: boolean;
41-
// close style
42-
showIconLeft: number;
4338
}
4439

4540
export default class SearchInputBar extends React.Component<SearchInputBarProps, SearchInputBarState> {
@@ -48,20 +43,19 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
4843
super(props);
4944
this.state = {
5045
showIcon: false,
51-
showIconLeft: 0,
5246
};
5347
}
5448

5549
needFocus = (type: 'search' | 'close' | 'actived') => {
5650
if (type === 'close') {
5751
this.props.onClear?.();
58-
} else if (type === 'search') {
52+
} else if (type === 'search' && this.props.value) {
5953
this.props.onSearch?.();
54+
return;
6055
}
6156
if (type === 'actived') {
6257
this.setState({ showIcon: true });
6358
}
64-
console.log('object', type);
6559
this.inputRef.current && this.inputRef.current.focus();
6660
};
6761
render() {
@@ -80,11 +74,9 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
8074
return (
8175
<View style={[styles.centerFlex]}>
8276
<View style={StyleSheet.flatten([styles.searchContainer, styles.centerFlex, containerStyle])}>
83-
<View>
84-
<TouchableOpacity style={{}} onPress={() => this.needFocus('search')}>
85-
<Icon name="search" size={18} color="#999" height={'100%'} {...searchIcon} />
86-
</TouchableOpacity>
87-
</View>
77+
<TouchableOpacity style={{}} onPress={() => this.needFocus('search')}>
78+
<Icon name="search" size={18} color="#999" height={'100%'} {...searchIcon} />
79+
</TouchableOpacity>
8880
<TextInput
8981
{...other}
9082
value={value}
@@ -98,7 +90,9 @@ export default class SearchInputBar extends React.Component<SearchInputBarProps,
9890
other?.onFocus?.(e);
9991
}}
10092
onBlur={(e: NativeSyntheticEvent<TextInputFocusEventData>) => {
101-
this.setState({ showIcon: false });
93+
if (showActionButton !== null && !value) {
94+
this.setState({ showIcon: false });
95+
}
10296
other?.onBlur?.(e);
10397
}}
10498
/>
@@ -134,7 +128,7 @@ const styles = StyleSheet.create({
134128
marginRight: 10,
135129
},
136130
textInput: {
137-
height: 40,
131+
height: '100%',
138132
flex: 1,
139133
fontSize: 18,
140134
paddingHorizontal: 8,

0 commit comments

Comments
 (0)