1
+ 'use strict' ;
2
+
3
+ Object . defineProperty ( exports , "__esModule" , {
4
+ value : true
5
+ } ) ;
6
+
7
+ var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
8
+
9
+ var _templateObject = _taggedTemplateLiteral ( [ '\n width: 100%;\n' ] , [ '\n width: 100%;\n' ] ) ;
10
+
11
+ var _react = require ( 'react' ) ;
12
+
13
+ var _react2 = _interopRequireDefault ( _react ) ;
14
+
15
+ var _propTypes = require ( 'prop-types' ) ;
16
+
17
+ var _propTypes2 = _interopRequireDefault ( _propTypes ) ;
18
+
19
+ var _InputBox = require ( './../InputBox' ) ;
20
+
21
+ var _InputBox2 = _interopRequireDefault ( _InputBox ) ;
22
+
23
+ var _util = require ( './util' ) ;
24
+
25
+ var _lodash = require ( 'lodash' ) ;
26
+
27
+ var _styledComponents = require ( 'styled-components' ) ;
28
+
29
+ var _styledComponents2 = _interopRequireDefault ( _styledComponents ) ;
30
+
31
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
32
+
33
+ function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( "Cannot call a class as a function" ) ; } }
34
+
35
+ function _possibleConstructorReturn ( self , call ) { if ( ! self ) { throw new ReferenceError ( "this hasn't been initialised - super() hasn't been called" ) ; } return call && ( typeof call === "object" || typeof call === "function" ) ? call : self ; }
36
+
37
+ function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
38
+
39
+ function _taggedTemplateLiteral ( strings , raw ) { return Object . freeze ( Object . defineProperties ( strings , { raw : { value : Object . freeze ( raw ) } } ) ) ; }
40
+
41
+ var Container = _styledComponents2 . default . div ( _templateObject ) ;
42
+
43
+ var ReactSelectionTree = function ( _Component ) {
44
+ _inherits ( ReactSelectionTree , _Component ) ;
45
+
46
+ function ReactSelectionTree ( props ) {
47
+ _classCallCheck ( this , ReactSelectionTree ) ;
48
+
49
+ var _this = _possibleConstructorReturn ( this , ( ReactSelectionTree . __proto__ || Object . getPrototypeOf ( ReactSelectionTree ) ) . call ( this , props ) ) ;
50
+
51
+ _this . state = {
52
+ data : false ,
53
+ originalData : false ,
54
+ flatData : false
55
+ } ;
56
+ return _this ;
57
+ }
58
+
59
+ _createClass ( ReactSelectionTree , [ {
60
+ key : 'componentDidMount' ,
61
+ value : function componentDidMount ( ) {
62
+ this . load ( ) ;
63
+ }
64
+ } , {
65
+ key : 'load' ,
66
+ value : function load ( ) {
67
+ var data = this . props . data ;
68
+ var flatData = ( 0 , _util . flattenNode ) ( data , false ) ;
69
+ this . setState ( {
70
+ data : data ,
71
+ flatData : flatData ,
72
+ originalData : ( 0 , _lodash . merge ) ( { } , this . state . data )
73
+ } ) ;
74
+ }
75
+ } , {
76
+ key : 'reset' ,
77
+ value : function reset ( ) {
78
+ var flatData = this . state . flatData ;
79
+ Object . keys ( flatData ) . forEach ( function ( key ) {
80
+ if ( flatData [ key ] && typeof flatData [ key ] . checked !== 'undefined' ) {
81
+ flatData [ key ] . checked = false ;
82
+ }
83
+ } ) ;
84
+ var rootData = this . state . data ;
85
+ while ( rootData . parent && rootData . parent !== null ) {
86
+ rootData = rootData . parent ;
87
+ }
88
+ rootData . search_result = undefined ;
89
+ this . setState ( {
90
+ data : Object . assign ( { } , rootData ) ,
91
+ flatData : Object . assign ( { } , this . state . flatData )
92
+ } ) ;
93
+ }
94
+ } , {
95
+ key : 'update' ,
96
+ value : function update ( data , callback ) {
97
+ this . setState ( {
98
+ data : Object . assign ( { } , data ) ,
99
+ flatData : Object . assign ( { } , this . state . flatData )
100
+ } , function ( ) {
101
+ if ( callback ) {
102
+ callback ( data ) ;
103
+ }
104
+ } ) ;
105
+ }
106
+ } , {
107
+ key : 'updateOriginalFromData' ,
108
+ value : function updateOriginalFromData ( ) {
109
+ ( 0 , _util . copyTreeState ) ( this . state . originalData , this . state . data ) ;
110
+ this . setState ( {
111
+ originalData : Object . assign ( { } , this . state . originalData )
112
+ } ) ;
113
+ }
114
+ } , {
115
+ key : 'updateDataFromOriginal' ,
116
+ value : function updateDataFromOriginal ( ) {
117
+ ( 0 , _util . copyTreeState ) ( this . state . data , this . state . originalData ) ;
118
+ var rootNode = Object . assign ( { } , this . state . data ) ;
119
+ this . setState ( {
120
+ data : rootNode ,
121
+ flatData : ( 0 , _util . flattenNode ) ( rootNode )
122
+ } ) ;
123
+ }
124
+ } , {
125
+ key : 'checkEmpty' ,
126
+ value : function checkEmpty ( ) {
127
+ return this . state . data && this . state . data !== null && this . state . data . children && this . state . data . children !== null && Object . keys ( this . state . data . children ) . length === 0 ;
128
+ }
129
+ } , {
130
+ key : 'render' ,
131
+ value : function render ( ) {
132
+ var _this2 = this ;
133
+
134
+ return _react2 . default . createElement (
135
+ Container ,
136
+ null ,
137
+ this . checkEmpty ( ) && _react2 . default . createElement (
138
+ 'p' ,
139
+ null ,
140
+ 'There are no available data.'
141
+ ) ,
142
+ ! this . checkEmpty ( ) && this . state . data && _react2 . default . createElement ( _InputBox2 . default , {
143
+ name : this . props . name ,
144
+ filter : this . state . data ,
145
+ checkFilter : function checkFilter ( name , filter , checked ) {
146
+ filter . checked = checked ;
147
+ if ( checked ) {
148
+ ( 0 , _util . flattenChildren ) ( filter , true ) ;
149
+ }
150
+ if ( ! checked ) {
151
+ ( 0 , _util . flattenParent ) ( filter , false ) ;
152
+ ( 0 , _util . flattenChildren ) ( filter , false ) ;
153
+ }
154
+ _this2 . update ( filter . parent && filter . parent !== null ? filter . parent : filter , _this2 . props . check ) ;
155
+ } ,
156
+ updateFilter : function updateFilter ( data ) {
157
+ _this2 . update ( data ) ;
158
+ _this2 . props . breadcrumClick ( data ) ;
159
+ } ,
160
+ searchFilter : function searchFilter ( value ) {
161
+ var filter = _this2 . state . data ;
162
+ var result = [ ] ;
163
+ Object . keys ( _this2 . state . flatData ) . forEach ( function ( key ) {
164
+ if ( result . length < 15 && _this2 . state . flatData [ key ] . name . toLowerCase ( ) . indexOf ( value . toLowerCase ( ) ) !== - 1 ) {
165
+ result . push ( _this2 . state . flatData [ key ] ) ;
166
+ }
167
+ } ) ;
168
+ while ( filter . parent && filter . parent !== null ) {
169
+ filter = _this2 . state . data . parent ;
170
+ }
171
+ filter . search_result = result ;
172
+ _this2 . update ( filter , _this2 . props . search ) ;
173
+ } ,
174
+ resetSearch : function resetSearch ( ) {
175
+ var filter = _this2 . state . data ;
176
+ while ( filter . parent && filter . parent !== null ) {
177
+ filter = _this2 . state . data . parent ;
178
+ }
179
+ filter . search_result = [ ] ;
180
+ _this2 . update ( filter , _this2 . props . reset ) ;
181
+ } ,
182
+ uncheckAll : function uncheckAll ( ) {
183
+ var filter = _this2 . state . data ;
184
+ var flatData = _this2 . state . flatData ;
185
+ Object . keys ( flatData ) . forEach ( function ( key ) {
186
+ flatData [ key ] . checked = false ;
187
+ } ) ;
188
+ while ( filter . parent && filter . parent !== null ) {
189
+ filter = _this2 . state . data . parent ;
190
+ }
191
+ _this2 . update ( filter , _this2 . props . uncheckAll ) ;
192
+ } ,
193
+ clearall : this . props . showClearAll ,
194
+ search : this . props . showSearch ,
195
+ multipleSelect : this . props . multipleSelect
196
+ } )
197
+ ) ;
198
+ }
199
+ } ] ) ;
200
+
201
+ return ReactSelectionTree ;
202
+ } ( _react . Component ) ;
203
+
204
+ ReactSelectionTree . propTypes = {
205
+ name : _propTypes2 . default . string . isRequired ,
206
+ data : _propTypes2 . default . object . isRequired ,
207
+ excludeSelect : _propTypes2 . default . array ,
208
+
209
+ breadcrumClick : _propTypes2 . default . func . isRequired ,
210
+ check : _propTypes2 . default . func . isRequired ,
211
+ search : _propTypes2 . default . func . isRequired ,
212
+ reset : _propTypes2 . default . func . isRequired ,
213
+ uncheckAll : _propTypes2 . default . func . isRequired ,
214
+ showSearch : _propTypes2 . default . bool ,
215
+ showClearAll : _propTypes2 . default . bool ,
216
+ infinite : _propTypes2 . default . bool ,
217
+ multipleSelect : _propTypes2 . default . bool
218
+ } ;
219
+ ReactSelectionTree . defaultProps = {
220
+ name : '' ,
221
+ search : true
222
+ } ;
223
+ exports . default = ReactSelectionTree ;
0 commit comments