CxJS

TextField

import { TextField } from 'cx/widgets'; Copied

The TextField widget is used for single-line text inputs. It’s one of the most commonly used form controls with support for validation, placeholders, icons, and various input modes.

<LabelsTopLayout columns={2}>
  <TextField label="Standard" value={bind(m.text, "Hello World")} autoFocus />
  <TextField label="Disabled" value={m.text} disabled />
  <TextField label="Read-only" value={m.text} readOnly />
  <TextField
    label="Placeholder"
    value={m.placeholder}
    placeholder="Type something..."
  />
  <TextField label="Required" value={m.text} required />
  <TextField
    label="Min/Max Length"
    value={m.text}
    minLength={3}
    maxLength={8}
  />
</LabelsTopLayout>

Styling

TextField supports icons, clear buttons, and custom styling for the input element.

<LabelsTopLayout columns={2}>
  <TextField
    label="With Icon"
    value={m.search}
    icon="search"
    placeholder="Search..."
    showClear
  />
  <TextField
    label="Password Toggle"
    value={bind(m.password, "secret123")}
    inputType={expr(m.showPassword, (show) => (show ? "text" : "password"))}
    icon={{
      name: expr(m.showPassword, (show) => (show ? "eye-off" : "eye")),
      style: "pointer-events: all; cursor: pointer",
      onClick: (e, { store }) => {
        e.stopPropagation()
        store.toggle(m.showPassword)
      },
    }}
  />
  <TextField
    label="Custom Input Style"
    value={bind(m.text, "Styled input")}
    inputStyle={{ border: "2px solid #3b82f6", borderRadius: "8px" }}
  />
  <TextField label="View Mode" value={m.text} viewMode emptyText="N/A" />
</LabelsTopLayout>
Styled input

See Also

Configuration

Core Properties

PropertyTypeDefaultDescription
valuestringnullText value of the input
placeholderstringHint text displayed when the field is empty
disabledbooleanfalseDisables the input
enabledbooleanOpposite of disabled
readOnlybooleanfalseMakes the input read-only
requiredbooleanfalseMarks the field as required
labelstringField label text
viewModebooleanfalseDisplay as plain text instead of interactive input
emptyTextstringText shown in view mode when the field is empty
emptyValueanynullValue written to the store when the field is cleared
inputTypestring"text"HTML input type. Use "password" for password fields
reactOnstring"change input blur enter"Events that trigger value updates. Options: "input", "change", "enter", "blur". Separate with space

Validation

PropertyTypeDefaultDescription
minLengthnumberMinimum allowed text length
maxLengthnumberMaximum allowed text length
minLengthValidationErrorTextstring"Enter {[{0}-{1}]} more character(s)."Error message when text is too short
maxLengthValidationErrorTextstring"Use {0} characters or fewer."Error message when text is too long
validationRegExpRegExpRegular expression for validating the input
validationErrorTextstring"The entered value is not valid."Error message when regex validation fails
trimbooleanfalseTrims whitespace before storing and validating
errorstringCustom error message. Field is marked invalid if set
visitedbooleanIf true, shows validation errors immediately
validationModestring"tooltip"How to show errors: "tooltip", "help", "help-block"
validationParamsobjectAdditional params passed to onValidate

Appearance

PropertyTypeDefaultDescription
iconstring/objectIcon on the left side. Can include onClick and tooltip
showClearbooleanfalseShows a clear button when the field has a value
hideClearbooleanfalseOpposite of showClear
alwaysShowClearbooleanfalseShows clear button even when field is required
tooltipstring/objectTooltip text or configuration
inputStylestring/objectCSS styles applied to the input element
inputClassstringCSS class applied to the input element
inputAttrsobjectAdditional HTML attributes for the input element
asteriskbooleanfalseAppends asterisk to label for required fields

Behavior

PropertyTypeDefaultDescription
autoFocusbooleanfalseAutomatically focuses the field on mount
tabOnEnterKeybooleanfalseMoves focus to the next field when Enter is pressed
trackFocusbooleanfalseAdds cxs-focus CSS class when input is focused
focusedbooleanBound value updated when field gains/loses focus
tabIndexstringCustom tab index
keyboardShortcutstringKeyboard shortcut to focus the field
idstringHTML id attribute for the input

Callbacks

PropertyDescription
onFocus(e, instance) => void - Called when field receives focus
onBlur(e, instance) => void - Called when field loses focus
onKeyDown(e, instance) => boolean - Return false to prevent default
onValidate(value, instance, validationParams) => string - Return error message or false to clear errors