1.5.0 • Published 8 months ago

@twintag/twintag-form v1.5.0

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

Twintag Form

Content

Introduction

Twintag Form provides a web component that allows you to create a form based on a JSON object, leaving the actions to the developer.

Framework Agnostic

Twintag Form exposes a web component that can be used in any framework or vanilla JS.

<twintag-form options="..." config="[...]"></twintag-form>

Parameters

The form component receives the following parameters:

ParameterTypeDescription
configarrayArray that contains the form structure.
optionsobjectOptions to customize the form

Config

The config object contains the form structure. It is organized by the index of the items, where each item is an Input field represented by an object with the following properties:

PropertyTypeDescription
typestringComponent type. The subtype is optional as part of the string: input:<subtype>
subtypestringComponent subtype
labelstringField label.
bindstringField name.
[
  {
    "type": "input",
    "props": {
      "subtype": "simple",
      "label": "Email",
      "bind": "email"
    }
  },
  {
    "type": "input:textarea",
    "props": {
      "label": "Message",
      "bind": "message"
    }
  },
  {
    "type": "input",
    "props": {
      "subtype": "submit",
      "label": "Submit"
    }
  }
]

Options

Twintag Form provides some options to customize the form:

OptionTypeDescription
variantdefault / betaVariant of the form.
sizebase / smDefault: base (font-size set to 16px).
resetOnSubmitbooleanIf true, the form will be reset after submitting. (false per default)
defaultValuesobjectThe properties are the same as the bind values in your config.

Inputs

Input options

Each input can be customized with the following optional options:

OptionTypeDescription
descriptionstringDescription of the input.
errorstringError message.
placeholderstringPlaceholder of the input.
optionalFieldbooleanIf true, the field will be optional. (false per default)
{
  "type": "input",
  "props": {
    "subtype": "simple",
    "label": "Email",
    "bind": "email",
    "options": {
      "description": "Please enter you email",
      "error": "The field is empty."
      "placeholder": "Write here...",
      "optionalField": true
    }
  }
}

Subtypes

  • Simple
{
  "type": "input",
  "props": {
    "subtype": "simple",
    "label": "Email",
    "bind": "email"
  }
}
  • Textarea
{
  "type": "input",
  "props": {
    "subtype": "textarea",
    "label": "Message",
    "bind": "message"
  }
}
  • File
{
  "type": "input",
  "props": {
    "subtype": "file",
    "label": "File",
    "bind": "file"
  }
}
  • Checkbox
{
  "type": "input",
  "props": {
    "subtype": "checkbox",
    "label": "Checkbox",
    "bind": "checkbox"
  }
}
  • Toggle
{
  "type": "input",
  "props": {
    "subtype": "toggle",
    "label": "Toggle",
    "bind": "toggle"
  }
}
  • Tri-Toggle
{
  "type": "input",
  "props": {
    "subtype": "tri-toggle",
    "label": "Toggle with 3 states",
    "bind": "tri-states"
  }
}
  • Radiogroup
{
  "type": "input",
  "props": {
    "subtype": "radiogroup",
    "label": "Radio Group",
    "bind": "radiogroup",
  },
  "children": [
    {
      "type": "item",
      "props": {
        "label": "First item",
        "bind": "first-item"
      }
    },
    ...
  ]
}
  • Submit
{
  "type": "input",
  "props": {
    "subtype": "submit",
    "label": "Submit"
  }
}
  • Date picker
{
  "type": "input",
  "props": {
    "subtype": "datepicker",
    "label": "Pick a date"
  }
}

Containers

  • Accordion
{
  "type": "accordion",
  "props": {
    "defaultValue": "first-item" // optional: default expanded item
  },
  "children": [
    {
      "type": "accordion",
      "props": {
        "subtype": "item",
        "value": "first-item", // optional
        "label": "Your label",
        "icon": "archive-box-x-mark"
      },
      "children": [
        {
          "type": "input",
          "props": {
            "subtype": "simple",
            "bind": "first-item"
          }
        },
      ...
      ]
    },
    ...
  ]
}
  • Category
{
  "type": "category",
  "props": {
    "label": "Category"
  },
  "children": [
    {
      "type": "input",
      "props": {
        "subtype": "simple",
        "label": "Your label"
      }
    },
    ...
  ]
}

Event Handling

<twintag-form
  id="yourform"
  options='{"variant":"beta","resetOnSubmit":true}'
  config='[{"type":"input:toggle","label":"Test","bind":"test","options":{"optionalField":true}},{"type":"input:submit","label":"Submit"}]'
></twintag-form>

<button id="submit-btn">Submit</button>
<button id="get-values-btn">Submit</button>

<script>
  const form = document.getElementById('yourform');
  const submit = document.getElementById('submit-btn');
  const getValues = document.getElementById('get-values-btn');

  // You can validate the form outside the component:
  submit.addEventListener('click', () => {
    const submitEvent = new Event('twintag-form-validate');
    form.shadowRoot.querySelector('form').dispatchEvent(submitEvent);
  });

  // If there are no errors, the form will emit a custom event with the data on submit
  form.addEventListener('twintag-form-valid', (e) => {
    console.log(e.detail); // { test: true }
  });

  // If there are errors, the form will emit a custom event with the errors
  form.addEventListener('twintag-form-invalid', (e) => {
    console.log(e.detail); // { test: 'Test is required' }
  });

  // You can get the values of the form outside the component:
  form.addEventListener('twintag-form-values', (e) => {
    console.log(e.detail);
  });
  getValues.addEventListener('click', () => {
    const getValuesEvent = new Event('twintag-form-get_values');
    form.shadowRoot.querySelector('form').dispatchEvent(getValuesEvent);
  });
</script>

CSS

The form component exposes the following CSS variables:

VariableDescription
--tf-labelColor used for the inputs label.
--tf-categoryColor used for the category labels.
--tf-backgroundBackground color for the input fields.
--tf-foregroundText color for the input fields.
--tf-mutedUsed for toggle and tri-toggle background when value is false.
--tf-muted-foregroundColor used for placeholder text (inputs) and description text.
--tf-primaryThis color complements primary-foreground: submit button text
--tf-primary-foregroundThis color is used for, submit background, label text and input value.
--tf-accentThe accent color is used for toggle, tri-toggle, checkbox and radiogroup inputs.
--tf-accent-foregroundThis color complements accent to show contrast between elements: checkbox check icon.
--tf-destructiveThis color is used for error messages and the asterisk to mark a required input.
--tf-inputThis color is used by the input borders.
--tf-ringThis color highlights a focused input.
--tf-radiusThis value is used for input rounded corners.
--tf-fontFont family for all the form.