Skip to content

Commit 8b013df

Browse files
authored
Merge pull request #265 from TRA-Tech/fix/handle-textinput-label-press
fix: handle textinput label press event
2 parents f90424e + 3b17cf7 commit 8b013df

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

src/components/TextInput/TextInput.tsx

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,31 @@ import useComponentTheme from '../../core/hooks/useComponentTheme';
99
import type { FCCWD, TextInputProps } from '../../types';
1010
import { applyDefaults } from '../../core/KitraProvider';
1111

12-
const AnimatedTextInput = Animated.createAnimatedComponent(RNTextInput);
12+
type BaseTextInputProps = RNTextInputProps & {
13+
inputRef?: React.Ref<RNTextInput>;
14+
};
15+
16+
const BaseTextInput = React.forwardRef<RNTextInput, BaseTextInputProps>(({ inputRef, ...props }, forwardedRef) => {
17+
const setRefs = (node: RNTextInput | null) => {
18+
if (typeof forwardedRef === 'function') {
19+
forwardedRef(node);
20+
} else if (forwardedRef) {
21+
(forwardedRef as { current: RNTextInput | null }).current = node;
22+
}
23+
24+
if (typeof inputRef === 'function') {
25+
inputRef(node);
26+
} else if (inputRef) {
27+
(inputRef as { current: RNTextInput | null }).current = node;
28+
}
29+
};
30+
31+
return <RNTextInput ref={setRefs} {...props} />;
32+
});
33+
34+
35+
const AnimatedTextInput = Animated.createAnimatedComponent(BaseTextInput);
36+
1337
const TextInput: FCCWD<TextInputProps & RNTextInputProps> = (
1438
{ inputStyle,
1539
editable = true,
@@ -229,6 +253,9 @@ const TextInput: FCCWD<TextInputProps & RNTextInputProps> = (
229253
}
230254
}, [props?.value]);
231255

256+
257+
258+
232259
return (
233260
<View style={[containerStyle]}>
234261
<Animated.View style={[{
@@ -282,7 +309,7 @@ const TextInput: FCCWD<TextInputProps & RNTextInputProps> = (
282309
)}
283310

284311
<AnimatedTextInput
285-
ref={inputRef}
312+
inputRef={inputRef}
286313
editable={editable}
287314
style={[{
288315
marginTop: (variant === 'filled' && label) ? fontStyles[size].lineHeight / 2 : 0,
@@ -311,13 +338,15 @@ const TextInput: FCCWD<TextInputProps & RNTextInputProps> = (
311338
]}
312339
>
313340
<TouchableOpacity
314-
onPress={() => inputRef.current?.focus()}
341+
onPress={() => {
342+
inputRef.current?.focus();
343+
}}
315344
activeOpacity={0.9}
316345
onLayout={event => setLabelLayout({ width: event.nativeEvent.layout.width,
317346
height: event.nativeEvent.layout.height })}
318347
>
319348
<Animated.Text style={[{ fontFamily: labelStyles[size].default.fontFamily },
320-
labelStyle, labelFontAnimation, { color: statusTheme.label }]}
349+
labelStyle, labelFontAnimation, { color: statusTheme.label }]}
321350
>
322351
{label}
323352
</Animated.Text>

0 commit comments

Comments
 (0)