import {
  ChangeEvent,
  forwardRef,
  ForwardRefRenderFunction,
  SetStateAction,
} from 'react';
import styles from './forms.module.scss';

export type FieldType =
  | 'datetime-local'
  | 'email'
  | 'number'
  | 'search'
  | 'tel'
  | 'text'
  | 'textarea'
  | 'time'
  | 'url';

export type FieldProps = {
  /**
   * One or more ids that refers to the field name.
   */
  'aria-labelledby'?: string;
  /**
   * Add classnames to the field.
   */
  className?: string;
  /**
   * Field state. Either enabled (false) or disabled (true).
   */
  disabled?: boolean;
  /**
   * Field id attribute.
   */
  id: string;
  /**
   * Field maximum value.
   */
  max?: number | string;
  /**
   * Field minimum value.
   */
  min?: number | string;
  /**
   * Field name attribute.
   */
  name: string;
  /**
   * Placeholder value.
   */
  placeholder?: string;
  /**
   * True if the field is required. Default: false.
   */
  required?: boolean;
  /**
   * Callback function to set field value.
   */
  setValue: (value: SetStateAction<string>) => void;
  /**
   * Field incremental values that are valid.
   */
  step?: number | string;
  /**
   * Field type. Default: text.
   */
  type: FieldType;
  /**
   * Field value.
   */
  value: string;
};

/**
 * Field component.
 *
 * Render either an input or a textarea.
 */
const Field: ForwardRefRenderFunction<HTMLInputElement, FieldProps> = (
  { className = '', setValue, type, ...props },
  ref
) => {
  /**
   * Update select value when an option is selected.
   * @param e - The option change event.
   */
  const updateValue = (
    e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
  ) => {
    setValue(e.target.value);
  };

  return type === 'textarea' ? (
    <textarea
      onChange={updateValue}
      className={`${styles.field} ${styles['field--textarea']} ${className}`}
      {...props}
    />
  ) : (
    <input
      className={`${styles.field} ${className}`}
      onChange={updateValue}
      ref={ref}
      type={type}
      {...props}
    />
  );
};

export default forwardRef(Field);
ems/index.tsx?h=v1.0.0'>stats</a></td><td class='form'><form class='right' method='get' action='/www.armandphilippot.com/log/src/components/MetaItems/index.tsx'>
<input type='hidden' name='h' value='v1.0.0'/><input type='hidden' name='id' value='2b5af5146d7c22eac4a5150a038a05620ec5b8a3'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/www.armandphilippot.com/tree/?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>root</a>/<a href='/www.armandphilippot.com/tree/src?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>src</a>/<a href='/www.armandphilippot.com/tree/src/components?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>components</a>/<a href='/www.armandphilippot.com/tree/src/components/MetaItems?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>MetaItems</a>/<a href='/www.armandphilippot.com/tree/src/components/MetaItems/index.tsx?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>index.tsx</a></div><div class='content'>blob: e90d5a6f352f03ce7b454a46432f2de1c0c84456 (<a href='/www.armandphilippot.com/plain/src/components/MetaItems/index.tsx?h=v1.0.0&amp;id=2b5af5146d7c22eac4a5150a038a05620ec5b8a3'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
</pre></td>
<td class='lines'><pre><code>