Skip to content

Commit

Permalink
Add ToggleList sensor and story; misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metagrover committed Feb 23, 2017
1 parent 65ea38c commit 36d936b
Show file tree
Hide file tree
Showing 10 changed files with 486 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import reactivebase from "@appbaseio/reactivebase";
import { NestedList } from "./sensors/NestedList";
import NestedList from "./sensors/NestedList";
import ToggleList from "./sensors/ToggleList";

const combineObj = {
NestedList
NestedList,
ToggleList
};

Object.keys(reactivebase).forEach((component) => {
Expand Down
47 changes: 47 additions & 0 deletions app/assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,50 @@
src: url("../fonts/Lato/Lato-Bold.ttf") format("truetype");
font-weight: 900;
font-style: normal; }

.rbc.rbc-nestedlist-container {
padding: 0;
margin: 0; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist {
overflow: auto;
max-height: 500px;
padding: 0; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-indent {
padding-left: 1em; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist.rbc-title-active.rbc-search-inactive .row.rbc-list-container {
height: calc(100% - 50px); }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist.rbc-title-active.rbc-search-active .row.rbc-list-container {
height: calc(100% - 98px); }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist.rbc-title-inactive.rbc-search-inactive .row.rbc-list-container {
height: 100%; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist.rbc-title-inactive.rbc-search-active .row.rbc-list-container {
height: calc(100% - 48px); }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container {
padding: 5px 0;
width: 100%;
overflow: auto; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container.row {
padding: 6px 10px;
margin: 0; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container .rbc-list-item {
background: transparent;
border: 0;
box-shadow: none;
padding: 0; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container .rbc-list-item .rbc-count {
margin: 0 10px 0 2px;
top: -1px; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container .rbc-list-item i.fa-chevron-right {
color: #bfbfbf;
font-size: 12px; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container .rbc-list-item.rbc-item-active {
font-family: "Lato Bold"; }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-list-container .rbc-list-item.rbc-item-active i.fa-chevron-right {
transform: rotate(90deg); }
.rbc.rbc-nestedlist-container .rbc.rbc-nestedlist .rbc-search-container {
padding: 0; }

.rbc.rbc-togglelist {
padding: 0; }
.rbc.rbc-togglelist .rbc-list-item {
margin: 10px 0; }
82 changes: 82 additions & 0 deletions app/assets/styles/partials/components/_nestedlist.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.rbc.rbc-nestedlist-container {
padding: 0;
margin: 0;

.rbc.rbc-nestedlist {
overflow: auto;
max-height: 500px;
padding: 0;

.rbc-indent {
padding-left: 1em;
}

&.rbc-title-active {
&.rbc-search-inactive {
.row.rbc-list-container {
height: calc(100% - 50px);
}
}

&.rbc-search-active {
.row.rbc-list-container {
height: calc(100% - 98px);
}
}
}

&.rbc-title-inactive {
&.rbc-search-inactive {
.row.rbc-list-container {
height: 100%;
}
}

&.rbc-search-active {
.row.rbc-list-container {
height: calc(100% - 48px);
}
}
}

.rbc-list-container {
padding: 5px 0;
width: 100%;
overflow: auto;

&.row {
padding: 6px 10px;
margin: 0;
}

.rbc-list-item {
background: transparent;
border: 0;
box-shadow: none;
padding: 0;

.rbc-count {
margin: 0 10px 0 2px;
top: -1px;
}

i.fa-chevron-right {
color: lighten($grey, 15%);
font-size: 12px;
}

&.rbc-item-active {
font-family: $font-bold;

i.fa-chevron-right {
transform: rotate(90deg);
}
}
}
}

.rbc-search-container {
padding: 0;
}
}
}
7 changes: 7 additions & 0 deletions app/assets/styles/partials/components/_togglelist.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.rbc.rbc-togglelist {
padding: 0;

.rbc-list-item {
margin: 10px 0;
}
}
3 changes: 2 additions & 1 deletion app/assets/styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

@import "partials/theme/default";

@import "partials/components/nestedlist";
@import "partials/components/nestedlist",
"partials/components/togglelist";
198 changes: 198 additions & 0 deletions app/sensors/ToggleList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import React, { Component } from "react";
import classNames from "classnames";
import {
TYPES,
AppbaseSensorHelper as helper
} from "@appbaseio/reactivebase";

export default class ToggleList extends Component {
constructor(props) {
super(props);
this.state = {
selected: []
};
this.type = "term";
this.defaultSelected = this.props.defaultSelected;
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);
});
}
}
}

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);
});
}
}
}

