Skip to content

Commit

Permalink
almost finish
Browse files Browse the repository at this point in the history
  • Loading branch information
光弘 committed Feb 9, 2017
1 parent 66de86a commit e8c2905
Show file tree
Hide file tree
Showing 21 changed files with 546 additions and 200 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"jsx-a11y/no-static-element-interactions": "off",
"react/no-unused-prop-types": "off",
"react/forbid-prop-types": "off",
"react/prefer-es6-class": "off"
"react/prefer-es6-class": "off",
"jsx-a11y/anchor-has-content": "off"
}
}
27 changes: 22 additions & 5 deletions demo/CalendarDemo.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Button = require('uxcore-button');
const CalendarLocale = require('rc-calendar/lib/locale/zh_CN');
const React = require('react');
const moment = require('moment');
// const TimePickerLocale = require('rc-time-picker/lib/locale/zh_CN');
// const TimePicker = require('rc-time-picker');

Expand All @@ -16,6 +17,14 @@ function disabledDate(current) {
return current.getTime() > Date.now();
}

const enabledMinutes = [0, 15, 30, 45];
const disabledMinutes = [];
for (let i = 0; i < 60; i++) {
if (enabledMinutes.indexOf(i) === -1) {
disabledMinutes.push(i);
}
}

class Demo extends React.Component {
constructor(props) {
super(props);
Expand All @@ -33,7 +42,7 @@ class Demo extends React.Component {

handleClick() {
this.setState({
value: '2016-02-05',
value: '2017-01-05',
});
}

Expand All @@ -45,6 +54,7 @@ class Demo extends React.Component {
showDateInput: false,
locale: CalendarLocale,
prefixCls: 'kuma-calendar',
value: moment(me.state.value).locale('zh-cn'),
};

return (
Expand All @@ -58,14 +68,21 @@ class Demo extends React.Component {
<p>基本</p>
<Calendar
showTime
locale="en-us"
timezone={8}
showSecond={false}
locale="zh-cn"
format="YYYY-MM-DD HH:mm:ss"
disabledTime={() => (
{
disabledMinutes: () => disabledMinutes,
}
)}
timePicker={<Calendar.Pmam />}
value={this.state.value}
onSelect={this.onSelect.bind(this)}
showDateInput
/>
</div>
<div
{/* <div
className="kuma-form-field"
style={{
width: 400,
Expand Down Expand Up @@ -176,7 +193,7 @@ class Demo extends React.Component {
>
<p>直接渲染面板</p>
<RcCalendar {...panelOptions} />
</div>
</div>*/}
<Button onClick={me.handleClick.bind(me)}>changeTime</Button>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"rc-calendar": "~7.4.0",
"rc-time-picker": "~2.2.1",
"rc-util": "3.x",
"uxcore-button": "^0.4.6",
"uxcore-formatter": "~0.1.2",
"uxcore-select2": "^0.4.3",
"uxcore-tooltip": "~0.4.0"
}
}
10 changes: 7 additions & 3 deletions src/Calendar.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
const Datepicker = require('rc-calendar/lib/Picker');
const TimePicker = require('rc-time-picker/lib/Panel');
const React = require('react');
const classnames = require('classnames');
const moment = require('moment');

const TimePicker = require('./timePicker/Normal');
const RcCalendar = require('./RcCalendar');
const util = require('./util');
const i18n = require('./locale');

const CalendarLocale = {};

CalendarLocale['zh-cn'] = require('rc-calendar/lib/locale/zh_CN');
CalendarLocale['en-us'] = require('rc-calendar/lib/locale/en_US');

CalendarLocale['zh-cn'] = { ...CalendarLocale['zh-cn'], ...i18n['zh-cn'] };
CalendarLocale['en-us'] = { ...CalendarLocale['en-us'], ...i18n['en-us'] };

const { getCalendarContainer, generalizeFormat } = util;

class Calendar extends React.Component {
Expand Down Expand Up @@ -88,15 +92,15 @@ class Calendar extends React.Component {
return current.date();
},
disabledDate: (current) => {
if (typeof p.disabledDate === 'function') {
if (typeof p.disabledDate === 'function' && current) {
const date = current.clone();
date.getTime = current.valueOf;
return p.disabledDate(date);
}
return false;
},
disabledTime: (current) => {
if (typeof p.disabledTime === 'function') {
if (typeof p.disabledTime === 'function' && current) {
const date = current.clone();
date.getTime = current.valueOf;
return p.disabledTime(date);
Expand Down
1 change: 1 addition & 0 deletions src/Calendar.less
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@calendarPrefixCls: kuma-calendar;
@timePickerClass: kuma-time-picker;
@calendarSelectPrefixCls: kuma-select2;
@calendar-header-hover-bg: hsv(round(hsvhue(@brand-primary)), round(hsvsaturation(@brand-primary)) - 17%, round(hsvvalue(@brand-primary)) + 1%); // B1-1(s-17,b+1)

@import "style/common/index";
Expand Down
23 changes: 23 additions & 0 deletions src/CalendarFooter.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react';
import Button from 'uxcore-button';

const CalendarFooter = (props) => {
return (
<div className={`${props.prefixCls}-footer`}>
<Button onClick={props.onOk}>{props.locale.ok}</Button>
</div>
);
};

CalendarFooter.propTypes = {
locale: React.PropTypes.object,
prefixCls: React.PropTypes.string,
onOk: React.PropTypes.func,
};

CalendarFooter.defaultProps = {
onOk: () => {},
};

export default CalendarFooter;

232 changes: 232 additions & 0 deletions src/CalendarHeader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import React, { PropTypes } from 'react';
import MonthPanel from 'rc-calendar/lib/month/MonthPanel';
import YearPanel from 'rc-calendar/lib/year/YearPanel';
import toFragment from 'rc-util/lib/Children/mapSelf';
import Select from 'uxcore-select2';
import classnames from 'classnames';

const { Option } = Select;

function goMonth(direction) {
const next = this.props.value.clone();
next.add(direction, 'months');
this.props.onValueChange(next);
}

function goYear(direction) {
const next = this.props.value.clone();
next.add(direction, 'years');
this.props.onValueChange(next);
}

const CalendarHeader = React.createClass({
propTypes: {
enablePrev: PropTypes.any,
enableNext: PropTypes.any,
prefixCls: PropTypes.string,
showTimePicker: PropTypes.bool,
locale: PropTypes.object,
value: PropTypes.object,
onValueChange: PropTypes.func,
yearSelectOffset: PropTypes.number,
yearSelectTotal: PropTypes.number,
},

getDefaultProps() {
return {
enableNext: 1,
enablePrev: 1,
yearSelectOffset: 30,
yearSelectTotal: 60,
};
},

getInitialState() {
this.nextMonth = goMonth.bind(this, 1);
this.previousMonth = goMonth.bind(this, -1);
this.nextYear = goYear.bind(this, 1);
this.previousYear = goYear.bind(this, -1);
return {};
},

onSelect(value) {
this.setState({
showMonthPanel: 0,
showYearPanel: 0,
});
this.props.onValueChange(value);
},

onYearChange(year) {
const newValue = this.props.value.clone();
newValue.year(parseInt(year, 10));
const onValueChange = this.props.onValueChange;
if (onValueChange) {
onValueChange(newValue);
}
},

onMonthChange(month) {
const newValue = this.props.value.clone();
newValue.month(parseInt(month, 10));
const onValueChange = this.props.onValueChange;
if (onValueChange) {
onValueChange(newValue);
}
},

getSelectContainer() {
return this.root;
},

monthYearElement(showTimePicker) {
const props = this.props;
const prefixCls = props.prefixCls;
const locale = props.locale;
const value = props.value;
const monthBeforeYear = locale.monthBeforeYear;
const selectClassName = `${prefixCls}-${monthBeforeYear ? 'my-select' : 'ym-select'}`;
const yearValue = value.year();
const startYear = yearValue - props.yearSelectOffset;
const endYear = startYear + props.yearSelectTotal;
const yearOptions = [];
const monthOptions = [];
const isLocaleCn = locale.year === '年';
const suffix = isLocaleCn ? '年' : '';

for (let i = startYear; i < endYear; i++) {
yearOptions.push(<Option key={`${i}`} label={`${i + suffix}`}>{i}</Option>);
}
const year = (
<Select
value={`${yearValue}`}
className={classnames(`${prefixCls}-year-select`, {
[`${prefixCls}-cn-select`]: isLocaleCn,
})}
showSearch={false}
optionLabelProp="label"
transitionName=""
getPopupContainer={() => this.getSelectContainer()}
onChange={this.onYearChange}
>
{yearOptions}
</Select>
);
for (let i = 0; i < 12; i++) {
const current = value.clone();
const localeData = value.localeData();
current.month(i);
monthOptions.push(
<Option
key={`${i}`}
label={localeData.monthsShort(current)}
>{localeData.monthsShort(current)}</Option>
);
}
const month = (
<Select
value={`${value.month()}`}
dropdownMatchSelectWidth={false}
className={classnames(`${prefixCls}-month-select`, {
[`${prefixCls}-cn-select`]: isLocaleCn,
})}
showSearch={false}
optionLabelProp="label"
transitionName=""
getPopupContainer={() => this.getSelectContainer()}
onChange={this.onMonthChange}
>
{monthOptions}
</Select>
);
let my = [];
if (monthBeforeYear) {
my = [month, year];
} else {
my = [year, month];
}
return (<span className={selectClassName}>
{toFragment(my)}
</span>);
},

showIf(condition, el) {
return condition ? el : null;
},

showMonthPanel() {
this.setState({
showMonthPanel: 1,
showYearPanel: 0,
});
},

showYearPanel() {
this.setState({
showMonthPanel: 0,
showYearPanel: 1,
});
},

render() {
const props = this.props;
const { enableNext, enablePrev, prefixCls, locale, value, showTimePicker } = props;
const state = this.state;
let PanelClass = null;
if (state.showMonthPanel) {
PanelClass = MonthPanel;
} else if (state.showYearPanel) {
PanelClass = YearPanel;
}
let panel;
if (PanelClass) {
panel = (<PanelClass
locale={locale}
defaultValue={value}
rootPrefixCls={prefixCls}
onSelect={this.onSelect}
/>);
}
return (
<div
className={`${prefixCls}-header`}
ref={(c) => {
this.root = c;
}}
>
<div style={{ position: 'relative' }}>
{this.showIf(enablePrev,
<a
className={`${prefixCls}-prev-year-btn`}
role="button"
onClick={this.previousYear}
title={locale.previousYear}
/>)}
{this.showIf(enablePrev,
<a
className={`${prefixCls}-prev-month-btn`}
role="button"
onClick={this.previousMonth}
title={locale.previousMonth}
/>)}
{this.monthYearElement(showTimePicker)}
{this.showIf(enableNext,
<a
className={`${prefixCls}-next-month-btn`}
onClick={this.nextMonth}
title={locale.nextMonth}
/>)}
{this.showIf(enableNext,
<a
className={`${prefixCls}-next-year-btn`}
onClick={this.nextYear}
title={locale.nextYear}
/>)}
</div>
{panel}
</div>
);
},
});

export default CalendarHeader;
Loading

0 comments on commit e8c2905

Please sign in to comment.