Skip to content

Commit

Permalink
es6
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzuo committed Jun 5, 2017
1 parent 3a9e164 commit dd09ce7
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 393 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015", "react"]
"presets": ["es2015", "stage-0", "react"]
}
62 changes: 27 additions & 35 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,43 @@
require('../src/less/input-moment.less');
require('./app.less');
import '../src/less/input-moment.less';
import './app.less';

var moment = require('moment');
var React = require('react');
var ReactDOM = require('react-dom');
var InputMoment = require('../src/input-moment');
var packageJson = require('../package.json');
import moment from 'moment';
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import InputMoment from '../src/input-moment';
import packageJson from '../package.json';

var App = React.createClass({
displayName: 'App',
class App extends Component {
state = {
m: moment()
};

getInitialState() {
return {
m: moment()
};
},
handleChange = m => {
this.setState({ m });
};

handleSave = () => {
console.log('saved', this.state.m.format('llll'));
};

render() {
return (
<div className="app">
<h1>{packageJson.name}</h1>
<h2>{packageJson.description}</h2>
<form>
<div className="input">
<input
type="text"
value={this.state.m.format('llll')}
readOnly
<div className="input">
<input type="text" value={this.state.m.format('llll')} readOnly />
</div>
<InputMoment
moment={this.state.m}
onChange={this.handleChange}
onSave={this.handleSave}
/>
</div>
<InputMoment
moment={this.state.m}
onChange={this.handleChange}
onSave={this.handleSave}
/>
</form>
</div>
);
},

handleChange(m) {
this.setState({ m });
},

handleSave() {
console.log('saved', this.state.m.format('llll'));
}
});
}

ReactDOM.render(<App/>, document.getElementById('app'));
ReactDOM.render(<App />, document.getElementById('app'));
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"babel-core": "^6.1.2",
"babel-jest": "^19.0.0",
"babel-loader": "^6.0.1",
"babel-preset-es2015": "^6.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.1.2",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.21.0",
"github-pages-deploy": "0.0.3",
"jest": "^19.0.2",
Expand All @@ -44,7 +45,6 @@
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"blacklist": "^1.1.4",
"classnames": "^2.2.0",
"lodash": "^4.17.4",
"react-input-slider": "^1.5.0"
Expand Down
118 changes: 55 additions & 63 deletions src/calendar.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,66 @@
var cx = require('classnames');
var blacklist = require('blacklist');
var moment = require('moment');
var React = require('react');
var range = require('lodash/range');
var chunk = require('lodash/chunk');
import moment from 'moment';
import React, { Component } from 'react';
import cx from 'classnames';
import range from 'lodash/range';
import chunk from 'lodash/chunk';

