Description
Field.String
is the base component for receiving user input where the target data is of type string
.
Demos
Empty
<Field.String onChange={(value) => console.log('onChange', value)} />
Placeholder
<Field.Stringplaceholder="Enter a text..."onChange={(value) => console.log('onChange', value)}/>
Label
<Field.Stringlabel="Label text"onChange={(value) => console.log('onChange', value)}/>
Label and value
<Field.Stringlabel="Label text"value="foo"onChange={(value) => console.log('onChange', value)}/>
Horizontal layout
<Field.Stringlabel="Label text"value="foo"layout="horizontal"onChange={(value) => console.log('onChange', value)}/>
Widths
<Field.Stringlabel="Default width (property omitted)"value="foo"onChange={(value) => console.log('onChange', value)}/><Field.Stringlabel="False (for browser default)"value="foo"width={false}onChange={(value) => console.log('onChange', value)}/><Field.Stringlabel="Medium"value="foo"width="medium"onChange={(value) => console.log('onChange', value)}/><Field.Stringlabel="Large"value="foo"width="large"onChange={(value) => console.log('onChange', value)}/><Field.Stringlabel="Stretch"value="foo"width="stretch"onChange={(value) => console.log('onChange', value)}/>
Icons
<Field.Stringlabel="Icon left"value="foo"leftIcon="check"onChange={(value) => console.log('onChange', value)}/><Field.Stringlabel="Icon right"value="foo"rightIcon="loupe"onChange={(value) => console.log('onChange', value)}/>
Character counter
 0
