You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
import React, { useState } from "react";
|
|
import { useFormikContext } from "formik";
|
|
|
|
import ErrorMessage from "./ErrorMessage";
|
|
import ImageInputList from "../ImageInputList";
|
|
import { View, Text, StyleSheet, Modal, Alert } from "react-native";
|
|
import colors from "../../config/colors";
|
|
import { dimensions } from "../../config/dimensions";
|
|
|
|
function FormImagePicker({ name }) {
|
|
const { errors, setFieldValue, touched, values } = useFormikContext();
|
|
const imageUris = values[name];
|
|
const [modalVisible, setModalVisible] = useState(false);
|
|
|
|
const handleAdd = (uri) => {
|
|
if (imageUris.length === 0) {
|
|
setFieldValue(name, [uri]);
|
|
} else {
|
|
createTwoButtonAlert(uri);
|
|
}
|
|
};
|
|
|
|
const createTwoButtonAlert = (uri) =>
|
|
Alert.alert(
|
|
"Substituir imagem?",
|
|
"É possível enviar apenas uma imagem",
|
|
[
|
|
{
|
|
text: "Cancel",
|
|
onPress: () => console.log("Cancel Pressed"),
|
|
style: "cancel"
|
|
},
|
|
{ text: "OK", onPress: () => setFieldValue(name, [uri])}
|
|
],
|
|
{ cancelable: false }
|
|
);
|
|
|
|
|
|
const handleRemove = (uri) => {
|
|
setFieldValue(
|
|
name,
|
|
imageUris.filter((imageUri) => imageUri !== uri)
|
|
);
|
|
};
|
|
|
|
return (
|
|
<View>
|
|
<ImageInputList
|
|
imageUris={imageUris}
|
|
onAddImage={handleAdd}
|
|
onRemoveImage={handleRemove}
|
|
/>
|
|
<ErrorMessage error={errors[name]} visible={touched[name]} />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
labelStyle: {
|
|
fontSize: dimensions.text.secondary,
|
|
fontWeight: "bold",
|
|
textAlign: "left",
|
|
color: colors.primary,
|
|
marginBottom: 5,
|
|
marginTop: 15,
|
|
},
|
|
centeredView: {
|
|
flex: 1,
|
|
justifyContent: "center",
|
|
alignItems: "center",
|
|
marginTop: 22,
|
|
},
|
|
});
|
|
|
|
|
|
|
|
export default FormImagePicker;
|