-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: formik, yup and reusable forms
- Loading branch information
Showing
20 changed files
with
1,340 additions
and
266 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { View, StyleSheet, Image } from "react-native"; | ||
import * as Yup from "yup"; | ||
import Screen from "../components/Screen"; | ||
import { | ||
AppForm, | ||
AppFormField, | ||
AppSubmitButton, | ||
AppFormPicker, | ||
} from "../components/forms"; | ||
|
||
const validationSchema = Yup.object().shape({ | ||
title: Yup.string().required().min(3).label("Title"), | ||
price: Yup.string().required().min(1).max(10000).label("Price"), | ||
description: Yup.string().required().min(3).label("Description"), | ||
category: Yup.string().required().min(4).label("Category"), | ||
// category: Yup.object().required().nullable().label("Category"), | ||
}); | ||
|
||
const formFields = { | ||
title: "", | ||
price: "", | ||
description: "", | ||
category: "", | ||
}; | ||
const categories = [ | ||
{ | ||
label: "Furniture", | ||
value: 1, | ||
}, | ||
{ | ||
label: "Clothing", | ||
value: 2, | ||
}, | ||
{ | ||
label: "Cameras", | ||
value: 3, | ||
}, | ||
]; | ||
export default function () { | ||
return ( | ||
<Screen styles={styles.container}> | ||
<AppForm | ||
initialValues={formFields} | ||
onSubmit={(values) => console.log(values)} | ||
validationSchema={validationSchema} | ||
> | ||
<AppFormField | ||
autoCorrect={false} | ||
maxLength={255} | ||
placeholder="Title" | ||
name="title" | ||
/> | ||
<AppFormField | ||
keyboardTyoe="numeric" | ||
maxLength={8} | ||
placeholder="0.00" | ||
name="price" | ||
/> | ||
<AppFormPicker | ||
items={categories} | ||
name="category" | ||
placeholder="Category" | ||
/> | ||
<AppFormField | ||
multiline | ||
maxLength={255} | ||
placeholder="Description" | ||
numberOfLines={3} | ||
name="description" | ||
/> | ||
<AppSubmitButton label="Post" /> | ||
</AppForm> | ||
</Screen> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
padding: 10, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { StyleSheet, Image } from "react-native"; | ||
import * as Yup from "yup"; | ||
//in app imports | ||
import Screen from "../components/Screen"; | ||
import { AppForm, AppFormField, AppSubmitButton } from "../components/forms"; | ||
|
||
const validationSchema = Yup.object().shape({ | ||
email: Yup.string().required().email().label("Email"), | ||
password: Yup.string().required().min(4).label("Password"), | ||
}); | ||
export default function () { | ||
return ( | ||
<Screen styles={styles.container}> | ||
<Image style={styles.logo} source={require("../assets/benz-logo.png")} /> | ||
<AppForm | ||
initialValues={{ email: "", password: "" }} | ||
onSubmit={(values) => console.log(values)} | ||
validationSchema={validationSchema} | ||
> | ||
<AppFormField | ||
icon="email" | ||
autoCapitalize="none" | ||
autoCorrect={false} | ||
keyboardType="email-address" | ||
textContentType="emailAddress" | ||
placeholder="Email" | ||
name="email" | ||
/> | ||
<AppFormField | ||
icon="lock" | ||
autoCapitalize="none" | ||
autoCorrect={false} | ||
secureTextEntry={true} | ||
textContentType="password" | ||
name="password" | ||
placeholder="Password" | ||
/> | ||
<AppSubmitButton label="Login" /> | ||
</AppForm> | ||
</Screen> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
padding: 10, | ||
}, | ||
logo: { | ||
width: 100, | ||
height: 100, | ||
alignSelf: "center", | ||
marginTop: 60, | ||
marginBottom: 30, | ||
}, | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
StyleSheet, | ||
Text, | ||
TouchableOpacity, | ||
View, | ||
ViewStyle, | ||
} from "react-native"; | ||
import React from "react"; | ||
import defaultStyles from "../default-styles"; | ||
|
||
interface Props { | ||
label: string; | ||
styles?: ViewStyle; | ||
onPress: () => void; | ||
} | ||
|
||
const AppButton: React.FC<Props> = ({ styles: propStyles, onPress, label }) => { | ||
return ( | ||
<TouchableOpacity onPress={onPress}> | ||
<View style={[styles.btnStyles, propStyles]}> | ||
<Text style={styles.textStyles}>{label}</Text> | ||
</View> | ||
</TouchableOpacity> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
btnStyles: { | ||
width: "100%", | ||
borderRadius: 1000, | ||
padding: 15, | ||
justifyContent: "center", | ||
alignItems: "center", | ||
backgroundColor: defaultStyles.colors.secondary, | ||
marginVertical: 10, | ||
}, | ||
textStyles: { | ||
color: "#fff", | ||
textTransform: "uppercase", | ||
fontWeight: "500", | ||
fontSize: 18, | ||
}, | ||
}); | ||
|
||
export default AppButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { Formik } from "formik"; | ||
|
||
interface Props { | ||
initialValues: any; | ||
onSubmit: (values) => void; | ||
validationSchema: any; | ||
} | ||
|
||
const AppForm: React.FC<Props> = ({ | ||
initialValues, | ||
onSubmit, | ||
validationSchema, | ||
children, | ||
}) => { | ||
return ( | ||
<Formik | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
validationSchema={validationSchema} | ||
> | ||
{() => <>{children}</>} | ||
</Formik> | ||
); | ||
}; | ||
|
||
export default AppForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { StyleSheet } from "react-native"; | ||
import React from "react"; | ||
//in app imports | ||
import AppTextInput from "../AppTextInput"; | ||
import { useFormikContext } from "formik"; | ||
import ErrorMessage from "./ErrorMessage"; | ||
|
||
interface Props { | ||
name: string; | ||
} | ||
|
||
const AppFormField: React.FC<Props> = ({ name, ...otherProps }) => { | ||
const { setFieldTouched, handleChange, errors, touched } = useFormikContext(); | ||
return ( | ||
<> | ||
<AppTextInput | ||
// icon="lock" | ||
// autoCapitalize="none" | ||
// autoCorrect={false} | ||
// secureTextEntry={true} | ||
// textContentType="password" | ||
onBlur={() => setFieldTouched(name)} | ||
onChangeText={handleChange(name)} | ||
// placeholder="Password" | ||
{...otherProps} | ||
/> | ||
<ErrorMessage visible={touched[name]} error={errors[name]} /> | ||
</> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({}); | ||
|
||
export default AppFormField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { StyleSheet } from "react-native"; | ||
import React from "react"; | ||
import { useFormikContext } from "formik"; | ||
import AppPicker from "../AppPicker"; | ||
import ErrorMessage from "./ErrorMessage"; | ||
|
||
interface Props { | ||
name: string; | ||
items: { label: string; value: any }[]; | ||
} | ||
|
||
const AppFormPicker: React.FC<Props> = ({ items, name, placeholder }) => { | ||
const { errors, values, setFieldValue, touched } = useFormikContext(); | ||
return ( | ||
<> | ||
<AppPicker | ||
icon="apps" | ||
placeholder={placeholder} | ||
items={items} | ||
onSelectItem={(item) => setFieldValue(name, item)} | ||
selectedItem={values[name] as string} | ||
/> | ||
<ErrorMessage visible={touched[name]} error={errors[name]} /> | ||
</> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({}); | ||
|
||
export default AppFormPicker; |
Oops, something went wrong.