reviewForm.js 2.14 KB
Newer Older
YoonDongMin's avatar
모달    
YoonDongMin committed
1
2
3
4
import React from 'react';
import { StyleSheet, Button, TextInput, View, Text } from 'react-native';
import { globalStyles } from '../styles/global.js'
import { Formik } from 'formik';
YoonDongMin's avatar
부채    
YoonDongMin committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import * as yup from 'yup';

const ReviewSchema = yup.object({
    title: yup.string()
        .required()
        .min(4),
    body: yup.string()
        .required()
        .min(8),
    rating: yup.string()
        .required()
        .test('is-num-1-5', 'Rating must be a number 1 -5', (val) => {
            return parseInt(val) < 6 && passInt(val) > 0;
        })
})


YoonDongMin's avatar
모달    
YoonDongMin committed
22
23
24

export default function ReviewForm(addReview) {
    return (
YoonDongMin's avatar
부채    
YoonDongMin committed
25
        <View style={globalStyles.container} >
YoonDongMin's avatar
모달    
YoonDongMin committed
26
27
            <Formik
                initialValues={{ title: '', body: '', rating: '' }}
YoonDongMin's avatar
부채    
YoonDongMin committed
28
                validationSchema={ReviewSchema}
YoonDongMin's avatar
모달    
YoonDongMin committed
29
30
31
                onSubmit={(values) => {
                    addReview(values);

YoonDongMin's avatar
부채    
YoonDongMin committed
32
33

                }}
YoonDongMin's avatar
모달    
YoonDongMin committed
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

            >
                {(props) => (
                    <View>
                        <TextInput
                            style={globalStyles.input}
                            placeholder='Review title'
                            onChangeText={props.handleChange('title')}
                            value={props.values.title}
                        />

                        <TextInput
                            multiline
                            style={globalStyles.input}
                            placeholder='Review body'
                            onChangeText={props.handleChange('body')}
                            value={props.values.body}
                        />

                        <TextInput
                            multiline
                            style={globalStyles.input}
                            placeholder='Rating (1-5)'
                            onChangeText={props.handleChange('rating')}
                            value={props.values.rating}
YoonDongMin's avatar
부채    
YoonDongMin committed
59
                            keyboardType = 'numeric'
YoonDongMin's avatar
모달    
YoonDongMin committed
60
61
                        />

YoonDongMin's avatar
부채    
YoonDongMin committed
62
                        <Button title='submit' color='maroon' onPress={props.handleSubmit} />
YoonDongMin's avatar
모달    
YoonDongMin committed
63
64
65
66
67
                    </View>
                )}

            </Formik>

YoonDongMin's avatar
부채    
YoonDongMin committed
68
        </View >
YoonDongMin's avatar
모달    
YoonDongMin committed
69
70
    )
}