Skip to content

Commit

Permalink
Add DatePicker component
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan687 committed Jan 4, 2017
1 parent 344928a commit d43e8ee
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {MultiRange} from './sensors/MultiRange';
import {SingleDropdownRange} from './sensors/SingleDropdownRange';
import {MultiDropdownRange} from './sensors/MultiDropdownRange';
import {ToggleButton} from './sensors/ToggleButton';
import {DatePicker} from './sensors/DatePicker';
import {ResultList} from './actuators/ResultList';
import {PaginatedResultList} from './actuators/PaginatedResultList';
// middleware
Expand All @@ -31,6 +32,7 @@ module.exports = {
SingleDropdownRange: SingleDropdownRange,
MultiDropdownRange: MultiDropdownRange,
ToggleButton: ToggleButton,
DatePicker: DatePicker,
ReactiveBase: ReactiveBase,
ResultList: ResultList,
PaginatedResultList: PaginatedResultList,
Expand Down
139 changes: 139 additions & 0 deletions app/sensors/DatePicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {default as React, Component} from 'react';
import { SingleDatePicker } from 'react-dates';
import classNames from 'classnames';
var moment = require('moment');
var momentPropTypes = require('react-moment-proptypes');
import { manager } from '../middleware/ChannelManager.js';
var helper = require('../middleware/helper.js');

export class DatePicker extends Component {
constructor(props, context) {
super(props);
this.state = {
currentValue: this.props.date,
focused: this.props.focused
};
this.type = 'range';
this.handleChange = this.handleChange.bind(this);
this.defaultQuery = this.defaultQuery.bind(this);
}

// Set query information
componentDidMount() {
this.setQueryInfo();
}

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

// build query for this sensor only
defaultQuery(value) {
let query = null;
if(value) {
query = {
'range': {
[this.props.appbaseField]: {
gte: value,
lt: moment(value).add(1, 'days')
}
}
};
}
return query;
}

// use this only if want to create actuators
// Create a channel which passes the depends and receive results whenever depends changes
createChannel() {
let depends = this.props.depends ? this.props.depends : {};
var channelObj = manager.create(this.context.appbaseRef, this.context.type, depends);
}

// handle the input change and pass the value inside sensor info
handleChange(inputVal) {
this.setState({
'currentValue': inputVal
});
var obj = {
key: this.props.sensorId,
value: inputVal
};
// pass the selected sensor value with sensorId as key,
let isExecuteQuery = true;
helper.selectedSensor.set(obj, isExecuteQuery);
}

// handle focus
handleFocus(focus) {
this.setState({
focused: focus
});
}

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

let cx = classNames({
'rbc-title-active': this.props.title,
'rbc-title-inactive': !this.props.title
});

return (
<div className={`rbc rbc-DatePicker col s12 col-xs-12 card thumbnail ${cx}`}>
{title}
<div className="rb-datepicker-component col s12 col-xs-12">
<SingleDatePicker
id={this.props.sensorId}
date={this.state.currentValue}
placeholder={this.props.placeholder}
focused={this.state.focused}
numberOfMonths={this.props.numberOfMonths}
isOutsideRange={() => false}
{...this.props.extra}
onDateChange={(date) => { this.handleChange(date) }}
onFocusChange={({ focused }) => { this.handleFocus(focused) }}
/>
</div>
</div>
);
}
}

DatePicker.propTypes = {
sensorId: React.PropTypes.string.isRequired,
appbaseField: React.PropTypes.string,
title: React.PropTypes.string,
placeholder: React.PropTypes.string,
date: momentPropTypes.momentObj,
focused: React.PropTypes.bool,
numberOfMonths: React.PropTypes.number,
extra: React.PropTypes.any
};

// Default props value
DatePicker.defaultProps = {
placeholder: 'Select Date',
numberOfMonths: 1,
focused: true,
date: null
};

// context type
DatePicker.contextTypes = {
appbaseRef: React.PropTypes.any.isRequired,
type: React.PropTypes.any.isRequired
};
103 changes: 103 additions & 0 deletions app/stories/DatePicker.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import React, { Component } from 'react';
var moment = require('moment');
import { ReactiveBase, DatePicker, ResultList } from '../app.js';
import { Img } from './Img.js';

require('./list.css');

