Elixir's UI (design)
Design (UI) components
Form

Form Component

This component is used to render a form with the given fields.

Preview

Imports

import EccUtilDesignForm from '@elixir-cloud/design/dist/react/form/index';

Example

EccUtilsDesignForm
import React from 'react';
import { Field } from '@elixir-cloud/design/dist/components/form/form';
import EccUtilDesignForm from '@elixir-cloud/design/dist/react/form/index';
 
export default function Form() {
  const fields: Field[] = [
    {
      key: 'service-name',
      label: 'Service name',
      type: 'text',
      fieldOptions: {
        required: true,
      },
    },
  ];
 
return <EccUtilDesignForm fields={fields} />
}

Properties

PropertyRequiredDefaultTypeDescription
fieldstrue[]ArrayArray of fields to be rendered in the form.

fields

This property is used to render the fields in the form. Fields can be passed as the array of objects. Each object represents a field. The object can have the following properties.

PropertyRequiredDefaultTypeDescription
keytruenullstringUnique key for the field.
labeltruenullstringLabel for the field.
typefalsetexttext, date, number, email, password, tel, url, search, datetime-local, time, file, switch, array, groupType of the field.
childrenfalsenullarrayChildren fields for the field if type is array. This allows fields to be added dynamically
fieldOptions.requiredfalsefalsebooleanWhether the field is required or not.
fieldOptions.defaultfalsenullstring, booleanValue of the field
fieldOptions.multiplefalsefalsebooleanWhether fields of type file accept multiple values. Only applies to fields of type file
fieldOptions.tooltipfalsenullstringTooltip or help-text that will render a popup when hovered over label of form field.
fieldOptions.acceptfalsenullstringA comma separated string that determines the types of files that fields of type file will accept. Example (opens in a new tab). Only applies to fields of type file
fieldOptions.returnIfEmptyfalsefalsebooleanDetermines if the data from an empty input field will be returned in the form data when empty.
arrayOptions.defaultInstancesfalsenullnumberSets a default number of instances for fields of type array Only applies to fields of type array
arrayOptions.maxfalsenullnumberSets a maximum number of instances for fields of type array Only applies to fields of type array
arrayOptions.minfalsenullnumberSets a minimum number of instances for fields of type array Only applies to fields of type array arrayOptions.defaultInstances must also be set and must be a number greater than arrayOptions.min
groupOptions.collapsiblefalsenullnumberDetermines if a field of type group will be collapsible

Events

Event NameReact Event NameDescription
ecc-utils-submitEccUtilsSubmitEventThis event is fired when the form is submitted. The event detail contains the form data.

Methods

Method NameArgumentsDescription
idle()noneReset the form state to idle. Doesn't affect the form values.
loading()noneSet the form state to loading. Disables the submit button.
success()Set the form state to success. Show the success message.
error()Set the form state to error. Show the error message at end.
disableSubmit()booleanToggled the disable state on the form submit button

Parts

Part NameDescription
formComponent's internal form.
submit-buttonComponent's internal submit button
fieldComponent's row containing a input, label & other elements.
labelComponent's label for field.
input-baseComponent's input base element.
inputComponent's input element. :placeholder and other pseudo selectors are supported.
input-labelComponent's label for email, file, text, date, number, tel, url, search, datetime-local, time and password type fields
headerComponent's header for array and group type fields
containerComponent's container for array and group type fields
itemIndividual child fields in an array and group type field
array-headerComponent's header for array type field containing label & add button.
array-labelComponent's label for array type field.
array-itemIndividual child fields in an array type field.
add-buttonComponent's add button for array type field.
remove-buttonComponent's remove button for array type field.
switchComponent's switch.
switch-thumbComponent's switch thumb element.
switch-labelComponent's label for switch type field
groupComponent's group field
group-itemIndividual child fields in a group type field
group-headerComponent's header for group type field, containing label
group-labelComponent's label for group type field
group-toggle-iconComponent's toggle icon for group type field
group-contentContent area for group type field, where the children are rendered

