Skip to content

Commit 6a08140

Browse files
committed
Handle dropdown state on initial render
Make sure to set first option as value, if this is what is displayed on first view. Essential for conditional visibility checks before state gets updated.
1 parent 0c04657 commit 6a08140

5 files changed

Lines changed: 23 additions & 2 deletions

File tree

cypress/e2e/visible.cy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ describe('Conditional Visibility Tests', () => {
33
cy.visit('/visible');
44
});
55

6+
afterEach(() => {
7+
cy.clearOPFS();
8+
});
9+
610
it('should show/hide text fields based on input conditions', () => {
711
// Initially, the third field should be hidden
812
cy.get('[name="fset1.text3"]').should('not.exist');
@@ -53,6 +57,10 @@ describe('Conditional Visibility Tests', () => {
5357
cy.get('[name="fset2.text4"]').should('not.exist');
5458
});
5559

60+
it('should show field based on initial value of dropdown, state not changed yet', () => {
61+
cy.get('markdown-component').contains('I am visible when first dropdown says').should('be.visible');
62+
});
63+
5664
it('should show/hide fields based on mathematical conditions', () => {
5765
// Initially, the secret number field should be hidden
5866
cy.get('[name="fset3.num3"]').should('not.exist');

cypress/yaml/visible/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ form:
3434
- hide
3535
- show
3636
- unhide
37+
static1:
38+
type: markdown
39+
markdown: I am visible when first dropdown says **hide**
40+
visible: fset2.drop1 == 'hide'
3741
text4:
3842
type: textinput
3943
label: "Sixth"

doc/conditional.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Conditional visibility
22

3-
Visibility can be configured for most elements. The configuration option `visible` defines a condition that must be met if the elements is to be displayed.
3+
Visibility can be configured for most elements. The configuration option `visible` defines a condition that must be met if the element is to be displayed.
4+
5+
**Current limitation:** at present all visibility checks are conducted just before an element is rendered for the first time and then on every state change - when some value is updated. So on initial form view, before any interactions, you can only query fields that come **before** your element.
46

57
## YAML
68

@@ -33,6 +35,8 @@ visible: full.dotted.elementId + full.dotted.another.elementId > 100
3335
3436
Fields contained in a table may not be dependent on other table fields. That would potentially break the table, because the columns would not be identical (some columns could have extra conditional fields).
3537
38+
Also, field names in tables are created dynamically, so you cannot use them in visibility conditions. This affects conditional options in dropdowns, too. Their visibility may not depend on a field in a table.
39+
3640
A field in a table can depend on a field outside the table. If the visibility condition is met, a new row will be displayed in all columns.
3741
3842
## Exception: Clone

doc/formelements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ Representation of a select input.
364364

365365
Options:
366366
* **EITHER** `choices` _(optionally required)_ - defines available options. Markdown is supported.
367-
* **OR** `conditional_choices` _(optionally required)_ - defines options available if a condition is met (depending on the value of another form field). This option has priority over `choices`!
367+
* **OR** `conditional_choices` _(optionally required)_ - defines options available if a condition is met (depending on the value of another form field). This option has priority over `choices`! Refer to [Conditional visibility](conditional.md) for explanation and limitations.
368368
* `empty_label` _(optional)_ - a placeholder text shown if no value has been chosen (e.g. "Please select"). **Note:** this is not a real option and has no value that could be saved. In multiselect fields it is irrelevant and ignored.
369369
* `multiselect` _(optional)_ - enables selecting multiple options
370370
* `size` _(optional)_ - if multiselect is turned on this defines the number of rows shown, otherwise ignored

js_src/components/DropdownComponent.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ export class DropdownComponent extends BaseComponent {
159159
myState.value = config.default;
160160
}
161161

162+
// without empty_label config, first option is the value
163+
if (!myState.value && !config.empty_label && !config.multiselect) {
164+
myState.value = config.choices[0];
165+
}
166+
162167
if (config.multiselect) {
163168
myState.value = U.stateMultivalue(myState.value);
164169

0 commit comments

Comments
 (0)