var Day = React.createClass({
displayName: 'Day',
const Day = ({ i, w, d, className, ...props }) => {
const prevMonth = w === 0 && i > 7;
const nextMonth = w >= 4 && i <= 14;
const cls = cx({
'prev-month': prevMonth,
'next-month': nextMonth,
'current-day': !prevMonth && !nextMonth && i === d
});

render() {
var i = this.props.i;
var w = this.props.w;
var prevMonth = (w === 0 && i > 7);
var nextMonth = (w >= 4 && i <= 14);
var props = blacklist(this.props, 'i', 'w', 'd', 'className');
props.className = cx({
'prev-month': prevMonth,
'next-month': nextMonth,
'current-day': !prevMonth && !nextMonth && (i === this.props.d)
});
return <td className={cls} {...props}>{i}</td>;
};

return <td {... props}>{i}</td>;
}
});
export default class Calendar extends Component {
selectDate = (i, w) => {
const prevMonth = w === 0 && i > 7;
const nextMonth = w >= 4 && i <= 14;
const m = this.props.moment;

module.exports = React.createClass({
displayName: 'Calendar',
m.date(i);
if (prevMonth) m.subtract(1, 'month');
if (nextMonth) m.add(1, 'month');

render() {
var m = this.props.moment;
var d = m.date();
var d1 = m.clone().subtract(1, 'month').endOf('month').date();
var d2 = m.clone().date(1).day();
var d3 = m.clone().endOf('month').date();
this.props.onChange(m);
};

var days = [].concat(
range(d1-d2+1, d1+1),
range(1, d3+1),
range(1, 42-d3-d2+1)
);
prevMonth = e => {
e.preventDefault();
this.props.onChange(this.props.moment.subtract(1, 'month'));
};

nextMonth = e => {
e.preventDefault();
this.props.onChange(this.props.moment.add(1, 'month'));
};

var weeks = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
render() {
const m = this.props.moment;
const d = m.date();
const d1 = m.clone().subtract(1, 'month').endOf('month').date();
const d2 = m.clone().date(1).day();
const d3 = m.clone().endOf('month').date();
const days = [].concat(
range(d1 - d2 + 1, d1 + 1),
range(1, d3 + 1),
range(1, 42 - d3 - d2 + 1)
);
const weeks = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

return (
<div className={cx('m-calendar', this.props.className)}>
<div className="toolbar">
<button type="button" className="prev-month" onClick={this.prevMonth}>
<i className={this.props.prevMonthIcon}/>
<i className={this.props.prevMonthIcon} />
</button>
<span className="current-date">{m.format('MMMM YYYY')}</span>
<button type="button" className="next-month" onClick={this.nextMonth}>
<i className={this.props.nextMonthIcon}/>
<i className={this.props.nextMonthIcon} />
</button>
</div>

Expand All @@ -64,8 +74,12 @@ module.exports = React.createClass({
<tbody>
{chunk(days, 7).map((row, w) => (
<tr key={w}>
{row.map((i) => (
<Day key={i} i={i} d={d} w={w}
{row.map(i => (
<Day
key={i}
i={i}
d={d}
w={w}
onClick={this.selectDate.bind(null, i, w)}
/>
))}
Expand All @@ -75,27 +89,5 @@ module.exports = React.createClass({
</table>
</div>
);
},

selectDate(i, w) {
var prevMonth = (w === 0 && i > 7);
var nextMonth = (w >= 4 && i <= 14);
var m = this.props.moment;

m.date(i);
if(prevMonth) m.subtract(1, 'month');
if(nextMonth) m.add(1, 'month');

this.props.onChange(m);
},

prevMonth(e) {
e.preventDefault();
this.props.onChange(this.props.moment.subtract(1, 'month'));
},

nextMonth(e) {
e.preventDefault();
this.props.onChange(this.props.moment.add(1, 'month'));
}
});
}
92 changes: 51 additions & 41 deletions src/input-moment.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,83 @@
var cx = require('classnames');
var blacklist = require('blacklist');
var moment = require('moment');
var React = require('react');
var Calendar = require('./calendar');
var Time = require('./time');
import cx from 'classnames';
import moment from 'moment';
import React, { Component } from 'react';
import Calendar from './calendar';
import Time from './time';

module.exports = React.createClass({
displayName: 'InputMoment',
export default class InputMoment extends Component {
static defaultProps = {
prevMonthIcon: 'ion-ios-arrow-left',
nextMonthIcon: 'ion-ios-arrow-right'
};

getInitialState() {
return {
tab: 0
};
},
state = {
tab: 0
};

getDefaultProps() {
return {
prevMonthIcon: 'ion-ios-arrow-left',
nextMonthIcon: 'ion-ios-arrow-right'
};
},
handleClickTab = (tab, e) => {
e.preventDefault();
this.setState({ tab: tab });
};

handleSave = e => {
e.preventDefault();
if (this.props.onSave) this.props.onSave();
};

render() {
var tab = this.state.tab;
var m = this.props.moment;
var props = blacklist(this.props, 'className', 'moment', 'prevMonthIcon', 'nextMonthIcon', 'onSave');
props.className = cx('m-input-moment', this.props.className);
const { tab } = this.state;
const {
moment: m,
className,
prevMonthIcon,
nextMonthIcon,
onSave,
...props
} = this.props;
const cls = cx('m-input-moment', className);

return (
<div {...props}>
<div className={cls} {...props}>
<div className="options">
<button type="button" className={cx('ion-calendar im-btn', {'is-active': tab === 0})} onClick={this.handleClickTab.bind(null, 0)}>
<button
type="button"
className={cx('ion-calendar im-btn', { 'is-active': tab === 0 })}
onClick={() => this.handleClickTab(0)}
>
Date
</button>
<button type="button" className={cx('ion-clock im-btn', {'is-active': tab === 1})} onClick={this.handleClickTab.bind(null, 1)}>
<button
type="button"
className={cx('ion-clock im-btn', { 'is-active': tab === 1 })}
onClick={() => this.handleClickTab(1)}
>
Time
</button>
</div>

<div className="tabs">
<Calendar
className={cx('tab', {'is-active': tab === 0})}
className={cx('tab', { 'is-active': tab === 0 })}
moment={m}
onChange={this.props.onChange}
prevMonthIcon={this.props.prevMonthIcon}
nextMonthIcon={this.props.nextMonthIcon}
/>
<Time
className={cx('tab', {'is-active': tab === 1})}
className={cx('tab', { 'is-active': tab === 1 })}
moment={m}
onChange={this.props.onChange}
/>
</div>

<button type="button" className="im-btn btn-save ion-checkmark"
onClick={this.handleSave}>
<button
type="button"
className="im-btn btn-save ion-checkmark"
onClick={this.handleSave}
>
Save
</button>
</div>
);
},

handleClickTab(tab, e) {
e.preventDefault();
this.setState({tab: tab});
},

handleSave(e) {
e.preventDefault();
if(this.props.onSave) this.props.onSave();
}
});
}
Loading

0 comments on commit dd09ce7

Please sign in to comment.