// set the query type and input data
setQueryInfo() {
const obj = {
key: this.props.componentId,
value: {
queryType: this.type,
inputData: this.props.appbaseField,
customQuery: this.props.customQuery ? this.props.customQuery : this.customQuery
}
};
helper.selectedSensor.setSensorInfo(obj);
}

// build query for this sensor only
customQuery(record) {
let query = null;

function generateRangeQuery(appbaseField) {
return record.map(singleRecord => ({
term: {
[appbaseField]: singleRecord.value
}
}));
}

if (record && record.length) {
query = {
bool: {
should: generateRangeQuery(this.props.appbaseField),
minimum_should_match: 1,
boost: 1.0
}
};
return query;
}
return query;
}

// handle the input change and pass the value inside sensor info
handleChange(record) {
const selected = this.state.selected;
let newSelection = [];
let selectedIndex = null;
selected.forEach((selectedRecord, index) => {
if (record.label === selectedRecord.label) {
selectedIndex = index;
selected.splice(index, 1);
}
});
if (selectedIndex === null) {
if (this.props.multiSelect) {
selected.push(record);
newSelection = selected;
} else {
newSelection.push(record);
}
} else {
newSelection = selected;
}
this.setState({
selected: newSelection
});
const obj = {
key: this.props.componentId,
value: newSelection
};
// pass the selected sensor value with componentId as key,
const isExecuteQuery = true;
helper.selectedSensor.set(obj, isExecuteQuery);
}

renderList() {
let list;
const selectedText = this.state.selected.map(record => record.label);
if (this.props.data) {
list = this.props.data.map(record => (
<div key={record.label} className="rbc-list-item">
<input
type="checkbox"
id={record.label}
className="rbc-checkbox-item"
checked={selectedText.indexOf(record.label) > -1}
onChange={() => this.handleChange(record)}
/>
<label htmlFor={record.label} className="rbc-label">{record.label}</label>
</div>
));
}
return list;
}

// render
render() {
let title = null;
if (this.props.title) {
title = (<h4 className="rbc-title col s12 col-xs-12">{this.props.title}</h4>);
}

const cx = classNames({
"rbc-title-active": this.props.title,
"rbc-title-inactive": !this.props.title,
"rbc-multiselect-active": this.props.multiSelect,
"rbc-multiselect-inactive": !this.props.multiSelect
});

return (
<div className={`rbc rbc-togglelist col s12 col-xs-12 card thumbnail ${cx}`}>
<div className="row">
{title}
<div className="col s12 col-xs-12">
{this.renderList()}
</div>
</div>
</div>
);
}
}

ToggleList.propTypes = {
componentId: React.PropTypes.string.isRequired,
appbaseField: React.PropTypes.string.isRequired,
title: React.PropTypes.string,
data: React.PropTypes.arrayOf(React.PropTypes.shape({
label: React.PropTypes.string.isRequired,
value: React.PropTypes.string.isRequired
})),
defaultSelected: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.arrayOf(React.PropTypes.string)
]),
multiSelect: React.PropTypes.bool,
customQuery: React.PropTypes.func
};

// Default props value
ToggleList.defaultProps = {
multiSelect: true
};

// context type
ToggleList.contextTypes = {
appbaseRef: React.PropTypes.any.isRequired,
type: React.PropTypes.any.isRequired
};

ToggleList.types = {
componentId: TYPES.STRING,
appbaseField: TYPES.STRING,
title: TYPES.STRING,
data: TYPES.OBJECT,
defaultSelected: TYPES.ARRAY,
multiSelect: TYPES.BOOLEAN,
customQuery: TYPES.FUNCTION
};
5 changes: 2 additions & 3 deletions app/stories/NestedList.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export default class NestedListDefault extends Component {
helper.ResponsiveStory();
}

onData(response) {
const res = response.res;
onData(res) {
let result = null;
if (res) {
let combineData = res.currentData;
if (res.mode === "historic") {
combineData = res.currentData.concat(res.newData);
} else if (res.mode === "streaming") {
} else if (res.mode === "streaming") {
combineData = helper.combineStreamData(res.currentData, res.newData);
}
if (combineData) {
Expand Down

0 comments on commit 36d936b

Please sign in to comment.