UNPKG

vira

Version:

A simple and highly versatile design system using element-vir.

67 lines (66 loc) 2.13 kB
import { check } from '@augment-vir/assert'; import { addSuffix, getObjectTypedValues } from '@augment-vir/common'; /** * Form field types for {@link ViraFormField}. * * @category Internal */ export var ViraFormFieldType; (function (ViraFormFieldType) { ViraFormFieldType["Text"] = "text"; /** Allows auto complete for _existing_ passwords used on this website (for login). */ ViraFormFieldType["ExistingPassword"] = "existing-password"; /** Allows auto complete for _new_ passwords used on this website (for login). */ ViraFormFieldType["NewPassword"] = "new-password"; /** Uses a password input without any attributes applied for auto complete hints. */ ViraFormFieldType["PlainPassword"] = "plain-password"; ViraFormFieldType["Email"] = "email"; ViraFormFieldType["Number"] = "number"; ViraFormFieldType["Select"] = "select"; ViraFormFieldType["Checkbox"] = "checkbox"; ViraFormFieldType["TextArea"] = "text-area"; ViraFormFieldType["Date"] = "date"; })(ViraFormFieldType || (ViraFormFieldType = {})); /** * Appends a `'*'` to a label if it exist sand if it is required. * * @category Internal */ export function applyRequiredLabel(label, isRequired) { if (label) { if (isRequired) { return addSuffix({ value: label, suffix: '*', }); } else { return label; } } else { return undefined; } } /** * Checks if all the {@link ViraFormField} entries in a given {@link ViraFormFields} are valid through * the following checks: * * - Checks that required fields are provided (not `undefined`) * - Ignores hidden fields * * @category Internal */ export function areFormFieldsValid(formFields) { return getObjectTypedValues(formFields).every((formField) => { if (formField.isHidden || !formField.isRequired) { return true; } else if (check.isString(formField.value)) { return !!formField.value; } else { return formField.value != undefined; } }); }