<Field.StringonChange={(value) => console.log('onChange', value)}characterCounter/>
3
<Field.Stringlabel="Label text"value="foo"onChange={(value) => console.log('onChange', value)}characterCounter/>
3/16
<Field.Stringlabel="Label text"value="foo"onChange={(value) => console.log('onChange', value)}maxLength={16}characterCounter/>
Clear
<Field.Stringvalue="foo"onChange={(value) => console.log('onChange', value)}clear/>
Disabled
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}disabled/>
Info
Useful information (?)
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}info="Useful information (?)"/>
Warning
I'm warning you...
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}warning={new FormError("I'm warning you...")}/>
Error
This is what is wrong...
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}error={new FormError('This is what is wrong...')}/>
Validation - Required
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}required/>
Validation - Minimum length
<Field.Stringvalue="foo"label="Label text (minimum 8 characters)"onChange={(value) => console.log('onChange', value)}minLength={8}/>
Validation - Maximum length and custom error message
<Field.Stringvalue="foo"label="Label text (maximum 8 characters)"onChange={(value) => console.log('onChange', value)}maxLength={8}errorMessages={{maxLength: "You can't write THAT long.. Max 8 chars!",}}/>
Validation - Pattern
<Field.Stringvalue="foo"label="Label text"onChange={(value) => console.log('onChange', value)}pattern="^foo123"/>
Synchronous external validator (called on every change)
<Field.Stringvalue="foo"label="Label text (minimum 4 characters)"validator={(value) =>value.length < 4 ? new FormError('At least 4 characters') : undefined}onChange={(value) => console.log('onChange', value)}/>
Asynchronous external validator (called on every change)
<Field.Stringvalue="foo"label="Label text (minimum 4 characters)"validator={(value) =>new Promise((resolve) =>setTimeout(() =>resolve(value.length < 5? new FormError('At least 5 characters'): undefined),1500))}onChange={(value) => console.log('onChange', value)}/>
Synchronous external validator (called on blur)
<Field.Stringvalue="foo"label="Label text (minimum 4 characters)"onBlurValidator={(value) =>value.length < 4 ? new FormError('At least 4 characters') : undefined}onChange={(value) => console.log('onChange', value)}/>
Asynchronous external validator (called on blur)
<Field.Stringvalue="foo"label="Label text (minimum 4 characters)"onBlurValidator={(value) =>new Promise((resolve) =>setTimeout(() =>resolve(value.length < 5? new FormError('At least 5 characters'): undefined),1500))}onChange={(value) => console.log('onChange', value)}/>
Multiline, empty
<Field.StringonChange={(value) => console.log('onChange', value)}multiline/>
Multiline, placeholder
<Field.Stringplaceholder="Enter text here"onChange={(value) => console.log('onChange', value)}multiline/>
Multiline, label & value
<Field.Stringvalue="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis in tempus odio, nec interdum orci. Integer vehicula ipsum et risus finibus, vitae commodo ex luctus. Nam viverra sollicitudin dictum. Vivamus maximus dignissim lorem, vitae viverra erat dapibus a."label="Label text"onChange={(value) => console.log('onChange', value)}multiline/>
Properties
Standard input component props
Property | Type | Description |
---|---|---|
data-testid | string | (optional) Test ID |
className | string | (optional) Outer DOM element class name |
value | string | (optional) Source data value for the input |
layout | string | (optional) Layout for the label and input. Can be horizontal or vertical |
label | string | (optional) Field label to show above / before the input feature |
labelDescription | string | (optional) A more discreet text displayed beside the label (i.e for "(optional)") |
labelSecondary | string | (optional) Secondary information displayed at the end of the label line (i.e character counter) |
placeholder | string | (optional) Text showing in place of the value if no value is given |
path | string | (optional) JSON Pointer for where the data for this input is located in the source dataset (when using DataContext) |
info | Error or string | (optional) Info message shown below / after the input |
warning | Error or string | (optional) Warning message shown below / after the input |
error | Error | (optional) Error message shown below / after the input |
disabled | boolean | (optional) Set true to show the field but without the possibility of changing the value. |
emptyValue | any | (optional) The value to use (in onChange events etc) when emptying the field. Makes it possible for instance to provide undefined instead of an empty string when clearing the content of a text input. |
required | boolean | (optional) When set true , the input will give an error if the value cannot be empty. |
schema | object | (optional) Custom JSON Schema for validating the value. |
validateInitially | string | (optional) Set true to show validation based errors initially (from given value-prop or source data) before the user interacts with the field. |
validateUnchanged | string | (optional) Set true to show validation based errors when the field is touched (like focusing a field and blurring) without having changed the value. Since the user did not introduce a new error, this will apply when the value was initially invalid based on validation. |
continuousValidation | string | (optional) Set true to show validation based errors continuously while writing, not just when blurring the field. |
errorMessages | object | (optional) Custom error messages for each type of error, overriding default messages. |
validator | function | (optional) Custom validator function that will be called for every change done by the user. Can be asynchronous or synchronous. |
onBlurValidator | function | (optional) Custom validator function that will be called when the user leaves the field (blurring a text input, closing a dropdown etc). Can be asynchronous or synchronous. |
toInput | function | (optional) Derivate called when the received / active value is sent to the input. Can be used for casting, changing syntax etc. |
fromInput | function | (optional) Derivate called when changes is made by the user, to cast or change syntax back to the original (opposite of toInput ). |
Component-specific props
Property | Type | Description |
---|---|---|
inputClassName | string | (optional) Class name set on the <input> DOM element |
type | string | (optional) Input DOM element type |
multiline | boolean | (optional) True to be able to write in multiple lines (switching from input-element to textarea-element) |
leftIcon | string | (optional) For icon at the left side of the text input |
rightIcon | string | (optional) For icon at the right side of the text input |
clear | boolean | (optional) True to have a clickable clear-icon for removing the active value |
autoresize | boolean | (optional) For multiline , set true to expand when writing longer texts. |
autoresizeMaxRows | boolean | (optional) For multiline , set how many rows of text can be shown at max. |
characterCounter | boolean | (optional) True to show a character counter. |
minLength | boolean | (optional) Validation for minimum length of the text (number of characters) |
maxLength | boolean | (optional) Validation for maximum length of the text (number of characters) |
pattern | boolean | (optional) Validation based on regex pattern |
width | string or false | (optional)false for no width (use browser default), medium or large for predefined standard widths, stretch for fill available width. |
space | Space | (optional) Spacing object with keys top , right , bottom and left |
Events
Event | Description |
---|---|
onChange | (optional) Will be called on value changes made by the user, with the new value as argument. |
onFocus | (optional) Will be called when the component gets into focus. Like clicking inside a text input or opening a dropdown. Called with active value as argument. |
onBlur | (optional) Will be called when the component stop being in focus. Like when going to next field, or closing a dropdown. Called with active value as argument. |