File size: 1,114 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
export type Field = {
  name: string;
  label: string;
  required?: boolean;
  type: 'text' | 'number';
};

const ContactFormExample: React.FC<{
  recipientId: string;
  fields: Field[];
}> = ({ fields = [], recipientId }) => {
  return (
    <form
      onSubmit={(e) => {
        const formData = new FormData(e.currentTarget);
        const formProps = Object.fromEntries(formData);
        alert(`would send contact form to ${recipientId}.\n
${Object.keys(formProps)
  .map((key) => `${key}: ${formProps[key]}`)
  .join('\n')}
        `);
        e.preventDefault();
      }}
      style={{
        border: '1px solid black',
        padding: 10,
      }}
    >
      {fields.map((field, index) => (
        <label key={field.name ?? index}>
          {field.label} {field.required ? '*' : ''}
          <br />
          <input
            type={field.type}
            required={field.required}
            name={field.name}
          />
          <br />
        </label>
      ))}
      <button type="submit">Submit</button>
    </form>
  );
};

export default ContactFormExample;