Skip to content

Commit 2849be7

Browse files
committed
Pressing on dismiss (x) only fully works the second time - a very complex fix
1 parent 3004293 commit 2849be7

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

lib/components/Keyboard/KeyboardInput/CustomKeyboardView/CustomKeyboardView.android.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ export default class CustomKeyboardView extends CustomKeyboardViewBase<CustomKey
1010
static displayName = 'IGNORE';
1111

1212
async componentDidUpdate(prevProps: CustomKeyboardViewBaseProps) {
13-
const {component, inputRef} = this.props;
13+
const {component, inputRef, shouldFocus, onKeyboardDismiss} = this.props;
1414

1515
if (prevProps.component !== component) {
16+
1617
if (!component) {
17-
await TextInputKeyboardManager.reset();
18-
if (inputRef?.current) {
19-
inputRef.current.focus?.();
20-
} else {
21-
inputRef?.focus?.();
18+
// await TextInputKeyboardManager.reset();
19+
if (shouldFocus) {
20+
if (inputRef?.current) {
21+
inputRef.current.focus?.();
22+
} else {
23+
inputRef?.focus?.();
24+
}
2225
}
2326
} else {
2427
Keyboard.dismiss();
2528
}
29+
30+
onKeyboardDismiss?.();
2631
}
2732

2833
super.componentDidUpdate(prevProps);

lib/components/Keyboard/KeyboardInput/CustomKeyboardViewBase.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type CustomKeyboardViewBaseProps = {
66
inputRef?: any;
77
initialProps?: any;
88
keyboardHeight?: number;
9+
onKeyboardDismiss?: () => void;
10+
shouldFocus?: boolean;
911
component?: string;
1012
onItemSelected?: (component?: string, args?: any) => void;
1113
onRequestShowKeyboard?: (keyboardId: string) => void;

lib/components/Keyboard/KeyboardInput/KeyboardAccessoryView.tsx

+21-3
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,13 @@ class KeyboardAccessoryView extends Component<KeyboardAccessoryViewProps> {
7373
scrollBehavior: KeyboardTrackingView.scrollBehaviors.FIXED_OFFSET
7474
};
7575

76+
static currentId = 0;
77+
7678
// TODO: fix
7779
customInputControllerEventsSubscriber: any;
7880
trackingViewRef: any;
7981
subscription: any;
82+
id: number = ++KeyboardAccessoryView.currentId;
8083

8184
constructor(props: KeyboardAccessoryViewProps) {
8285
super(props);
@@ -92,7 +95,8 @@ class KeyboardAccessoryView extends Component<KeyboardAccessoryViewProps> {
9295
}
9396

9497
state = {
95-
keyboardHeight: 0
98+
keyboardHeight: 0,
99+
shouldFocus: true
96100
};
97101

98102
componentWillUnmount() {
@@ -176,8 +180,16 @@ class KeyboardAccessoryView extends Component<KeyboardAccessoryViewProps> {
176180
this.setState({keyboardHeight});
177181
};
178182

183+
onDismiss = () => {
184+
this.setState({shouldFocus: false});
185+
};
186+
187+
onKeyboardDismiss = () => {
188+
this.setState({shouldFocus: true});
189+
};
190+
179191
render() {
180-
const {keyboardHeight} = this.state;
192+
const {keyboardHeight, shouldFocus} = this.state;
181193
const {
182194
renderContent,
183195
kbInputRef,
@@ -196,10 +208,16 @@ class KeyboardAccessoryView extends Component<KeyboardAccessoryViewProps> {
196208
style={styles.trackingToolbarContainer}
197209
onLayout={this.onContainerComponentHeightChanged}
198210
>
199-
<KeyboardHeightListener onKeyboardHeightChange={this.onKeyboardHeightChange}/>
211+
<KeyboardHeightListener
212+
id={`${this.id}`}
213+
onDismiss={this.onDismiss}
214+
onKeyboardHeightChange={this.onKeyboardHeightChange}
215+
/>
200216
<>{renderContent?.()}</>
201217
<CustomKeyboardView
202218
keyboardHeight={keyboardHeight}
219+
shouldFocus={shouldFocus}
220+
onKeyboardDismiss={this.onKeyboardDismiss}
203221
inputRef={kbInputRef}
204222
component={kbComponent}
205223
initialProps={this.processInitialProps()}

lib/components/Keyboard/KeyboardInput/utils/KeyboardUtils.ts

+36-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,38 @@ const DEFAULT_KEYBOARD_HEIGHT = IS_IOS ? 216 : 312; // TODO: verify this value f
77

88
export default class KeyboardUtils {
99
static displayName = 'KeyboardUtils';
10+
private static listeners: {[key: string]: () => void} = {};
11+
12+
private static addListener = (id: string, onDismiss?: () => void) => {
13+
if (id && onDismiss && !KeyboardUtils.listeners[id]) {
14+
KeyboardUtils.listeners[id] = onDismiss;
15+
}
16+
};
17+
18+
private static removeListener = (id: string) => {
19+
if (id && KeyboardUtils.listeners[id]) {
20+
delete KeyboardUtils.listeners[id];
21+
}
22+
};
23+
1024
/**
1125
* Used to dismiss (close) the keyboard.
1226
*/
1327
static dismiss = () => {
1428
Keyboard.dismiss();
1529
TextInputKeyboardManager.dismissKeyboard();
30+
Object.keys(KeyboardUtils.listeners).forEach(key => {
31+
KeyboardUtils.listeners[key]();
32+
});
1633
};
1734
}
1835

19-
const useKeyboardHeight = () => {
36+
interface KeyboardHeightProps {
37+
id: string;
38+
onDismiss: () => void;
39+
}
40+
41+
const useKeyboardHeight = ({id, onDismiss}: KeyboardHeightProps) => {
2042
const [isInitialized, setIsInitialized] = useState(false);
2143
const [keyboardHeight, setKeyboardHeight] = useState(DEFAULT_KEYBOARD_HEIGHT);
2244
const [isVisible, setIsVisible] = useState(false);
@@ -37,9 +59,13 @@ const useKeyboardHeight = () => {
3759
useEffect(() => {
3860
const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', keyboardDidShow);
3961
const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', keyboardDidHide);
62+
// @ts-ignore
63+
KeyboardUtils.addListener(id, onDismiss);
4064
return () => {
4165
keyboardDidShowListener.remove();
4266
keyboardDidHideListener.remove();
67+
// @ts-ignore
68+
KeyboardUtils.removeListener(id);
4369
};
4470
// eslint-disable-next-line react-hooks/exhaustive-deps
4571
}, []);
@@ -48,12 +74,19 @@ const useKeyboardHeight = () => {
4874
};
4975

5076
export interface KeyboardHeightListenerProps {
77+
id: string;
78+
onDismiss: () => void;
5179
onKeyboardHeightChange?: (height: number) => void;
5280
onKeyboardVisibilityChange?: (isKeyboardVisible: boolean) => void;
5381
}
5482

55-
const KeyboardHeightListener = ({onKeyboardHeightChange, onKeyboardVisibilityChange}: KeyboardHeightListenerProps) => {
56-
const {keyboardHeight, isKeyboardVisible} = useKeyboardHeight();
83+
const KeyboardHeightListener = ({
84+
id,
85+
onDismiss,
86+
onKeyboardHeightChange,
87+
onKeyboardVisibilityChange
88+
}: KeyboardHeightListenerProps) => {
89+
const {keyboardHeight, isKeyboardVisible} = useKeyboardHeight({id, onDismiss});
5790

5891
useEffect(() => {
5992
onKeyboardHeightChange?.(keyboardHeight);

0 commit comments

Comments
 (0)