11import classNames from 'classnames' ;
22import isEqual from 'lodash/isEqual' ;
3+ import nanoid from 'nanoid' ;
34import PropTypes from 'prop-types' ;
45import React from 'react' ;
5- import nanoid from 'nanoid' ;
66
77import Button from './Button' ;
88import TreeNode from './TreeNode' ;
@@ -80,7 +80,7 @@ class CheckboxTree extends React.Component {
8080 this . nodes = { } ;
8181
8282 this . flattenNodes ( props . nodes ) ;
83- this . unserializeLists ( {
83+ this . deserializeLists ( {
8484 checked : props . checked ,
8585 expanded : props . expanded ,
8686 } ) ;
@@ -96,7 +96,7 @@ class CheckboxTree extends React.Component {
9696 this . flattenNodes ( nodes ) ;
9797 }
9898
99- this . unserializeLists ( { checked, expanded } ) ;
99+ this . deserializeLists ( { checked, expanded } ) ;
100100 }
101101
102102 onCheck ( node ) {
@@ -122,14 +122,16 @@ class CheckboxTree extends React.Component {
122122 }
123123
124124 getFormattedNodes ( nodes ) {
125+ const nodeMap = this . nodes ;
126+
125127 return nodes . map ( ( node ) => {
126128 const formatted = { ...node } ;
127129
128- formatted . checked = this . nodes [ node . value ] . checked ;
129- formatted . expanded = this . nodes [ node . value ] . expanded ;
130+ formatted . checked = nodeMap [ node . value ] . checked ;
131+ formatted . expanded = nodeMap [ node . value ] . expanded ;
130132 formatted . showCheckbox = node . showCheckbox !== undefined ? node . showCheckbox : true ;
131133
132- if ( this . nodes [ node . value ] . isParent ) {
134+ if ( nodeMap [ node . value ] . isParent ) {
133135 formatted . children = this . getFormattedNodes ( formatted . children ) ;
134136 } else {
135137 formatted . children = null ;
@@ -215,15 +217,15 @@ class CheckboxTree extends React.Component {
215217 } ) ;
216218 }
217219
218- unserializeLists ( lists ) {
220+ deserializeLists ( lists ) {
219221 // Reset values to false
220222 Object . keys ( this . nodes ) . forEach ( ( value ) => {
221223 Object . keys ( lists ) . forEach ( ( listKey ) => {
222224 this . nodes [ value ] [ listKey ] = false ;
223225 } ) ;
224226 } ) ;
225227
226- // Unserialize values and set their nodes to true
228+ // Deserialize values and set their nodes to true
227229 Object . keys ( lists ) . forEach ( ( listKey ) => {
228230 lists [ listKey ] . forEach ( ( value ) => {
229231 if ( this . nodes [ value ] !== undefined ) {
@@ -375,26 +377,29 @@ class CheckboxTree extends React.Component {
375377 }
376378
377379 renderArrayHiddenInput ( ) {
378- return this . props . checked . map ( ( value ) => {
379- const name = `${ this . props . name } []` ;
380+ const { checked, name : inputName } = this . props ;
381+
382+ return checked . map ( ( value ) => {
383+ const name = `${ inputName } []` ;
380384
381385 return < input key = { value } name = { name } type = "hidden" value = { value } /> ;
382386 } ) ;
383387 }
384388
385389 renderJoinedHiddenInput ( ) {
386- const checked = this . props . checked . join ( ',' ) ;
390+ const { checked, name } = this . props ;
391+ const inputValue = checked . join ( ',' ) ;
387392
388- return < input name = { this . props . name } type = "hidden" value = { checked } /> ;
393+ return < input name = { name } type = "hidden" value = { inputValue } /> ;
389394 }
390395
391396 render ( ) {
392- const nodes = this . getFormattedNodes ( this . props . nodes ) ;
393- const treeNodes = this . renderTreeNodes ( nodes ) ;
397+ const { disabled , nodes, nativeCheckboxes } = this . props ;
398+ const treeNodes = this . renderTreeNodes ( this . getFormattedNodes ( nodes ) ) ;
394399 const className = classNames ( {
395400 'react-checkbox-tree' : true ,
396- 'rct-disabled' : this . props . disabled ,
397- 'rct-native-display' : this . props . nativeCheckboxes ,
401+ 'rct-disabled' : disabled ,
402+ 'rct-native-display' : nativeCheckboxes ,
398403 } ) ;
399404
400405 return (
0 commit comments