-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ToggleList sensor and story; misc fixes
- Loading branch information
1 parent
65ea38c
commit 36d936b
Showing
10 changed files
with
486 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.