diff --git a/example/customCheckList.js b/example/customCheckList.js index 9822f2f..fc5f9f0 100644 --- a/example/customCheckList.js +++ b/example/customCheckList.js @@ -14,7 +14,9 @@ const data = Emojis; const renderItem = ({ item }) => { return ( - + { class Selector extends Component { constructor(props) { super(props); + this.preSelectedItems = data.slice(0, 4); this.state = { loader: true, + selectedItems: [], }; } componentDidMount() { - setTimeout(() => this.setState({ loader: false }), 500); + setTimeout(() => { + this.setState({ loader: false }); + this.setState({ selectedItems: this.preSelectedItems }); + }, 500); } render() { @@ -46,48 +53,60 @@ class Selector extends Component { const border = 'grey'; return ( - { - // eslint-disable-next-line no-console - console.log('My updated list :: ', ids); - }} - onLoading={() => ( - - - - Loading.... - - - )} - selectedListItems={data.slice(0, 4)} - checkboxProp={Platform.select({ - // Optional - ios: { - // iOS (supported from v0.3.0) - boxType: 'square', - tintColor: border, - onTintColor: theme, - onCheckColor: '#fff', - onFillColor: theme, - }, - android: { - tintColors: { - true: theme, - false: border, + + { + // eslint-disable-next-line no-console + console.log('My updated list :: ', ids, items); + this.setState({ selectedItems: items }); + }} + onLoading={() => ( + + + + Loading.... + + + )} + selectedListItems={data.slice(0, 4)} + checkboxProp={Platform.select({ + // Optional + ios: { + // iOS (supported from v0.3.0) + boxType: 'square', + tintColor: border, + onTintColor: theme, + onCheckColor: '#fff', + onFillColor: theme, }, - }, - })} - renderItem={renderItem} - // listItemStyle={{ borderBottomColor: "#eee", borderBottomWidth: 1 }} - /> + android: { + tintColors: { + true: theme, + false: border, + }, + }, + })} + renderItem={renderItem} + /> + + + Total selected : {this.state.selectedItems.length} + ); } diff --git a/src/checkList.js b/src/checkList.js index 4feb665..f5bd059 100644 --- a/src/checkList.js +++ b/src/checkList.js @@ -14,29 +14,34 @@ class CheckboxList extends Component { this.selectAllItems = this.selectAllItems.bind(this); } - shouldComponentUpdate(nextProps, nextState) { + handleOnChange() { this.props.onChange({ - ids: nextState.selectedIndexes, - items: nextState.selectedListItems, + ids: this.state.selectedIndexes, + items: this.state.selectedListItems, }); - return true; } unSelectAllItem() { - this.setState({ - selectedIndexes: [], - selectedListItems: [], - }); + this.setState( + { + selectedIndexes: [], + selectedListItems: [], + }, + this.handleOnChange, + ); } selectAllItems() { const { selectedIndexes } = this.state; const { listItems } = this.props; if (selectedIndexes.length < listItems.length) { - this.setState({ - selectedIndexes: listItems.map(item => item.id), - selectedListItems: [...listItems], - }); + this.setState( + { + selectedIndexes: listItems.map(item => item.id), + selectedListItems: [...listItems], + }, + this.handleOnChange, + ); } else { this.unSelectAllItem(); } @@ -55,10 +60,13 @@ class CheckboxList extends Component { currentSelectedIds.push(data.id); currentSelectedItems.push(data); } - this.setState({ - selectedIndexes: currentSelectedIds, - selectedListItems: currentSelectedItems, - }); + this.setState( + { + selectedIndexes: currentSelectedIds, + selectedListItems: currentSelectedItems, + }, + this.handleOnChange, + ); } render() {