-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseReducerForm.tsx
195 lines (179 loc) · 4.69 KB
/
UseReducerForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
type FC,
type ChangeEvent,
type SyntheticEvent,
useState,
useReducer,
} from 'react';
import {
Box,
Button,
ButtonGroup,
Checkbox,
FormLabel,
Input,
Select,
} from '@chakra-ui/react';
import ReactSelect from 'react-select';
import { wrappingType, prefectures } from 'schemas/constants';
import { type DeliveryForm } from 'schemas/deliveryForm';
import { type ValueOf } from 'utils';
const initialState: DeliveryForm = {
zipCode: '',
prefecture: '',
city: '',
address: '',
building: '',
wrapping: 'none',
};
const ActionType = {
reset: 'deliveryForm/reset',
updated: 'deliveryForm/updated',
} as const;
type Action = {
type: ValueOf<typeof ActionType>;
payload?: Partial<DeliveryForm>;
};
const reducer = (state: DeliveryForm, action: Action): DeliveryForm => {
switch (action.type) {
case ActionType.reset:
return initialState;
case ActionType.updated:
return {
...state,
...action.payload,
};
default: {
const _: never = action.type;
return state;
}
}
};
const reset = (): Action => ({
type: ActionType.reset,
});
const updated = (payload: Partial<DeliveryForm>): Action => ({
type: ActionType.updated,
payload,
});
/**
* main component
*/
const UseReducerForm: FC = () => {
const [formState, dispatch] = useReducer(reducer, initialState);
const [isConfirmed, setIsConfirmed] = useState<boolean>(false);
const handleSubmit = (e: SyntheticEvent) => {
e.preventDefault();
console.log(formState);
};
const handleReset = (e: SyntheticEvent) => {
e.stopPropagation();
setIsConfirmed(false);
dispatch(reset());
};
return (
<Box p={4} w="md" borderWidth="1px" borderRadius="lg" boxShadow="base">
<form onSubmit={handleSubmit}>
<FormLabel htmlFor="zipcode" mt={4}>
郵便番号
</FormLabel>
<Input
size="md"
maxLength={7}
value={formState.zipCode}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
dispatch(updated({ zipCode: e.target.value }));
}}
/>
<FormLabel htmlFor="prefecture" mt={4}>
都道府県
</FormLabel>
<ReactSelect
options={prefectures.map((pref) => ({ value: pref, label: pref }))}
value={{ value: formState.prefecture, label: formState.prefecture }}
onChange={(prefObj) => {
dispatch(updated({ prefecture: prefObj?.value ?? '' }));
}}
/>
<FormLabel htmlFor="city" mt={4}>
市区町村
</FormLabel>
<Input
size="md"
value={formState.city}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
dispatch(updated({ city: e.target.value }));
}}
/>
<FormLabel htmlFor="address" mt={4}>
番地
</FormLabel>
<Input
size="md"
value={formState.address}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
dispatch(updated({ address: e.target.value }));
}}
/>
<FormLabel htmlFor="building" mt={4}>
建物名・部屋番号
</FormLabel>
<Input
size="md"
value={formState.building}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
dispatch(updated({ building: e.target.value }));
}}
/>
<FormLabel htmlFor="wrapping" mt={4}>
包装オプション
</FormLabel>
<Select
value={formState.wrapping}
onChange={(e: ChangeEvent<HTMLSelectElement>) => {
dispatch(
updated({
wrapping: e.target.value as keyof typeof wrappingType,
}),
);
}}
>
{Object.entries(wrappingType).map(([code, value]) => (
<option key={code} value={code}>
{value}
</option>
))}
</Select>
<Checkbox
isChecked={isConfirmed}
onChange={(e: ChangeEvent<HTMLInputElement>) => {
setIsConfirmed(e.target.checked);
}}
mt={4}
>
内容を確認しました
</Checkbox>
<ButtonGroup my={3} w="xs">
<Button
w="30%"
colorScheme="orange"
variant="solid"
type="submit"
isDisabled={!isConfirmed}
>
送信
</Button>
<Button
w="30%"
colorScheme="orange"
variant="outline"
onClick={handleReset}
>
リセット
</Button>
</ButtonGroup>
</form>
</Box>
);
};
export default UseReducerForm;