aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/atoms/forms/form.tsx
blob: 330715399ea650e860fc247b34705accf9d08960 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /*
import { Children, FC, FormEvent, Fragment, ReactNode } from 'react';
import styles from './forms.module.scss';

export type FormProps = {
  /**
   * An accessible name.
   */
  'aria-label'?: string;
  /**
   * One or more ids that refers to the form name.
   */
  'aria-labelledby'?: string;
  /**
   * The form body.
   */
  children: ReactNode;
  /**
   * Set additional classnames to the form wrapper.
   */
  className?: string;
  /**
   * Wrap each items with a div. Default: true.
   */
  grouped?: boolean;
  /**
   * If grouped, set additional classnames to the items wrapper.
   */
  itemsClassName?: string;
  /**
   * A callback function to execute on submit.
   */
  onSubmit: () => void;
};

/**
 * Form component.
 *
 * Render children wrapped in a form element.
 */
const Form: FC<FormProps> = ({
  children,
  grouped = true,
  itemsClassName = '',
  onSubmit,
  ...props
}) => {
  const arrayChildren = Children.toArray(children);

  /**
   * Get the form items.
   * @returns {JSX.Element[]} An array of child elements wrapped in a div.
   */
  const getFormItems = (): JSX.Element[] => {
    return arrayChildren.map((child, index) =>
      grouped ? (
        <div
          key={`item-${index}`}
          className={`${styles.item} ${itemsClassName}`}
        >
          {child}
        </div>
      ) : (
        <Fragment key={`item-${index}`}>{child}</Fragment>
      )
    );
  };

  /**
   * Handle form submit.
   * @param {FormEvent} e - The form event.
   */
  const handleSubmit = (e: FormEvent) => {
    e.preventDefault();
    onSubmit();
  };

  return (
    <form onSubmit={handleSubmit} {...props}>
      {getFormItems()}
    </form>
  );
};

export default Form;