|
| 1 | +import { Button, Checkbox, Flex, FormGroup, Input, Panel, Select, Form as StyledForm, Textarea } from '@bigcommerce/big-design'; |
| 2 | +import { ChangeEvent, FormEvent, useState } from 'react'; |
| 3 | +import { FormData, StringKeyValue } from '../types'; |
| 4 | + |
| 5 | +interface FormProps { |
| 6 | + formData: FormData; |
| 7 | + onCancel(): void; |
| 8 | + onSubmit(form: FormData): void; |
| 9 | +} |
| 10 | + |
| 11 | +const FormErrors = { |
| 12 | + name: 'Product name is required', |
| 13 | + price: 'Default price is required', |
| 14 | +}; |
| 15 | + |
| 16 | +const Form = ({ formData, onCancel, onSubmit }: FormProps) => { |
| 17 | + const { description, isVisible, name, price, type } = formData; |
| 18 | + const [form, setForm] = useState<FormData>({ description, isVisible, name, price, type }); |
| 19 | + const [errors, setErrors] = useState<StringKeyValue>({}); |
| 20 | + |
| 21 | + const handleChange = (event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 22 | + const { name: formName, value } = event?.target; |
| 23 | + setForm(prevForm => ({ ...prevForm, [formName]: value })); |
| 24 | + |
| 25 | + // Add error if it exists in FormErrors and the input is empty, otherwise remove from errors |
| 26 | + !value && FormErrors[formName] |
| 27 | + ? setErrors(prevErrors => ({ ...prevErrors, [formName]: FormErrors[formName] })) |
| 28 | + : setErrors(({ [formName]: removed, ...prevErrors }) => ({ ...prevErrors })); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleSelectChange = (value: string) => { |
| 32 | + setForm(prevForm => ({ ...prevForm, type: value })); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleCheckboxChange = (event: ChangeEvent<HTMLInputElement>) => { |
| 36 | + const { checked, name: formName } = event?.target; |
| 37 | + setForm(prevForm => ({ ...prevForm, [formName]: checked })); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleSubmit = (event: FormEvent<EventTarget>) => { |
| 41 | + event.preventDefault(); |
| 42 | + |
| 43 | + // If there are errors, do not submit the form |
| 44 | + const hasErrors = Object.keys(errors).length > 0; |
| 45 | + if (hasErrors) return; |
| 46 | + |
| 47 | + onSubmit(form); |
| 48 | + }; |
| 49 | + |
| 50 | + return ( |
| 51 | + <StyledForm onSubmit={handleSubmit}> |
| 52 | + <Panel header="Basic Information"> |
| 53 | + <FormGroup> |
| 54 | + <Input |
| 55 | + error={errors?.name} |
| 56 | + label="Product name" |
| 57 | + name="name" |
| 58 | + required |
| 59 | + value={form.name} |
| 60 | + onChange={handleChange} |
| 61 | + /> |
| 62 | + </FormGroup> |
| 63 | + <FormGroup> |
| 64 | + <Select |
| 65 | + label="Product type" |
| 66 | + name="type" |
| 67 | + options={[ |
| 68 | + { value: 'physical', content: 'Physical' }, |
| 69 | + { value: 'digital', content: 'Digital' } |
| 70 | + ]} |
| 71 | + required |
| 72 | + value={form.type} |
| 73 | + onOptionChange={handleSelectChange} |
| 74 | + /> |
| 75 | + </FormGroup> |
| 76 | + <FormGroup> |
| 77 | + <Input |
| 78 | + error={errors?.price} |
| 79 | + iconLeft={'$'} |
| 80 | + label="Default price (excluding tax)" |
| 81 | + name="price" |
| 82 | + placeholder="10.00" |
| 83 | + required |
| 84 | + type="number" |
| 85 | + step="0.01" |
| 86 | + value={form.price} |
| 87 | + onChange={handleChange} |
| 88 | + /> |
| 89 | + </FormGroup> |
| 90 | + <FormGroup> |
| 91 | + <Checkbox |
| 92 | + name="isVisible" |
| 93 | + checked={form.isVisible} |
| 94 | + onChange={handleCheckboxChange} |
| 95 | + label="Visible on storefront" |
| 96 | + /> |
| 97 | + </FormGroup> |
| 98 | + </Panel> |
| 99 | + <Panel header="Description"> |
| 100 | + <FormGroup> |
| 101 | + {/* Using description for demo purposes. Consider using a wysiwig instead (e.g. TinyMCE) */} |
| 102 | + <Textarea |
| 103 | + label="Description" |
| 104 | + name="description" |
| 105 | + placeholder="Product info" |
| 106 | + value={form.description} |
| 107 | + onChange={handleChange} |
| 108 | + /> |
| 109 | + </FormGroup> |
| 110 | + </Panel> |
| 111 | + <Flex justifyContent="flex-end"> |
| 112 | + <Button |
| 113 | + marginRight="medium" |
| 114 | + type="button" |
| 115 | + variant="subtle" |
| 116 | + onClick={onCancel} |
| 117 | + > |
| 118 | + Cancel |
| 119 | + </Button> |
| 120 | + <Button type="submit">Save</Button> |
| 121 | + </Flex> |
| 122 | + </StyledForm> |
| 123 | + ); |
| 124 | +}; |
| 125 | + |
| 126 | +export default Form; |
0 commit comments