Advertisement
ikamal7

GenerateFormComponent.js

Jun 7th, 2023
1,231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import withForm from "@/components/Booking/withForm";
  2. import {useSelector} from "react-redux";
  3.  
  4. const GenerateFormComponent = ({ fields, handleFieldChange }) => {
  5.     const formFields = useSelector((state) => state.form.fields);
  6.     console.log(fields);
  7.     const renderField = (field) => {
  8.         switch (field.type) {
  9.             case 'text':
  10.                 return (
  11.                     <div key={field.id} className={`lg:col-span-1 col-span-2 mb-7.5`}>
  12.                         <label htmlFor={field.id} className={`font-manrope text-title-sm font-semibold text-black mb-3 block`}>
  13.                             {field.label}
  14.                         </label>
  15.                             <input
  16.                                 type={field.type}
  17.                                 name={field.id}
  18.                                 id={field.id}
  19.                                 className={`border border-grey w-full px-4 py-3 text-title-sm text-black placeholder-silver`}
  20.                                 placeholder={field.placeholder}
  21.                                 value={formFields.find((f) => f.id === field.id)?.value || ''}
  22.                                 onChange={(e) => handleFieldChange(field.id, e.target.value)}
  23.                             />
  24.                     </div>
  25.                 );
  26.             case 'checkbox':
  27.                 return (
  28.                     <div key={field.id} className={`mb-7.5 col-span-2`}>
  29.                         <label className={`font-manrope text-title-sm font-semibold text-black mb-3 block`}>
  30.                             {field.label}
  31.                         </label>
  32.                         {field.extraLabel && (
  33.                             <p className={`font-manrope text-[14px] text-light-black font-normal mb-3`}>{field.extraLabel}</p>
  34.                         )}
  35.                         <div className={`border border-grey p-4`}>
  36.                             {field.options.map((option) => (
  37.                                 <div key={option.value} className={`flex items-center mb-3`}>
  38.                                     <input
  39.                                         type="checkbox"
  40.                                         name={`${field.id}[]`}
  41.                                         id={field.id}
  42.                                         value={option.value}
  43.                                         onChange={(e) => handleFieldChange(field.id, e.target.value)}
  44.                                     />
  45.                                     <span className={`font-manrope text-title-xsm3 font-normal text-light-black ml-2 block`}>{option.label}</span>
  46.                                 </div>
  47.                             ))}
  48.                         </div>
  49.                     </div>
  50.                 );
  51.  
  52.             case 'select':
  53.                 return (
  54.                     <div key={field.id} className={`lg:col-span-1 col-span-2 mb-7.5`}>
  55.                         <label htmlFor={field.id} className={`font-manrope text-title-sm font-semibold text-black mb-3 block`}>
  56.                             {field.label}
  57.                         </label>
  58.                         <select
  59.                             name={field.id}
  60.                             id={field.id}
  61.                             className={`border border-grey w-full px-4 py-3 text-title-sm text-black placeholder-silver`}
  62.                             value={formFields.find((f) => f.id === field.id)?.value || ''}
  63.                             onChange={(e) => handleFieldChange(field.id, e.target.value)}
  64.                         >
  65.  
  66.                             {field.options.map((option, index) => (
  67.                                 <option key={index} value={option}>
  68.                                     {option}
  69.                                 </option>
  70.                             ))}
  71.                         </select>
  72.                     </div>
  73.                 );
  74.                 case 'textarea':
  75.                     return (
  76.                         <div key={field.id} className={`col-span-2 mb-7.5`}>
  77.                             <label htmlFor={field.id} className={`font-manrope text-title-sm font-semibold text-black mb-3 block`}>
  78.                                 {field.label}
  79.                             </label>
  80.                             <textarea
  81.                                 name={field.id}
  82.                                 id={field.id}
  83.                                 rows={5}
  84.                                 className={`border border-grey w-full px-4 py-3 text-title-sm text-black placeholder-silver`}
  85.                                 placeholder={field.placeholder}
  86.                                 value={formFields.find((f) => f.id === field.id)?.value || ''}
  87.                                 onChange={(e) => handleFieldChange(field.id, e.target.value)}
  88.                             ></textarea>
  89.                             </div>
  90.                     );
  91.             default:
  92.                 return null;
  93.         }
  94.     };
  95.  
  96.     return <div className={`grid grid-cols-1 lg:grid-cols-2 lg:gap-8`}>{fields.map((field) => renderField(field))}</div>;
  97. };
  98.  
  99. export default withForm(GenerateFormComponent);
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement