Skip to main content

TextField: React

A TextField is an interactive control used in forms to collect data from the user in a single-line text field.

Installation

Before you can start using the @mediahuis/chameleon-react component, you’ll need to install it, for more information check: Getting started for developers.

With that done, you’re now ready to implement the component in your application.

import { TextField } from '@mediahuis/chameleon-react';

export default function Example() {
return <TextField id="textfield-1" label="TextField Example" />;
}

Properties

PropertyTypeDefaultRequiredDescription
accessibilityLabelClearButtonString'Clear'NoThe a11y label for the clearButton.
actionReactNode-NoContents displayed within the ActionLabel.
classNameString-NoAllows extending the class names of the component.
clearButtonBoolean-NoShow or hide clearButton.
disabledBoolean-NoSets the HTML disabled attribute.
errorBoolean-NoVisually displayed as error text. Adds necessary accessibility tags.
iconLeftSVGType-NoName of the icon on the left (extends Icon), choose one of the icons package.
iconLeftComponentReactNode-NoiconLeftComponent replaces iconLeft, you can add any component here.
iconRightSVGType-NoName of the icon on the right (extends Icon), choose one of the icons package.
iconRightComponentReactNode-NoiconRightComponent replaces iconRight, you can add any component here.
idString-YesNative HTML id attribute. Also affects label-id and message-id for accessibility reasons.
labelReactNode-YesContents displayed within the PrimaryLabel.
labelHiddenBoolean-NoVisually hide the label. (label is still required for a11y)
labelPropsGenericComponentPropsWithoutAs{}NoProps passed to the label component.
maskString-NoHelps the user with the input by ensuring a predefined format. This can be useful for dates, numerics, phone numbers, etc.
messageString-NoContents displayed beneath the input.
nameString-NoName of the form element.
onBlurFocusEventHandler-NoFires when the TextField loses focus.
onChangeChangeEventHandler-NoWill trigger when the TextField's value changes.
onFocusFocusEventHandler-NoFires when the TextField receives focus.
optionalLabelString-NoContents displayed within the OptionalLabel.
placeholderString-No
successBoolean-NoTextField is successfully filled in.
typeString'text'NoThe type of the input.
valueString | Number-NoThe value of the input.
styleStyleProps-No

Mask

To visually aid the user with the expected format, you can use an input mask. Input masks will display a template of the expected value. The mask is built out of several definitions:

  • 9: numeric
  • a: alphabetical
  • *: alphanumeric

This example shows a controlled input with a mask. Note that we use defaultValue instead of value here.

() => {
const [value, setValue] = useState('');

const onChange = event => {
setValue(event.target.value);
};

return (
<>
<div>Input value: {value}</div>
<TextField
id="optional-label"
label="Enter your billing details"
mask="BE99 9999 9999 9999"
defaultValue="BE"
onChange={onChange}
/>
</>
);
};

required

Chameleon form inputs are required by default, with the required attribute automatically set. To mark a field as optional, pass the optionalLabel prop to remove the required attribute.