Skip to content

Commit be5ab3c

Browse files
committed
Add documentation for dynamicOptions
1 parent f5cd9a3 commit be5ab3c

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* [component](api/ui-schema/field/component.md)
1212
* [children](api/ui-schema/field/children.md)
1313
* [displayOptions](api/ui-schema/field/display-options.md)
14+
* [dynamicOptions](api/ui-schema/field/dynamic-options.md)
1415
* [errorHandler](api/ui-schema/field/error-handler.md)
1516
* [errorOptions](api/ui-schema/field/error-options.md)
1617
* [fieldOptions](api/ui-schema/field/field-options.md)

docs/api/ui-schema/field.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A `field` is an `object` and must have a [component](field/component.md) propert
99
* [component](field/component.md) <span style="color: red">\*</span>
1010
* [children](field/children.md)
1111
* [displayOptions](field/display-options.md)
12+
* [dynamicOptions](field/dynamic-options.md)
1213
* [errorHandler](field/error-handler.md)
1314
* [errorOptions](field/error-options.md)
1415
* [fieldOptions](field/field-options.md)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# dynamicOptions
2+
3+
Sometimes a field needs to have properties added/removed when a field has a certain value. This is where `dynamicOptions` comes into play.
4+
`dynamicOptions` can alter any of a [field's top level properties](../field.md), like `children`, `component`, `fieldOptions` etc. when the given `schema` is `true` or `false`. The behavior is the same as [displayOptions](displayOptions.md), however in contrast to `displayOptions` you can pass in an array of options by using an array.
5+
6+
There are 2 options: Full and Single.
7+
8+
## Full
9+
10+
The full model uses the entire form model as data.
11+
12+
```js
13+
// Option 1 - full JSON schema
14+
data() {
15+
return {
16+
uiSchema: [{
17+
component: 'div',
18+
children: [{
19+
component: 'div',
20+
dynamicOptions: {
21+
schema: {
22+
type: object,
23+
properties: {
24+
firstName: {
25+
type: 'string',
26+
minLength: 3
27+
}
28+
},
29+
required: ['firstName']
30+
},
31+
options: {
32+
// any of a field's top level property is valid here
33+
fieldOptions: {
34+
// any value that is valid in fieldOptions can be used here
35+
class: ['text-success']
36+
}
37+
}
38+
}
39+
}]
40+
}]
41+
}
42+
}
43+
```
44+
45+
## Single
46+
47+
The Full option can be a bit verbose when you only rely on a single field's model, and thus you set the `model` property on the `displayOptions` object to only use the value of that field's model.
48+
49+
```js
50+
// Option 2 - single schema
51+
data() {
52+
return {
53+
uiSchema: [{
54+
component: 'div',
55+
children: [{
56+
component: 'div',
57+
// Here we use an array of dynamic options, but you don't have to do that just to use the single schema
58+
// You can just as well use an object like in the "Full" example above.
59+
dynamicOptions: [
60+
{
61+
// Here we set to use the firstName model as the value to evaluate the schema against
62+
model: 'firstName',
63+
schema: {
64+
type: 'string',
65+
minLength: 3
66+
},
67+
options: {
68+
// any of a field's top level property is valid here
69+
fieldOptions: {
70+
// any value that is valid in fieldOptions can be used here
71+
class: ['text-success'],
72+
domProps: {
73+
innerHTML: 'Looking good, person with a first name with more than 3 characters!'
74+
}
75+
}
76+
}
77+
},
78+
{
79+
model: 'firstName',
80+
schema: {
81+
type: 'string',
82+
maxLength: 2
83+
},
84+
options: {
85+
// any of a field's top level property is valid here
86+
fieldOptions: {
87+
// any value that is valid in fieldOptions can be used here
88+
class: ['text-warning'],
89+
domProps: {
90+
innerHTML: 'Hey, that is cool! Your first name is less than or equal to 2 characters!'
91+
}
92+
}
93+
}
94+
}
95+
]
96+
}]
97+
}]
98+
}
99+
}
100+
```

0 commit comments

Comments
 (0)