CSS Variables

👨‍🏭

Under construction

Examples

Complex Form

fields
const fields: Fields[] = [
  {
    key: 'name',
    label: 'Name',
    type: 'text',
    fieldOptions: {
      required: true,
      tooltip: 'Your name',
    },
  },
  {
    key: 'email',
    label: 'Email',
    type: 'email',
    fieldOptions: {
      tooltip: 'Your email address',
    },
  },
  {
    key: 'address',
    label: 'Address',
    type: 'group',
    groupOptions: {
      collapsible: true,
      tooltip: 'Your address',
    },
    fieldOptions: {
      tooltip: 'Group for address',
    },
    arrayOptions: {
      defaultInstances: 1,
      max: 4,
      min: 1,
    },
    children: [
      {
        key: 'Details',
        label: 'Details',
        type: 'array',
        fieldOptions: {
          tooltip: 'Details for address',
        },
        arrayOptions: {
          defaultInstances: 0,
          max: 2,
        },
        children: [
          {
            key: 'houseNumber',
            label: 'House Number',
            type: 'text',
            fieldOptions: {
              required: true,
              tooltip: 'Your house number',
            },
          },
          {
            key: 'street',
            label: 'Street',
            type: 'text',
            fieldOptions: {
              default: '1601 Harrier Ln',
              required: false,
              tooltip: 'Your street name',
            },
          },
          {
            key: 'city',
            label: 'City',
            type: 'text',
            fieldOptions: {
              required: true,
              tooltip: 'Your city name',
            },
          },
          {
            key: 'isPrimary',
            label: 'Primary',
            type: 'switch',
            fieldOptions: {
              default: true,
              tooltip: 'Is this your primary residence?',
            },
          },
        ],
      },
    ],
  },
  {
    key: '18+',
    label: '18+',
    type: 'switch',
    fieldOptions: {
      tooltip: 'Are you over 18 years old?',
    },
  },
  {
    key: 'id',
    label: 'ID',
    type: 'file',
    fieldOptions: {
      required: true,
      tooltip: 'Your ID document',
    },
  },
];

Styled Form

form.css
.styled-form-example::part(form) {
  display: flex;
  flex-direction: column;
}
 
.styled-form-example::part(submit-button) {
  margin-top: 20px;
  background-color: #4caf50;
  border: none;
  color: white;
  border-radius: 10px;
}
 
.styled-form-example::part(field) {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
 
.styled-form-example::part(array-header) {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
 
.styled-form-example::part(array-label) {
  font-weight: bold;
}
 
.styled-form-example::part(array-item) {
  padding-top: 10px;
  padding-bottom: 10px;
  padding-right: 10px;
  border: 1px solid #ccc;
  border-radius: 10px;
}
 
.styled-form-example::part(add-button) {
  color: #4caf50;
  border: none;
  border-radius: 10px;
}
 
.styled-form-example::part(remove-button) {
  color: #f44336;
  margin-bottom: -12px;
}
 
.styled-form-example::part(label) {
  font-size: 0.85rem;
}
 
.styled-form-example::part(switch) {
  background-color: #fff;
  height: 20px;
  border: 1px solid #ccc;
  box-shadow: 1px 1px 1px 1px #ccc;
}
 
.styled-form-example::part(switch-thumb) {
  background-color: #4caf50;
  height: 20px;
  width: 20px;
  border: 0px;
}
 
.styled-form-example::part(input-base) {
  border: 1px solid #ccc;
  border-radius: 10px;
  box-shadow: 1px 1px 1px 1px #ccc;
}
 
.styled-form-example::part(input) {
  font-size: small;
}
 
.styled-form-example::part(input):focus {
  outline: none;
  background-color: #f5f5f5;
}
 
.styled-form-example::part(input)::file-selector-button {
  background-color: #fff;
  color: #4caf50;
  font-weight: bold;
}
;