Skip to main content

Default values

We can define default values to fields through the json. Note that the default value will be set to the input only if the input has been visible. If the input is never shown, the default value will not be set in the data. The default value will be set if the data does not exists, if it exists but his value is null, the default value will not be set. It"s the same if the value is an empty string.

When the default value is set

warning

The default behaviour when checking if the default value should be set is to check if the data is nil, so if the data is null, undefined, the default value will be set.

But sometimes we might have the value of the field explicitly set to null, by a other field or by a onValueChange set. In this case, the default value will be set and that may be not what we want.

To avoid this, we can set the property strictDefault: true to the default value object. In this case, the default value will be set only if the data is undefined. So only when the data have not been set yet.

{
"component": "text",
"label": "First name with strict default value",
"default": {
"value": "Bernard",
"strictDefaultValue": true
}
}

Storybook example

Strict Default Value#

Global Strict Default Value#

Hardcoded default value

We can simply set a hardcoded default value

{
"component": "date",
"label": "Birthdate",
"default": {
"value": "1955-02-05"
}
}

The date input will have 1955-02-05 as value we it will be rendered.

Default value coming from other data

We can set a default value based on an other data

{
"component": "text",
"label": "Default value from other data",
"default": {
"from": "default_string"
}
}

Storybook example

Default Values#

Specified Strict Default Value#