export default class SingleListDefault extends Component {
constructor(props) {
super(props);
this.onData = this.onData.bind(this);
this.DEFAULT_IMAGE = 'http://www.avidog.com/wp-content/uploads/2015/01/BellaHead082712_11-50x65.jpg';
}

onData(res) {
let result, combineData = res.currentData;
if(res.mode === 'historic') {
combineData = res.currentData.concat(res.newData);
}
if (combineData) {
result = combineData.map((markerData, index) => {
let marker = markerData._source;
return this.itemMarkup(marker, markerData);
});
}
return result;
}

itemMarkup(marker, markerData) {
return (
<a className="full_row single-record single_record_for_clone"
href={marker.event ? marker.event.event_url : ''}
target="_blank"
key={markerData._id}>
<div className="img-container">
<Img key={markerData._id} src={marker.member ? marker.member.photo : this.DEFAULT_IMAGE} />
</div>
<div className="text-container full_row">
<div className="text-head text-overflow full_row">
<span className="text-head-info text-overflow">
{marker.member ? marker.member.member_name : ''} is going to {marker.event ? marker.event.event_name : ''}
</span>
<span className="text-head-city">
{marker.group ? marker.group.group_city : ''} ({moment(marker.mtime).format('MM-DD')})
</span>
</div>
<div className="text-description text-overflow full_row">
<ul className="highlight_tags">
{
marker.group.group_topics.map(function(tag,i){
return (<li key={i}>{tag.topic_name}</li>)
})
}
</ul>
</div>
</div>
</a>
);
}

render() {
return (
<ReactiveBase
appname="meetup_demo1"
username="yafYCRWns"
password="c9c9b34e-185c-42e5-bdfe-b7c32d543f2e"
type="meetupdata1"
>
<div className="row">
<div className="col s6 col-xs-6">
<DatePicker
sensorId="DateSensor"
appbaseField={this.props.mapping.date}
{...this.props}
/>
</div>

<div className="col s6 col-xs-6">
<ResultList
sensorId="SearchResult"
appbaseField={this.props.mapping.topic}
title="Meetups"
from={0}
size={20}
onData={this.onData}
requestOnScroll={true}
depends={{
DateSensor: {"operation": "must"}
}}
/>
</div>
</div>
</ReactiveBase>
);
}
}

SingleListDefault.defaultProps = {
title: 'DatePicker',
mapping: {
date: 'mtime'
}
};
31 changes: 31 additions & 0 deletions app/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
var moment = require('moment');
import { storiesOf, addDecorator } from '@kadira/storybook';
import { withKnobs, text, boolean, number } from '@kadira/storybook-addon-knobs';
import withReadme from 'storybook-readme/with-readme';
Expand Down Expand Up @@ -34,6 +35,7 @@ import MultiDropdownListDefault from './MultiDropdownList.stories';
import SingleDropdownRangeDefault from './SingleDropdownRange.stories';
import MultiDropdownRangeDefault from './MultiDropdownRange.stories';
import PaginatedResultListDefault from './PaginatedResultListDefault.stories';
import DatePickerDefault from './DatePicker.stories';

require ('../../node_modules/materialize-css/dist/css/materialize.min.css');
require ('../../dist/css/vendor.min.css');
Expand Down Expand Up @@ -225,3 +227,32 @@ storiesOf('PaginatedResultList', module)
.add('Basic', () => (
<PaginatedResultListDefault />
));

storiesOf('DatePicker', module)
.addDecorator(withKnobs)
.add('Basic', withReadme(removeFirstLine(TextFieldReadme), () => (
<DatePickerDefault />
)))
.add('Show more than 1 month', withReadme(removeFirstLine(TextFieldReadme), () => (
<DatePickerDefault
numberOfMonths={2}
/>
)))
.add('Default date', withReadme(removeFirstLine(TextFieldReadme), () => (
<DatePickerDefault
date={moment()}
/>
)))
.add('Initial Focus', withReadme(removeFirstLine(TextFieldReadme), () => (
<DatePickerDefault
focused={false}
/>
)))
.add('React-dates props', withReadme(removeFirstLine(TextFieldReadme), () => (
<DatePickerDefault
extra = {{
'withFullScreenPortal': true,
'showClearDate': true
}}
/>
)))
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
"babel-preset-stage-0": "^6.5.0",
"classnames": "^2.2.5",
"fbemitter": "^2.0.2",
"jquery": "^3.1.1",
"lodash": "^4.15.0",
"moment": "^2.14.1",
"rc-slider": "^5.4.0",
"react": "^15.1.0",
"react-dates": "^4.3.0",
"react-dom": "^15.1.0",
"react-select": "^1.0.0-beta13",
"react-tooltip": "^3.1.8"
Expand Down Expand Up @@ -62,6 +65,7 @@
"lodash-webpack-plugin": "^0.10.6",
"materialize-css": "^0.97.8",
"raw-loader": "^0.5.1",
"react-addons-shallow-compare": "^15.4.1",
"react-scripts": "^0.7.0",
"shebang-loader": "0.0.1",
"storybook-readme": "^1.2.0",
Expand Down

0 comments on commit d43e8ee

Please sign in to comment.