2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
|
|
|
import * as moment from 'moment';
|
2017-08-31 20:44:22 +08:00
|
|
|
import Calendar from 'rc-calendar';
|
|
|
|
import RcDatePicker from 'rc-calendar/lib/Picker';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import Icon from '../icon';
|
2018-03-09 15:04:21 +08:00
|
|
|
import interopDefault from '../_util/interopDefault';
|
2017-08-31 20:44:22 +08:00
|
|
|
|
2017-11-21 21:09:24 +08:00
|
|
|
function formatValue(value: moment.Moment | null, format: string): string {
|
2017-08-31 20:44:22 +08:00
|
|
|
return (value && value.format(format)) || '';
|
|
|
|
}
|
|
|
|
|
2017-11-17 14:38:54 +08:00
|
|
|
export default class WeekPicker extends React.Component<any, any> {
|
2017-08-31 20:44:22 +08:00
|
|
|
static defaultProps = {
|
2018-03-01 17:24:06 +08:00
|
|
|
format: 'gggg-wo',
|
2017-08-31 20:44:22 +08:00
|
|
|
allowClear: true,
|
|
|
|
};
|
2017-11-30 09:54:31 +08:00
|
|
|
|
|
|
|
private input: any;
|
|
|
|
|
2017-11-21 21:09:24 +08:00
|
|
|
constructor(props: any) {
|
2017-08-31 20:44:22 +08:00
|
|
|
super(props);
|
|
|
|
const value = props.value || props.defaultValue;
|
2018-03-09 15:04:21 +08:00
|
|
|
if (value && !interopDefault(moment).isMoment(value)) {
|
2017-08-31 20:44:22 +08:00
|
|
|
throw new Error(
|
|
|
|
'The value/defaultValue of DatePicker or MonthPicker must be ' +
|
|
|
|
'a moment object after `antd@2.0`, see: https://u.ant.design/date-picker-value',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
this.state = {
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
}
|
2017-11-21 21:09:24 +08:00
|
|
|
componentWillReceiveProps(nextProps: any) {
|
2017-08-31 20:44:22 +08:00
|
|
|
if ('value' in nextProps) {
|
|
|
|
this.setState({ value: nextProps.value });
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 21:09:24 +08:00
|
|
|
weekDateRender = (current: any) => {
|
2017-08-31 20:44:22 +08:00
|
|
|
const selectedValue = this.state.value;
|
|
|
|
const { prefixCls } = this.props;
|
|
|
|
if (selectedValue &&
|
2018-03-29 18:03:14 +08:00
|
|
|
current.year() === selectedValue.year() &&
|
|
|
|
current.week() === selectedValue.week()) {
|
2017-08-31 20:44:22 +08:00
|
|
|
return (
|
|
|
|
<div className={`${prefixCls}-selected-day`}>
|
|
|
|
<div className={`${prefixCls}-date`}>
|
|
|
|
{current.date()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2018-04-17 13:50:27 +08:00
|
|
|
<div className={`${prefixCls}-date`}>
|
2017-08-31 20:44:22 +08:00
|
|
|
{current.date()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-11-21 21:09:24 +08:00
|
|
|
handleChange = (value: moment.Moment | null) => {
|
2017-08-31 20:44:22 +08:00
|
|
|
if (!('value' in this.props)) {
|
|
|
|
this.setState({ value });
|
|
|
|
}
|
|
|
|
this.props.onChange(value, formatValue(value, this.props.format));
|
|
|
|
}
|
2017-11-21 21:09:24 +08:00
|
|
|
clearSelection = (e: React.MouseEvent<HTMLElement>) => {
|
2017-08-31 20:44:22 +08:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
this.handleChange(null);
|
|
|
|
}
|
2017-11-30 09:54:31 +08:00
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.input.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.input.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
saveInput = (node: any) => {
|
|
|
|
this.input = node;
|
|
|
|
}
|
|
|
|
|
2017-08-31 20:44:22 +08:00
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
prefixCls, className, disabled, pickerClass, popupStyle,
|
2017-10-20 14:54:38 +08:00
|
|
|
pickerInputClass, format, allowClear, locale, localeCode, disabledDate,
|
2018-03-29 18:03:14 +08:00
|
|
|
style, onFocus, onBlur,
|
2017-08-31 20:44:22 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const pickerValue = this.state.value;
|
|
|
|
if (pickerValue && localeCode) {
|
|
|
|
pickerValue.locale(localeCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
const placeholder = ('placeholder' in this.props)
|
|
|
|
? this.props.placeholder : locale.lang.placeholder;
|
|
|
|
|
|
|
|
const calendar = (
|
|
|
|
<Calendar
|
|
|
|
showWeekNumber
|
|
|
|
dateRender={this.weekDateRender}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
format={format}
|
|
|
|
locale={locale.lang}
|
|
|
|
showDateInput={false}
|
|
|
|
showToday={false}
|
|
|
|
disabledDate={disabledDate}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const clearIcon = (!disabled && allowClear && this.state.value) ? (
|
|
|
|
<Icon
|
|
|
|
type="cross-circle"
|
|
|
|
className={`${prefixCls}-picker-clear`}
|
|
|
|
onClick={this.clearSelection}
|
|
|
|
/>
|
|
|
|
) : null;
|
2017-11-21 21:09:24 +08:00
|
|
|
const input = ({ value }: { value: moment.Moment | undefined }) => {
|
2017-08-31 20:44:22 +08:00
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
<input
|
2017-11-30 09:54:31 +08:00
|
|
|
ref={this.saveInput}
|
2017-08-31 20:44:22 +08:00
|
|
|
disabled={disabled}
|
|
|
|
readOnly
|
|
|
|
value={(value && value.format(format)) || ''}
|
|
|
|
placeholder={placeholder}
|
|
|
|
className={pickerInputClass}
|
2018-03-29 18:03:14 +08:00
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
style={style}
|
2017-08-31 20:44:22 +08:00
|
|
|
/>
|
|
|
|
{clearIcon}
|
|
|
|
<span className={`${prefixCls}-picker-icon`} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
return (
|
2018-01-24 10:45:46 +08:00
|
|
|
<span className={classNames(className, pickerClass)} id={this.props.id}>
|
2017-08-31 20:44:22 +08:00
|
|
|
<RcDatePicker
|
|
|
|
{...this.props}
|
|
|
|
calendar={calendar}
|
|
|
|
prefixCls={`${prefixCls}-picker-container`}
|
|
|
|
value={pickerValue}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
style={popupStyle}
|
|
|
|
>
|
|
|
|
{input}
|
|
|
|
</RcDatePicker>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|