Skip to content

Commit

Permalink
fix ToggleList behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
metagrover committed Feb 24, 2017
1 parent 36d936b commit b53a132
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions app/sensors/ToggleList.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,70 @@ import {
AppbaseSensorHelper as helper
} from "@appbaseio/reactivebase";

const _ = require("lodash");

export default class ToggleList extends Component {
constructor(props) {
super(props);
this.state = {
selected: []
};
this.type = "term";
this.defaultSelected = this.props.defaultSelected;
this.defaultSelected = null;
this.handleChange = this.handleChange.bind(this);
this.customQuery = this.customQuery.bind(this);
}

// Set query information
componentDidMount() {
this.setQueryInfo();
if (this.defaultSelected) {
const records = this.props.data.filter(record => this.defaultSelected.indexOf(record.label) > -1);
if (records && records.length) {
records.forEach((singleRecord) => {
setTimeout(this.handleChange.bind(this, singleRecord), 1000);
});
}
}
this.initialize(this.props);
}

componentWillReceiveProps(nextProps) {
this.initialize(nextProps);
}

componentWillUpdate() {
if (this.defaultSelected !== this.props.defaultSelected) {
this.defaultSelected = this.props.defaultSelected;
const records = this.props.data.filter(record => this.defaultSelected.indexOf(record.label) > -1);
if (records && records.length) {
records.forEach((singleRecord) => {
setTimeout(this.handleChange.bind(this, singleRecord), 1000);
});
initialize(props) {
if (props.defaultSelected) {
if (!props.multiSelect) {
if (typeof props.defaultSelected === "string") {
if (this.defaultSelected !== props.defaultSelected) {
this.defaultSelected = props.defaultSelected;
const records = props.data.filter(record => this.defaultSelected.indexOf(record.label) > -1);

this.setState({
selected: records
});
const obj = {
key: props.componentId,
value: records
};
helper.selectedSensor.set(obj, true);
}
} else {
console.error(`${props.componentId} - defaultSelected prop should be of type "string"`);
}
} else {
if (typeof props.defaultSelected === "object") {
if (!_.isEqual(this.defaultSelected, props.defaultSelected)) {
this.defaultSelected = props.defaultSelected;
let records = [];
this.defaultSelected.forEach((item) => {
records = records.concat(props.data.filter(record => item.indexOf(record.label) > -1));
});
this.setState({
selected: records
});
const obj = {
key: props.componentId,
value: records
};
helper.selectedSensor.set(obj, true);
}
} else {
console.error(`${props.componentId} - defaultSelected prop should be an "array"`);
}
}
}
}
Expand Down

0 comments on commit b53a132

Please sign in to comment.