|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState } from 'react'; |
| 4 | +import { FormProps } from '../../shared/types'; |
| 5 | + |
| 6 | +const Form = ({ title, description, inputs, radioBtns, textarea, checkboxes, btn, btnPosition }: FormProps) => { |
| 7 | + const [inputValues, setInputValues] = useState([]); |
| 8 | + const [radioBtnValue, setRadioBtnValue] = useState(''); |
| 9 | + const [textareaValues, setTextareaValues] = useState(''); |
| 10 | + const [checkedState, setCheckedState] = useState<boolean[]>(new Array(checkboxes && checkboxes.length).fill(false)); |
| 11 | + |
| 12 | + // Update the value of the entry fields |
| 13 | + const changeInputValueHandler = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 14 | + const { name, value } = event.target; |
| 15 | + |
| 16 | + setInputValues({ |
| 17 | + ...inputValues, |
| 18 | + [name]: value, |
| 19 | + }); |
| 20 | + }; |
| 21 | + |
| 22 | + // Update checked radio buttons |
| 23 | + const changeRadioBtnsHandler = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 24 | + setRadioBtnValue(event.target.value); |
| 25 | + }; |
| 26 | + |
| 27 | + // Update the textarea value |
| 28 | + const changeTextareaHandler = (event: React.ChangeEvent<HTMLTextAreaElement>) => { |
| 29 | + setTextareaValues(event.target.value); |
| 30 | + }; |
| 31 | + |
| 32 | + // Update checkbox radio buttons |
| 33 | + const changeCheckboxHandler = (index: number) => { |
| 34 | + setCheckedState((prevValues) => { |
| 35 | + const newValues = [...(prevValues as boolean[])]; |
| 36 | + newValues.map(() => { |
| 37 | + newValues[index] = !checkedState[index]; |
| 38 | + }); |
| 39 | + return newValues; |
| 40 | + }); |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div className="card h-fit max-w-6xl p-5 md:p-12" id="form"> |
| 45 | + {title && <h2 className={`${description ? 'mb-2' : 'mb-4'} text-2xl font-bold`}>{title}</h2>} |
| 46 | + {description && <p className="mb-4">{description}</p>} |
| 47 | + <form id="contactForm"> |
| 48 | + <div className="mb-6"> |
| 49 | + {/* Inputs */} |
| 50 | + <div className="mx-0 mb-1 sm:mb-4"> |
| 51 | + {inputs.map(({ type, label, name, placeholder }, index) => ( |
| 52 | + <div key={`item-input-${index}`} className="mx-0 mb-1 sm:mb-4"> |
| 53 | + <label htmlFor={name} className="pb-1 text-xs uppercase tracking-wider"> |
| 54 | + {label} |
| 55 | + </label> |
| 56 | + <input |
| 57 | + type={type} |
| 58 | + name={name} |
| 59 | + value={inputValues[index]} |
| 60 | + onChange={changeInputValueHandler} |
| 61 | + placeholder={placeholder} |
| 62 | + className="mb-2 w-full rounded-md border border-gray-400 py-2 pl-2 pr-4 shadow-md dark:text-gray-300 sm:mb-0" |
| 63 | + /> |
| 64 | + </div> |
| 65 | + ))} |
| 66 | + </div> |
| 67 | + {/* Radio buttons */} |
| 68 | + {radioBtns && ( |
| 69 | + <div className="mx-0 mb-1 sm:mb-3"> |
| 70 | + <label className="pb-1 text-xs uppercase tracking-wider">{radioBtns?.label}</label> |
| 71 | + <div className="flex flex-wrap"> |
| 72 | + {radioBtns.radios.map(({ label }, index) => ( |
| 73 | + <div key={`radio-btn-${index}`} className="mr-4 items-baseline"> |
| 74 | + <input |
| 75 | + type="radio" |
| 76 | + name={label} |
| 77 | + value={`value${index}`} |
| 78 | + checked={radioBtnValue === `value${index}`} |
| 79 | + onChange={changeRadioBtnsHandler} |
| 80 | + className="cursor-pointer" |
| 81 | + /> |
| 82 | + <label className="ml-2">{label}</label> |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + {/* Textarea */} |
| 89 | + {textarea && ( |
| 90 | + <div className={`mx-0 mb-1 sm:mb-4`}> |
| 91 | + <label htmlFor={textarea.name} className="pb-1 text-xs uppercase tracking-wider"> |
| 92 | + {textarea.label} |
| 93 | + </label> |
| 94 | + <textarea |
| 95 | + name={textarea.name} |
| 96 | + cols={textarea.cols} |
| 97 | + rows={textarea.rows} |
| 98 | + value={textareaValues} |
| 99 | + onChange={(e) => changeTextareaHandler(e)} |
| 100 | + placeholder={textarea.placeholder} |
| 101 | + className="mb-2 w-full rounded-md border border-gray-400 py-2 pl-2 pr-4 shadow-md dark:text-gray-300 sm:mb-0" |
| 102 | + /> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + {/* Checkboxes */} |
| 106 | + {checkboxes && ( |
| 107 | + <div className="mx-0 mb-1 sm:mb-4"> |
| 108 | + {checkboxes.map(({ label }, index) => ( |
| 109 | + <div key={`checkbox-${index}`} className="mx-0 my-1 flex items-baseline"> |
| 110 | + <input |
| 111 | + type="checkbox" |
| 112 | + name={label} |
| 113 | + checked={checkedState[index]} |
| 114 | + onChange={() => changeCheckboxHandler(index)} |
| 115 | + className="cursor-pointer" |
| 116 | + /> |
| 117 | + <label className="ml-2">{label}</label> |
| 118 | + </div> |
| 119 | + ))} |
| 120 | + </div> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + <div |
| 124 | + className={`${btnPosition === 'left' ? 'text-left' : btnPosition === 'right' ? 'text-right' : 'text-center'}`} |
| 125 | + > |
| 126 | + <button type={btn.type} className="btn btn-primary sm:mb-0"> |
| 127 | + {btn.title} |
| 128 | + </button> |
| 129 | + </div> |
| 130 | + </form> |
| 131 | + </div> |
| 132 | + ); |
| 133 | +}; |
| 134 | + |
| 135 | +export default Form; |
0 commit comments