1.3.0 • Published 4 days ago

@twintag/twintag-form v1.3.0

Weekly downloads
-
License
-
Repository
-
Last release
4 days 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",
    "subtype": "simple",
    "label": "Email",
    "bind": "email"
  },
  {
    "type": "input:textarea",
    "label": "Message",
    "bind": "message"
  },
  {
    "type": "input",
    "subtype": "submit",
    "label": "Submit"
  }
]

Options

Twintag Form provides some options to customize the form:

OptionTypeDescription
variantdefault/betaVariant of the form.
resetOnSubmitbooleanIf true, the form will be reset after submitting. (false per default)

Inputs

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",
  "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",
  "subtype": "simple",
  "label": "Email",
  "bind": "email"
}
  • Textarea
{
  "type": "input",
  "subtype": "textarea",
  "label": "Message",
  "bind": "message"
}
  • File
{
  "type": "input",
  "subtype": "file",
  "label": "File",
  "bind": "file"
}
  • Checkbox
{
  "type": "input",
  "subtype": "checkbox",
  "label": "Checkbox",
  "bind": "checkbox"
}
  • Toggle
{
  "type": "input",
  "subtype": "toggle",
  "label": "Toggle",
  "bind": "toggle"
}
  • Radiogroup
{
  "type": "input",
  "subtype": "radiogroup",
  "label": "Radio Group",
  "bind": "radiogroup",
  "children": [
    {
      "type": "item",
      "label": "First item",
      "bind": "first-item"
    },
    ...
  ]
}
  • Submit
{
  "type": "input",
  "subtype": "submit",
  "label": "Submit"
}

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="btn">Submit</button>

<script>
  const form = document.getElementById('yourform');
  const btn = document.getElementById('btn');

  // 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 validate the form outside the component:
  btn.addEventListener('click', () => {
    const submitEvent = new Event('twintag-form-validate');
    form.shadowRoot.querySelector('form').dispatchEvent(submitEvent);
  });
</script>

CSS

The form component exposes the following CSS variables:

VariableDescription
--tf-backgroundBackground color for the input fields.
--tf-foregroundColor used for the category labels.
--tf-mutedUsed for switch 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 switch, 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 borders of the inputs.
--tf-ringThis color highlights a focused input.
--tf-radiusThis value is used for rounded corners of all inputs.
--tf-fontFont family for all the form.