@klyftig/mobile-ui 1.3.7 → 1.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useState, useCallback, memo } from 'react';
|
|
1
|
+
import React, { useState, useCallback, useEffect, useRef, memo } from 'react';
|
|
2
2
|
import { View, TextInput as RNTextInput, Text, TouchableOpacity, StyleSheet } from 'react-native';
|
|
3
3
|
import { MaterialCommunityIcons as Icon } from '@expo/vector-icons';
|
|
4
4
|
import { useTheme } from '../hooks/useTheme';
|
|
@@ -28,6 +28,13 @@ interface GlassInputProps {
|
|
|
28
28
|
selectTextOnFocus?: boolean;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
// When a single-line input is not focused, anchor the selection to the start so
|
|
32
|
+
// iOS/Android scroll a value longer than the field to reveal its BEGINNING
|
|
33
|
+
// ("Carrot - Cream Sauce…") instead of its tail ("…Sauce - pistachio nuts").
|
|
34
|
+
// Only applied while unfocused — during editing the selection stays uncontrolled
|
|
35
|
+
// so the caret follows taps and typing normally.
|
|
36
|
+
const SELECTION_START = { start: 0, end: 0 } as const;
|
|
37
|
+
|
|
31
38
|
const GlassInputComponent: React.FC<GlassInputProps> = ({
|
|
32
39
|
label,
|
|
33
40
|
value,
|
|
@@ -53,6 +60,21 @@ const GlassInputComponent: React.FC<GlassInputProps> = ({
|
|
|
53
60
|
const { colors } = useTheme();
|
|
54
61
|
const [isSecure, setIsSecure] = useState(secureTextEntry);
|
|
55
62
|
const [isFocused, setIsFocused] = useState(false);
|
|
63
|
+
const inputRef = useRef<RNTextInput>(null);
|
|
64
|
+
|
|
65
|
+
// Reinforces the `selection` prop below: on Android, setting `selection`
|
|
66
|
+
// on the SAME render as a `value` that's longer than the field's width is
|
|
67
|
+
// often ignored — the native EditText hasn't measured/laid out the text
|
|
68
|
+
// yet, so it doesn't know where to scroll to reveal position 0. Re-asserting
|
|
69
|
+
// via setNativeProps a tick after layout commits (not the same frame)
|
|
70
|
+
// reliably forces the scroll where the declarative prop alone doesn't.
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (isFocused || multiline) return;
|
|
73
|
+
const id = setTimeout(() => {
|
|
74
|
+
inputRef.current?.setNativeProps({ selection: SELECTION_START });
|
|
75
|
+
}, 0);
|
|
76
|
+
return () => clearTimeout(id);
|
|
77
|
+
}, [value, isFocused, multiline]);
|
|
56
78
|
|
|
57
79
|
const handleFocus = useCallback(() => {
|
|
58
80
|
setIsFocused(true);
|
|
@@ -85,6 +107,7 @@ const GlassInputComponent: React.FC<GlassInputProps> = ({
|
|
|
85
107
|
<Icon name={icon} size={20} color="#9B9B9B" style={styles.leftIcon} />
|
|
86
108
|
)}
|
|
87
109
|
<RNTextInput
|
|
110
|
+
ref={inputRef}
|
|
88
111
|
value={value}
|
|
89
112
|
onChangeText={onChangeText}
|
|
90
113
|
placeholder={placeholder}
|
|
@@ -98,6 +121,7 @@ const GlassInputComponent: React.FC<GlassInputProps> = ({
|
|
|
98
121
|
onFocus={handleFocus}
|
|
99
122
|
onBlur={handleBlur}
|
|
100
123
|
selectTextOnFocus={selectTextOnFocus}
|
|
124
|
+
selection={!isFocused && !multiline ? SELECTION_START : undefined}
|
|
101
125
|
multiline={multiline}
|
|
102
126
|
numberOfLines={numberOfLines}
|
|
103
127
|
textAlignVertical={multiline ? 'top' : undefined}
|