11import { LightningElement , api } from 'lwc' ;
22
3+ const DEFAULT_MESSAGE_WHEN_VALUE_MISSING = 'Complete this field.' ;
4+
35export default class CheckboxGroup extends LightningElement {
4- @api label = '' ;
5- @api readOnly = false ;
6- @api required = false ;
7- @api multiple = false ;
8- @api messageWhenValueMissing = 'Complete this field.' ;
6+ _label = '' ;
7+ _readOnly = false ;
8+ _required = false ;
9+ _multiple = false ;
10+ _validity = true ;
11+ _showError = false ;
12+ _customValidityErrorMsg = "" ;
13+ _messageWhenValueMissing = DEFAULT_MESSAGE_WHEN_VALUE_MISSING ;
14+ _checkboxes = [ ] ;
915
10- validity = true ;
11- showError = false ;
12- customValidityErrorMsg = "" ;
13- checkboxes = [ ] ;
16+ @ api
17+ get label ( ) {
18+ return this . _label ;
19+ }
1420
15- renderedCallback ( ) {
16- this . checkboxes . forEach ( ( checkbox ) => {
17- checkbox . readOnly = this . readOnly ;
18- } ) ;
21+ set label ( value ) {
22+ this . _label = value ;
1923 }
2024
21- get errorMessage ( ) {
22- return this . customValidityErrorMsg ? this . customValidityErrorMsg : this . messageWhenValueMissing ;
25+ @api
26+ get readOnly ( ) {
27+ return this . _readOnly ;
2328 }
2429
25- get formElementClass ( ) {
26- let classes = 'slds-form-element' ;
27- classes += this . showError ? ' slds-has-error' : '' ;
28- classes += this . readOnly ? ' read-only' : '' ;
29- return classes ;
30+ set readOnly ( value ) {
31+ this . _readOnly = ! ! value ;
3032 }
3133
32- get showLabel ( ) {
33- return ! ! this . label ;
34+ @api
35+ get required ( ) {
36+ return this . _required ;
37+ }
38+
39+ set required ( value ) {
40+ this . _required = ! ! value ;
41+ }
42+
43+ @api
44+ get multiple ( ) {
45+ return this . _multiple ;
46+ }
47+
48+ set multiple ( value ) {
49+ this . _multiple = ! ! value ;
50+ }
51+
52+ @api
53+ get messageWhenValueMissing ( ) {
54+ return this . _messageWhenValueMissing ;
55+ }
56+
57+ set messageWhenValueMissing ( value ) {
58+ this . _messageWhenValueMissing = value ? value : DEFAULT_MESSAGE_WHEN_VALUE_MISSING ;
3459 }
3560
3661 @api
3762 clear ( ) {
38- this . checkboxes . forEach ( ( checkbox ) => {
63+ this . _checkboxes . forEach ( ( checkbox ) => {
3964 checkbox . checked = false ;
4065 } ) ;
4166 }
4267
4368 @api
4469 blur ( ) {
45- this . checkboxes . forEach ( ( checkbox ) => {
70+ this . _checkboxes . forEach ( ( checkbox ) => {
4671 checkbox . blur ( ) ;
4772 } ) ;
4873 }
4974
5075 @api
5176 focus ( ) {
52- if ( this . checkboxes . length > 0 ) {
53- this . checkboxes [ 0 ] . focus ( ) ;
77+ if ( this . _checkboxes . length > 0 ) {
78+ this . _checkboxes [ 0 ] . focus ( ) ;
5479 }
5580 }
5681
5782 @api
5883 checkValidity ( ) {
59- if ( this . customValidityErrorMsg && ! this . readOnly ) {
60- this . validity = false ;
84+ if ( this . _customValidityErrorMsg && ! this . readOnly ) {
85+ this . _validity = false ;
6186 } else {
62- this . validity = this . checkboxes . reduce ( ( acc , cb ) => acc && cb . checkValidity ( ) , true ) ;
87+ this . _validity = true ;
6388
6489 if ( this . required && ! this . readOnly ) {
65- this . validity &= this . checkboxes . reduce ( ( acc , cb ) => acc || cb . checked , false ) ;
90+ this . _validity &= this . _checkboxes . reduce ( ( acc , cb ) => acc || cb . checked , false ) ;
6691 }
6792 }
6893
69- return this . validity ;
94+ return this . _validity ;
7095 }
7196
7297 @api
7398 reportValidity ( ) {
74- this . showError = ! this . checkValidity ( ) ;
75- return this . validity ;
99+ this . _showError = ! this . checkValidity ( ) ;
100+ return this . _validity ;
76101 }
77102
78103 @api
79104 setCustomValidity ( message ) {
80- this . customValidityErrorMsg = message ;
105+ this . _customValidityErrorMsg = message ;
81106 this . reportValidity ( ) ;
82- }
107+ }
108+
109+ renderedCallback ( ) {
110+ this . _checkboxes . forEach ( ( cb ) => {
111+ cb . bind ( this ) ;
112+ } ) ;
113+ }
83114
84115 handleChange = ( event ) => {
85116 if ( ! this . multiple && event . target . checked ) {
86- this . checkboxes . filter ( cb => cb !== event . target ) . forEach ( cb => {
117+ this . _checkboxes . filter ( cb => cb !== event . target ) . forEach ( cb => {
87118 cb . checked = false ;
88119 this . dispatchEvent ( new CustomEvent ( "change" , { detail : cb } ) ) ;
89120 } ) ;
@@ -112,7 +143,7 @@ export default class CheckboxGroup extends LightningElement {
112143 handleSlotChange ( event ) {
113144 const slot = event . target ;
114145
115- this . checkboxes = slot . assignedNodes ( { flatten : true } ) . filter ( node => {
146+ this . _checkboxes = slot . assignedNodes ( { flatten : true } ) . filter ( node => {
116147 if ( node . tagName !== 'C-CHECKBOX' ) {
117148 slot . removeChild ( node ) ;
118149 return false ;
@@ -121,8 +152,8 @@ export default class CheckboxGroup extends LightningElement {
121152 return true ;
122153 } ) ;
123154
124- this . checkboxes . forEach ( cb => {
125- cb . readOnly = this . readOnly ;
155+ this . _checkboxes . forEach ( cb => {
156+ cb . bind ( this ) ;
126157
127158 cb . removeEventListener ( 'change' , this . handleChange , true ) ;
128159 cb . addEventListener ( 'change' , this . handleChange , true ) ;
@@ -140,4 +171,19 @@ export default class CheckboxGroup extends LightningElement {
140171 cb . addEventListener ( 'blur' , this . handleBlur , true ) ;
141172 } ) ;
142173 }
174+
175+ get errorMessage ( ) {
176+ return this . _customValidityErrorMsg ? this . _customValidityErrorMsg : this . messageWhenValueMissing ;
177+ }
178+
179+ get formElementClass ( ) {
180+ let classes = 'slds-form-element' ;
181+ classes += this . _showError ? ' slds-has-error' : '' ;
182+ classes += this . readOnly ? ' read-only' : '' ;
183+ return classes ;
184+ }
185+
186+ get showLabel ( ) {
187+ return ! ! this . label ;
188+ }
143189}
0 commit comments