-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.js
More file actions
152 lines (140 loc) · 3.57 KB
/
Copy pathInput.js
File metadata and controls
152 lines (140 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useState } from 'react';
import {
View,
Text,
TextInput,
StyleSheet,
Pressable,
} from 'react-native';
import { Controller } from 'react-hook-form';
import { MicIcon, EyeIcon, EyeOffIcon } from '@components/Icons';
import { startSpeechToText } from 'react-native-voice-to-text';
import Toast from 'react-native-toast-message';
export default function Input({
control,
name,
label,
rules = {},
required = false,
handleChange,
password = false,
type,
voice = true, // 👈 default enabled
}) {
const [hidePassword, setHidePassword] = useState(password);
const [listening, setListening] = useState(false);
const keyboardType = type === 'number' ? 'numeric' : 'default';
const handleVoiceInput = async (onChange) => {
try {
setListening(true);
const audioText = await startSpeechToText();
if (audioText && typeof audioText === 'string') {
onChange(audioText);
handleChange && handleChange(audioText);
}
} catch (error) {
console.log('Voice error:', error);
Toast.show({
type: 'error',
text1: 'Voice input failed',
});
} finally {
setListening(false);
}
};
return (
<Controller
control={control}
name={name}
rules={rules}
render={({ field: { value, onChange }, fieldState: { error } }) => (
<View style={styles.container}>
{label && (
<Text style={styles.label}>
{label}
{required && <Text style={styles.asterisk}> *</Text>}
</Text>
)}
<View style={styles.inputWrapper}>
<TextInput
value={value}
onChangeText={(text) => {
onChange(text);
handleChange && handleChange(text);
}}
style={styles.input}
secureTextEntry={hidePassword}
placeholder={label}
keyboardType={keyboardType}
inputMode={type === 'number' ? 'numeric' : 'text'}
/>
{/* PASSWORD EYE ICON */}
{password && (
<Pressable
onPress={() => setHidePassword(!hidePassword)}
style={styles.icon}
>
{hidePassword ? <EyeOffIcon /> : <EyeIcon />}
</Pressable>
)}
{/* VOICE ICON */}
{!password && voice && (
<Pressable
onPress={() => handleVoiceInput(onChange)}
disabled={listening}
style={styles.icon}
>
<MicIcon
color={listening ? '#d32f2f' : '#666'}
/>
</Pressable>
)}
</View>
{listening && (
<Text style={styles.listeningText}>🎙 Listening…</Text>
)}
{error && <Text style={styles.error}>{error.message}</Text>}
</View>
)}
/>
);
}
const styles = StyleSheet.create({
container: {
marginBottom: 16,
},
label: {
marginBottom: 6,
fontSize: 14,
fontWeight: '500',
color: '#333',
},
inputWrapper: {
flexDirection: 'row',
alignItems: 'center',
borderRadius: 8,
borderWidth: 1,
borderColor: '#ddd',
paddingHorizontal: 12,
backgroundColor: '#fff',
},
input: {
flex: 1,
height: 44,
fontSize: 15,
color: '#000',
},
icon: {
paddingLeft: 8,
},
error: {
marginTop: 4,
color: '#d32f2f',
fontSize: 12,
},
listeningText: {
marginTop: 4,
fontSize: 12,
color: '#1976d2',
},
});