Skip to content

Commit

Permalink
feat: add form-text-input component
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-wasmeier-titanom committed May 24, 2024
1 parent 2fd9e46 commit 934c42e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions frontend/src/components/form-text-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import {
Control,
Controller,
FieldErrors,
FieldValues,
InputValidationRules,
Path,
RegisterOptions,
} from "react-hook-form";
import { View, Text, TextInput, TextInputProps } from "react-native";

type Props<T extends FieldValues> = {
name: Path<T>;
labelText: string;
textInputProps: Omit<TextInputProps, "onChangeText" | "value">;
control: Control<T>;
errors: FieldErrors<T>;
rules?: RegisterOptions<T>;
};

function FormTextInput<T extends FieldValues>({
name,
labelText,
textInputProps,
control,
errors,
rules,
}: Props<T>) {
return (
<View>
<Text className="text-white mb-2">
{labelText} {rules?.required && "*"}
</Text>
<Controller
control={control}
rules={rules}
render={({ field: { onChange, value } }) => (
<TextInput
{...textInputProps}
onChangeText={onChange}
value={value}
className="p-4 rounded-lg bg-white mb-2"
/>
)}
name={name}
/>
{errors[name] && <Text className="text-red-300">Title is required</Text>}
</View>
);
}

export default FormTextInput;

0 comments on commit 934c42e

Please sign in to comment.