मुझे रिएक्ट नेटिव में अगले इनपुट पर ध्यान केंद्रित करने में समस्या है। मैं पूरे ऐप में GeneralTextInput.tsx नामक केवल एक इनपुट का उपयोग करता हूं।
इस उदाहरण में मेरे पास 2 इनपुट हैं ==> 1.समूह का नाम, 2.समूह विवरण
इसलिए मैं इस घटक के लिए माता-पिता में कुछ सहारा देता हूं:
<View style={classes.formContainer}>
<Text style={classes.label}>{t("group.name-your-group")}</Text>
<GeneralTextInput
width={"100%"}
returnKeyType={"next"}
isDoneReference={false}
deleteIcon
startIcon={"account-multiple"}
bordered={true}
placeholder={t("form.placeholders.groupName")}
value={props.newGroupName}
onChange={(val: string) => {
props.setNewGroupName(val);
if (val.length > 25) {
props.setNewGroupNameError(t("form.validations.max-25-char"));
}
if (val.length <= 25) {
props.setNewGroupNameError(undefined);
}
}}
/>
<Text style={classes.label}>{t("group.describe-your-group")}</Text>
<GeneralTextInput
width={"100%"}
returnKeyType={"done"}
isDoneReference={true}
isDismissed={true}
startIcon={"text"}
bordered={true}
isMultiLine={true}
numberOfLines={3}
placeholder={t("form.placeholders.groupDescription")}
value={props.newGroupDescription}
onChange={(val: string) => {
props.setNewGroupDescription(val);
if (val.length > 30) {
props.setNewGroupDescriptionError(t("form.validations.max-30-char"));
}
if (val.length < 30) {
props.setNewGroupDescriptionError(undefined);
}
}}
/>
</View>
और यह मेरा GeneralTextInput.tsx है, मुझे रेफरी के रूप में इनपुट को क्या देना चाहिए और मुझे इस पर कैसे ध्यान केंद्रित करना चाहिए?
import * as React from "react";
import {
NativeSyntheticEvent,
Platform,
StyleProp,
TextInputFocusEventData,
TextStyle,
View,
ViewStyle,
TextInput,
ImageStyle,
Pressable,
} from "react-native";
import { makeStyles, IStyledComponent } from "../../assets/theme/installation";
import { IconButton, Text, useTheme } from "react-native-paper";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import FontAwesome5Icon from "react-native-vector-icons/FontAwesome5";
import { theme } from "../../assets/theme/DefaultTheme";
import { TouchableWithoutFeedback } from "react-native-gesture-handler";
export interface IGeneralTextInputProps
extends IStyledComponent<GeneralTextInputStyles> {
readonly value: string | undefined;
readonly placeholder?: string;
readonly onChange: (newValue: string) => void;
readonly onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
readonly isPassword?: boolean;
readonly autoCapitalize?: boolean;
readonly error?: string;
readonly startIcon?: string;
readonly startIconFA5?: string;
readonly endIcon?: string;
readonly deleteIcon?: boolean;
readonly disabled?: boolean;
readonly disabledInputText?: boolean;
readonly bordered?: boolean;
readonly isMultiLine?: boolean;
readonly width?: number | string;
readonly numberOfLines?: number;
readonly keyboardType?: string;
readonly isGratitude?: boolean;
readonly autoCorrect?: boolean;
readonly selectedMeasureUnit?: string;
readonly returnKeyType?: string;
readonly isDoneReference?: boolean;
readonly isDismissed?: boolean;
}
export const GeneralTextInput: React.FC<IGeneralTextInputProps> = (
props: IGeneralTextInputProps,
) => {
const classes = useStyles(props);
const { fonts, colors } = useTheme();
const [isPressed, setIsPressed] = React.useState(false);
const [isPasswordVisible, setPasswordVisible] = React.useState(false);
const groupNameRef = React.useRef<HTMLInputElement>(null);
const groupDescRef = React.useRef<HTMLInputElement>(null);
return (
<View style={classes.container}>
<TouchableWithoutFeedback>
<View style={classes.root}>
<TextInput
ref={() => (props.isDoneReference ? groupDescRef : groupNameRef)}
onSubmitEditing={() => {
groupDescRef.current?.focus();
}}
blurOnSubmit={props.isDoneReference ? true : false}
keyboardType={
props.keyboardType === "numpad" ? "numeric" : "default"
}
autoCorrect={props.autoCorrect}
multiline={props.isMultiLine}
numberOfLines={props.numberOfLines}
maxLength={props.isGratitude ? 300 : 50}
editable={!props.disabled}
onBlur={props.onBlur}
autoCapitalize={
props.autoCapitalize != undefined ? "words" : "none"
}
secureTextEntry={
props.isPassword == undefined ? false : !isPasswordVisible
}
style={
props.disabledInputText
? classes.disabledTextInput
: classes.textInput
}
value={props.value}
placeholder={props.placeholder}
placeholderTextColor={fonts.text.small.color}
onTouchEnd={() => setIsPressed(true)}
onChangeText={(value) => props.onChange(value)}
returnKeyType={
props.returnKeyType === "next"
? "next"
: props.returnKeyType === "done"
? "done"
: "default"
}
/>
</View>
</TouchableWithoutFeedback>
</View>
);
};
3 जवाब
आप GeneralTextInput को फॉरवर्डरफ के साथ लपेटते हैं:
import { TextInput, TextInputProps } from "react-native";
export const GeneralTextInput: React.forwardRef<TextInput,IGeneralTextInputProps> = (
// type of props and ref will be inferred by ts
props
ref
) => {
....
return (
....
<TextInput
ref={ref}
{...props}
...
...
/>
)}
अब मूल घटक में एक उपयोग परिभाषित करें:
const secondInputRef = useRef<TextInput | null>(null);
आपके पास 2 सामान्य इनपुट हैं। पहले इनपुट पर
<GeneralTextInput
....
....
// add this. this will focus on secondInput
onSubmitEditing={() => {
secondInputRef.current?.focus();
}}
/>
दूसरा सामान्य इनपुट जैसा है वैसा ही होगा
अगर मैं सही ढंग से समझूं forwardRef वह नहीं है जिसे आप ढूंढ रहे हैं।
import * as React from "react";
import {
NativeSyntheticEvent,
Platform,
StyleProp,
TextInputFocusEventData,
TextStyle,
View,
ViewStyle,
TextInput,
ImageStyle,
Pressable,
} from "react-native";
import { makeStyles, IStyledComponent } from "../../assets/theme/installation";
import { IconButton, Text, useTheme } from "react-native-paper";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import FontAwesome5Icon from "react-native-vector-icons/FontAwesome5";
import { theme } from "../../assets/theme/DefaultTheme";
import { TouchableWithoutFeedback } from "react-native-gesture-handler";
export interface IGeneralTextInputProps
extends IStyledComponent<GeneralTextInputStyles> {
readonly value: string | undefined;
readonly placeholder?: string;
readonly onChange: (newValue: string) => void;
readonly onBlur?: (e: NativeSyntheticEvent<TextInputFocusEventData>) => void;
readonly isPassword?: boolean;
readonly autoCapitalize?: boolean;
readonly error?: string;
readonly startIcon?: string;
readonly startIconFA5?: string;
readonly endIcon?: string;
readonly deleteIcon?: boolean;
readonly disabled?: boolean;
readonly disabledInputText?: boolean;
readonly bordered?: boolean;
readonly isMultiLine?: boolean;
readonly width?: number | string;
readonly numberOfLines?: number;
readonly keyboardType?: string;
readonly isGratitude?: boolean;
readonly autoCorrect?: boolean;
readonly selectedMeasureUnit?: string;
readonly returnKeyType?: string;
readonly isDoneReference?: boolean;
readonly isDismissed?: boolean;
}
export const GeneralTextInput: React.forwardRef<IGeneralTextInputProps> = (
props: IGeneralTextInputProps,
ref: any
) => {
const classes = useStyles(props);
const { fonts, colors } = useTheme();
const [isPressed, setIsPressed] = React.useState(false);
const [isPasswordVisible, setPasswordVisible] = React.useState(false);
const groupNameRef = React.useRef<HTMLInputElement>(null);
const groupDescRef = React.useRef<HTMLInputElement>(null);
return (
<View style={classes.container}>
<TouchableWithoutFeedback>
<View style={classes.root}>
<TextInput
ref={() => (props.isDoneReference ? groupDescRef : groupNameRef)}
onSubmitEditing={() => {
groupDescRef.current?.focus();
}}
blurOnSubmit={props.isDoneReference ? true : false}
keyboardType={
props.keyboardType === "numpad" ? "numeric" : "default"
}
autoCorrect={props.autoCorrect}
multiline={props.isMultiLine}
numberOfLines={props.numberOfLines}
maxLength={props.isGratitude ? 300 : 50}
editable={!props.disabled}
onBlur={props.onBlur}
autoCapitalize={
props.autoCapitalize != undefined ? "words" : "none"
}
secureTextEntry={
props.isPassword == undefined ? false : !isPasswordVisible
}
style={
props.disabledInputText
? classes.disabledTextInput
: classes.textInput
}
value={props.value}
placeholder={props.placeholder}
placeholderTextColor={fonts.text.small.color}
onTouchEnd={() => setIsPressed(true)}
onChangeText={(value) => props.onChange(value)}
returnKeyType={
props.returnKeyType === "next"
? "next"
: props.returnKeyType === "done"
? "done"
: "default"
}
/>
</View>
</TouchableWithoutFeedback>
</View>
);
};));
const ref = React.createRef();
<GeneralTextInput
ref={ref}
width={"100%"}
returnKeyType={"next"}
isDoneReference={false}
deleteIcon
startIcon={"account-multiple"}
bordered={true}
placeholder={t("form.placeholders.groupName")}
value={props.newGroupName}
onChange={(val: string) => {
props.setNewGroupName(val);
if (val.length > 25) {
props.setNewGroupNameError(t("form.validations.max-25-char"));
}
if (val.length <= 25) {
props.setNewGroupNameError(undefined);
}
}}
/>
GeneralTextInput.tsx
में अपने संदर्भ को संभालने की कोशिश न करें, अपनी मुख्य फ़ाइल में दोनों के संदर्भ को अलग-अलग संभालें और मुख्य फ़ाइल से भी onSubmitEditing
प्रॉप्स पास करें।
अपने दूसरे GeneralTextInput
के लिए एक inputRef
बनाएं और इसे अपने पहले GeneralTextInput
घटक पर केंद्रित करें
<GeneralTextInput
onSubmitEditing = {() => inputRef.current.focus()}
...
>
<GeneralTextInput
ref = { inputRef }
...
>
फिर बस उन्हें अपनी GeneralTextInput.tsx
फ़ाइल के प्रोप के रूप में पास करें। उम्मीद है इससे आपका काम बनेगा
<TextInput
ref={props.ref || null}
onSubmitEditing={props.onSubmitEditing || null}
...
>
उम्मीद है इससे आपका काम बनेगा।