11import React from 'react' ;
2+ import PropTypes from 'prop-types' ;
23import { isEqual } from 'lodash' ;
34import { DualList } from './index' ;
45import { adjustProps } from './helpers' ;
6+ import { noop } from '../../common/helpers' ;
57
68class DualListControlled extends React . Component {
79 constructor ( props ) {
@@ -12,6 +14,11 @@ class DualListControlled extends React.Component {
1214 } ;
1315 }
1416
17+ componentDidMount ( ) {
18+ const { onComponentInit } = this . props ;
19+ onComponentInit ( this . state ) ;
20+ }
21+
1522 static getDerivedStateFromProps ( nextProps , prevState ) {
1623 return ! isEqual ( nextProps , prevState . prevProps )
1724 ? {
@@ -22,50 +29,55 @@ class DualListControlled extends React.Component {
2229 }
2330
2431 onItemChange = ( { side, items, selectCount, isMainChecked } ) => {
32+ const { onItemChange } = this . props ;
2533 this . setState ( {
2634 [ side ] : {
2735 ...this . state [ side ] ,
2836 items,
2937 selectCount,
3038 isMainChecked
3139 }
32- } ) ;
40+ } , ( ) => onItemChange ( this . state ) ) ;
3341 } ;
3442
3543 onMainCheckboxChange = ( { side, checked, items, selectCount } ) => {
44+ const { onMainCheckboxChange } = this . props ;
3645 this . setState ( {
3746 [ side ] : {
3847 ...this . state [ side ] ,
3948 items,
4049 selectCount,
4150 isMainChecked : checked
4251 }
43- } ) ;
52+ } , ( ) => onMainCheckboxChange ( this . state ) ) ;
4453 } ;
4554
4655 onSortClick = ( { side, items, isSortAsc } ) => {
56+ const { onSortClick } = this . props ;
4757 this . setState ( {
4858 [ side ] : {
4959 ...this . state [ side ] ,
5060 items,
5161 isSortAsc
5262 }
53- } ) ;
63+ } , ( ) => onSortClick ( this . state ) ) ;
5464 } ;
5565
5666 onFilterChange = ( { side, filterTerm, items, isMainChecked } ) => {
67+ const { onFilterChange } = this . props ;
5768 this . setState ( {
5869 [ side ] : {
5970 ...this . state [ side ] ,
6071 filterTerm,
6172 items,
6273 isMainChecked
6374 }
64- } ) ;
75+ } , ( ) => onFilterChange ( this . state ) ) ;
6576 } ;
6677
6778 onChange = ( { left, right } ) => {
68- this . setState ( { left, right } ) ;
79+ const { onChange } = this . props ;
80+ this . setState ( { left, right } , ( ) => onChange ( this . state ) ) ;
6981 } ;
7082
7183 render ( ) {
@@ -85,4 +97,46 @@ class DualListControlled extends React.Component {
8597 }
8698}
8799
100+ DualListControlled . propTypes = {
101+ /**
102+ * Function that runs after items have been moved between the lists.
103+ * Receives the updated state as a callback.
104+ */
105+ onChange : PropTypes . func ,
106+ /**
107+ * Function that runs after an item was clicked.
108+ * Receives the updated state as a callback.
109+ */
110+ onItemChange : PropTypes . func ,
111+ /**
112+ * Function that runs after the main checkbox was clicked.
113+ * Receives the updated state as a callback.
114+ */
115+ onMainCheckboxChange : PropTypes . func ,
116+ /**
117+ * Function that runs after the sort icon was clicked.
118+ * Receives the updated state as a callback.
119+ */
120+ onSortClick : PropTypes . func ,
121+ /**
122+ * Function that runs after the filter input has changed.
123+ * Receives the updated state as a callback.
124+ */
125+ onFilterChange : PropTypes . func ,
126+ /**
127+ * Function that runs after the component had mounted.
128+ * Receives the updated state as a callback.
129+ */
130+ onComponentInit : PropTypes . func ,
131+ } ;
132+
133+ DualListControlled . defaultProps = {
134+ onChange : noop ,
135+ onItemChange : noop ,
136+ onMainCheckboxChange : noop ,
137+ onSortClick : noop ,
138+ onFilterChange : noop ,
139+ onComponentInit : noop
140+ } ;
141+
88142export default DualListControlled ;
0 commit comments