Text Field

June 22, 2019 ยท View on GitHub

Form input field. Generates a styled text input element.

Main features

  • Optional label, floating label
  • Helper message
  • Multi-line
  • Dense or full-width formatting
  • Front-end validation
  • Custom validation function
  • "required" state indicators or messages
  • Postpone validation until after input
  • Character counter
  • Programmatically set value
  • Programmatically set focus

Usage

Options

Text field options

These options have effect on the overall component (label, input, help, error).

ParameterRequiredTypeDefaultDescription
counteroptionalNumberSet to any number greater than 0 to create a character counter below the field; optionally combine with maxlength (see below)
denseoptionalBooleanfalseCreates a more compact layout
floatingLabeloptionalBooleanfalseMakes the label move upward when the field gets focus
focusHelpoptionalBooleanfalseMakes the help text appear when the field gets focus
fullWidthoptionalBooleanfalseSet to true change the layout of the field better fitted for full width
helpoptionalStringHelp text below the field
hideClearoptionalBooleantrueSet to false to show the default browser clear button
hideSpinneroptionalBooleantrueSet to false to show the default browser step indicator on number inputs
hideValidationoptionalBooleanfalseSet to true to hide invalid state indicators
labeloptionalStringText label; unless floatingLabel is true, the label is functionally equal to a placeholder

Input options

These options also have effect on the generated HTML input field.

ParameterRequiredTypeDefaultDescription
autofocus (React: autoFocus)optionalBooleanSet to true to give the input field autofocus. Does not work on iOS; set focus explicitly when an event is fired.
defaultValueoptionalStringInitial input value
disabledoptionalBooleanfalseCreates a disabled input field
ignoreEventsoptionalArrayList of input event names to ignore, for instance ["onblur"]
multiLineoptionalBooleanfalseSet to true to create a textarea instead of an text input field
nameoptionalStringInput element name
onChangeoptionalFunction ({ focus: boolean, setInputState: setInputStateFn, dirty: boolean, value: string, el: HTMLElement, invalid: boolean, error: string }) => undefinedCallback function that receives the field state
readonly (React: readOnly)optionalBooleanCreates a readonly input field
rowsoptional (only when multiLine is true)NumberThe number of rows for the textarea
typeoptionalString: "text", "password", "email", "number", ..."text"Type of input element
valueoptionalStringInput value; this should normally not be set - for instance Safari will move the input to the end of the string at each change
placeholderoptionalStringPlaceholder text; alternative for using a label
domAttributesoptionalObjectAdditional attibutes such as autocapitalize (autoCapitalize for React)

Validation options

ParameterRequiredTypeDefaultDescription
erroroptionalStringMessage that is displayed when the field is invalid
maxoptionalNumberMaximum value (for type: number)
maxlength (React: maxLength)optionalIntegerMaximum number of characters (for type: text, email, search, password, tel, or url; browsers do not support this for type "number")
minoptionalNumberMinimum value (for type: number)
minlength (React: minLength)optionalIntegerMinimum number of characters (for type: text, email, search, password, tel, or url)
optionalIndicatoroptionalStringString to indicate that the field is optional; added to the label string
patternoptionalStringValidation regex pattern for fields of type text, search, url, tel, email, password
requiredoptionalBooleanfalseSet to true to use HTML5 field validation to test for a non-empty value; adds a "required mark" (asterisk character) to the label
requiredIndicatoroptionalString"*"String to indicate that the field is required; added to the label string
validoptionalBooleanfalseUse for per field validation when the field value is kept in local state, for instance when using a form validator; overrides built-in form validation
validateoptionalFunction `(value: string) => { valid: boolean, error: string }undefined`
validateAtStartoptionalBooleanfalseSet to true to validate the field before any user action
validateOnInputoptionalBooleanfalseSet to true to validate the field at the first keypress
validateResetOnClearoptionalBooleanfalseSet to true to re-initiate validation state when the field is cleared

Common component options

ParameterRequiredTypeDefaultDescription
afteroptionalString, hyperscript or componentExtra content after main content; note that this content is placed right of preceding elements with a higher stacking depth
beforeoptionalString, hyperscript or componentExtra content before main content; note that this content is placed left of subsequent elements with a lower stacking depth
classNameoptionalStringExtra CSS class appended to pe-textfield
elementoptionalString"div"HTML element tag
eventsoptionalObjectInput events; options object containing one or more events; predefined events are (Mithril) onfocus, onblur, oninput, onfocus, onclick, onkeydown, (React) onFocus, onBlur, onInput, onFocus, onClick, onKeyDown; events with the same name that are specified in the events option will overwrite the predefined functions; use ignoreEvents to ignore specific events
idoptionalStringHTML element id
tabindex (React: tabIndex)optionalInteger0Tab index
toneoptionalString: "dark" or "light"Renders the component light on dark (sets class pe-dark-tone); use "light" to locally inverse (sets class pe-light-tone)

CSS classes