@@ -87,15 +87,15 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
87
87
}
88
88
89
89
_enumTemplate ( ) {
90
- const { model, name, readOnly, disabled, value, outlined, legacy } = this ;
90
+ const { model, name, readOnly, disabled, value, outlined, legacy, _nilEnabled } = this ;
91
91
const values = model . schema . enum || [ ] ;
92
92
return html `
93
93
< anypoint-dropdown-menu
94
94
name ="${ name } "
95
- ?required ="${ model . required } "
95
+ ?required ="${ ! _nilEnabled && model . required } "
96
96
autovalidate
97
97
data-type ="enum "
98
- ?disabled ="${ readOnly || disabled } "
98
+ ?disabled ="${ readOnly || disabled || _nilEnabled } "
99
99
?outlined ="${ outlined } "
100
100
?legacy ="${ legacy } ">
101
101
< label slot ="label "> ${ model . schema . inputLabel } </ label >
@@ -111,15 +111,15 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
111
111
}
112
112
113
113
_booleanTemplate ( ) {
114
- const { model, name, readOnly, disabled, value, outlined, legacy } = this ;
114
+ const { model, name, readOnly, disabled, value, outlined, legacy, _nilEnabled } = this ;
115
115
const bindValue = ( value === true || value === 'true' ) ? 'true' : 'false' ;
116
116
return html `
117
117
< anypoint-dropdown-menu
118
118
name ="${ name } "
119
- ?required ="${ model . required } "
119
+ ?required ="${ ! _nilEnabled && model . required } "
120
120
autovalidate
121
121
data-type ="boolean "
122
- ?disabled ="${ readOnly || disabled } "
122
+ ?disabled ="${ readOnly || disabled || _nilEnabled } "
123
123
?outlined ="${ outlined } "
124
124
?legacy ="${ legacy } ">
125
125
< label slot ="label "> ${ model . schema . inputLabel } </ label >
@@ -136,7 +136,7 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
136
136
}
137
137
138
138
_inputTemplate ( ) {
139
- const { model, name, noLabelFloat, readOnly, disabled, value, outlined, legacy } = this ;
139
+ const { model, name, noLabelFloat, readOnly, disabled, value, outlined, legacy, _nilEnabled } = this ;
140
140
if ( ! model ) {
141
141
return ;
142
142
}
@@ -145,7 +145,7 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
145
145
}
146
146
return html `< anypoint-input
147
147
.value ="${ value } "
148
- ?required ="${ model . required } "
148
+ ?required ="${ ! _nilEnabled && model . required } "
149
149
.pattern ="${ model . schema . pattern } "
150
150
.name ="${ name } "
151
151
autovalidate
@@ -157,17 +157,18 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
157
157
.placeholder ="${ model . schema . inputPlaceholder } "
158
158
?nolabelfloat ="${ noLabelFloat } "
159
159
?readonly ="${ readOnly } "
160
- ?disabled ="${ disabled } "
160
+ ?disabled ="${ disabled || _nilEnabled } "
161
161
?outlined ="${ outlined } "
162
162
?legacy ="${ legacy } "
163
163
data-type ="input "
164
- @input ="${ this . _inputHandler } ">
164
+ @input ="${ this . _inputHandler } "
165
+ invalidmessage ="${ `${ name } did not pass validation` } ">
165
166
< label slot ="label "> ${ model . schema . inputLabel } </ label >
166
167
</ anypoint-input > ` ;
167
168
}
168
169
169
170
_arrayTemplate ( ) {
170
- const { model, name, readOnly, disabled, _arrayValue, outlined, legacy } = this ;
171
+ const { model, name, readOnly, disabled, _arrayValue, outlined, legacy, _nilEnabled } = this ;
171
172
const values = _arrayValue || [ ] ;
172
173
const itemLabel = model . schema . inputLabel || 'Parameter value' ;
173
174
return html `
@@ -177,7 +178,7 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
177
178
< div class ="array-item ">
178
179
< anypoint-input
179
180
.value ="${ item . value } "
180
- ?required ="${ model . required } "
181
+ ?required ="${ ! _nilEnabled && model . required } "
181
182
.pattern ="${ model . schema . pattern } "
182
183
.name ="${ name } "
183
184
autovalidate
@@ -188,12 +189,13 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
188
189
.minLength ="${ model . schema . minLength } "
189
190
nolabelfloat
190
191
?readonly ="${ readOnly } "
191
- ?disabled ="${ disabled } "
192
+ ?disabled ="${ disabled || _nilEnabled } "
192
193
?outlined ="${ outlined } "
193
194
?legacy ="${ legacy } "
194
195
data-type ="array "
195
196
data-index ="${ index } "
196
- @input ="${ this . _arrayValueHandler } ">
197
+ @input ="${ this . _arrayValueHandler } "
198
+ invalidmessage ="${ `${ name } did not pass validation` } ">
197
199
< label slot ="label "> ${ itemLabel } < label >
198
200
</ anypoint-input >
199
201
${ index ? html `< anypoint-icon-button
@@ -222,15 +224,15 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
222
224
}
223
225
224
226
render ( ) {
225
- const { readOnly, disabled, _isEnum, _isBoolean, _isInput, _isArray, _renderNillable } = this ;
227
+ const { readOnly, disabled, _isEnum, _isBoolean, _isInput, _isArray, _isNillable } = this ;
226
228
return html `
227
229
< div class ="content ">
228
230
${ _isEnum ? this . _enumTemplate ( ) : undefined }
229
231
${ _isBoolean ? this . _booleanTemplate ( ) : undefined }
230
232
${ _isInput ? this . _inputTemplate ( ) : undefined }
231
233
${ _isArray ? this . _arrayTemplate ( ) : undefined }
232
234
233
- ${ _renderNillable ? html `< anypoint-checkbox
235
+ ${ _isNillable ? html `< anypoint-checkbox
234
236
?disabled ="${ readOnly || disabled } "
235
237
class ="nil-option "
236
238
@checked-changed ="${ this . _nillableChanged } "> Nil</ anypoint-checkbox > ` : undefined }
@@ -272,10 +274,8 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
272
274
// Computed value, True if current item is an array object
273
275
_isArray : { type : Boolean } ,
274
276
// Computed value, True if current item is an union with nill value.
275
- _isNillable : {
276
- type : Boolean ,
277
- reflectToAttribute : true
278
- } ,
277
+ _isNillable : { type : Boolean } ,
278
+ _nilEnabled : { type : Boolean } ,
279
279
// Computed value, True if current item is a boolean value
280
280
_isBoolean : { type : Boolean } ,
281
281
// A value of an array item (only if `isArray` is set)
@@ -291,9 +291,7 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
291
291
/**
292
292
* When set the editor renders form controls disabled.
293
293
*/
294
- disabled : { type : Boolean } ,
295
- // Computed value, renders nillable switch when needed.
296
- _renderNillable : { type : Boolean }
294
+ disabled : { type : Boolean }
297
295
} ;
298
296
}
299
297
@@ -347,7 +345,6 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
347
345
return ;
348
346
}
349
347
this . __isArray = value ;
350
- this . _renderNillable = this . _computeRenderNillable ( this . _isNillable , value ) ;
351
348
this . _isArrayChanged ( value , this . value ) ;
352
349
if ( value ) {
353
350
this . setAttribute ( 'isarray' , '' ) ;
@@ -367,7 +364,6 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
367
364
return ;
368
365
}
369
366
this . __isNillable = value ;
370
- this . _renderNillable = this . _computeRenderNillable ( value , this . _isArray ) ;
371
367
if ( value ) {
372
368
this . setAttribute ( 'isnillable' , '' ) ;
373
369
} else {
@@ -525,62 +521,64 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
525
521
}
526
522
return ! ! m . value ;
527
523
}
528
-
524
+ /**
525
+ * Returns input(s) depending on model type.
526
+ * @return {Element|NodeList|undefined } Returns an element for input, enum, and
527
+ * boolean types. Returns NodeList for array type. Returns undefined when model is not set
528
+ * or DOM is not ready.
529
+ */
530
+ _getInputElement ( ) {
531
+ if ( this . _isInput ) {
532
+ return this . shadowRoot . querySelector ( 'anypoint-input[data-type="input"]' ) ;
533
+ }
534
+ if ( this . _isBoolean ) {
535
+ return this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="boolean"]' ) ;
536
+ }
537
+ if ( this . _isEnum ) {
538
+ return this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="enum"]' ) ;
539
+ }
540
+ if ( this . _isArray ) {
541
+ return this . shadowRoot . querySelectorAll ( 'anypoint-input[data-type="array"]' ) ;
542
+ }
543
+ }
544
+ /**
545
+ * Overrides `ValidatableMixin._getValidity`.
546
+ * If the element is set to be `NIL` value it always returns true.
547
+ * Otherwise it calls `_getInputsValidity()` for input validation result.
548
+ * @return {Boolean } Validation result
549
+ */
529
550
_getValidity ( ) {
530
551
if ( this . _nilEnabled ) {
531
552
return true ;
532
553
}
533
- switch ( true ) {
534
- case this . _isInput :
535
- {
536
- const input = this . shadowRoot . querySelector ( 'anypoint-input[data-type="input"]' ) ;
537
- return input ? input . validate ( ) : this . _defaultValidator ( ) ;
538
- }
539
- case this . _isBoolean :
540
- {
541
- const bool = this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="boolean"]' ) ;
542
- return bool ? bool . validate ( ) : this . _defaultValidator ( ) ;
543
- }
544
- case this . _isEnum :
545
- {
546
- const en = this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="enum"]' ) ;
547
- return en ? en . validate ( ) : this . _defaultValidator ( ) ;
548
- }
549
- case this . _isArray :
550
- {
551
- const inputs = this . shadowRoot . querySelectorAll ( 'anypoint-input[data-type="array"]' ) ;
552
- for ( let i = 0 ; i < inputs . length ; i ++ ) {
553
- if ( ! inputs [ i ] . validate ( ) ) {
554
- return false ;
555
- }
556
- }
557
- return true ;
558
- }
559
- default :
560
- return this . _defaultValidator ( ) ;
561
- }
554
+ return this . _getInputsValidity ( ) ;
562
555
}
563
556
/**
564
- * Computes value for `_renderNillable` property.
565
- *
566
- * @param {Boolean } isNillable
567
- * @param {Boolean } isArray
557
+ * Validates the inputs and returns validation state.
568
558
* @return {Boolean }
569
559
*/
570
- _computeRenderNillable ( isNillable , isArray ) {
571
- return ! ! isNillable && ! isArray ;
560
+ _getInputsValidity ( ) {
561
+ const node = this . _getInputElement ( ) ;
562
+ if ( ! node ) {
563
+ return this . _defaultValidator ( ) ;
564
+ }
565
+ if ( this . _isArray ) {
566
+ for ( let i = 0 ; i < node . length ; i ++ ) {
567
+ if ( ! node [ i ] . validate ( ) ) {
568
+ return false ;
569
+ }
570
+ }
571
+ return true ;
572
+ }
573
+ return node . validate ( ) ;
572
574
}
573
575
/**
574
576
* Controls value and input state when "nil" checkbox's value change.
575
577
* @param {CustomEvent } e
576
578
*/
577
- _nillableChanged ( e ) {
579
+ async _nillableChanged ( e ) {
578
580
const { value } = e . detail ;
579
581
this . _nilEnabled = value ;
580
- const input = this . _getInput ( ) ;
581
- if ( input ) {
582
- input . disabled = value ;
583
- }
584
582
if ( value ) {
585
583
this . _oldNilValue = this . value ;
586
584
this . value = 'nil' ;
@@ -592,37 +590,8 @@ class ApiPropertyFormItem extends ValidatableMixin(LitElement) {
592
590
this . value = '' ;
593
591
}
594
592
}
595
- }
596
- /**
597
- * Finds input element in the DOM
598
- * @return {Element|undefined } An element that represents the main UI input
599
- * element or undefined for array types.
600
- */
601
- _getInput ( ) {
602
- if ( this . _isEnum ) {
603
- return this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="enum"]' ) ;
604
- }
605
- if ( this . _isBoolean ) {
606
- return this . shadowRoot . querySelector ( 'anypoint-dropdown-menu[data-type="boolean"]' ) ;
607
- }
608
- if ( this . _isInput ) {
609
- return this . shadowRoot . querySelector ( 'anypoint-input[data-type="input"]' ) ;
610
- }
611
- }
612
- /**
613
- * Computes value for anypoint-input's always-float-label attribute.
614
- * It forces label float for some types of inputs.
615
- * @param {Boolean } inputFloatLabel
616
- * @param {String } inputType
617
- * @return {Boolean }
618
- */
619
- _computeAlwaysFloatLabel ( inputFloatLabel , inputType ) {
620
- if ( inputFloatLabel ) {
621
- return inputFloatLabel ;
622
- }
623
- return [
624
- 'date' , 'datetime' , 'datetime-local' , 'month' , 'time' , 'week' , 'file'
625
- ] . indexOf ( inputType ) !== - 1 ;
593
+ await this . updateComplete ;
594
+ this . _getInputsValidity ( ) ;
626
595
}
627
596
628
597
_listSelectionHandler ( e ) {
0 commit comments