ant-design/components/date-picker/WeekPicker.tsx

229 lines
5.8 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as moment from 'moment';
import { polyfill } from 'react-lifecycles-compat';
import Calendar from 'rc-calendar';
import RcDatePicker from 'rc-calendar/lib/Picker';
import classNames from 'classnames';
import Icon from '../icon';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2018-03-09 15:04:21 +08:00
import interopDefault from '../_util/interopDefault';
2017-11-21 21:09:24 +08:00
function formatValue(value: moment.Moment | null, format: string): string {
return (value && value.format(format)) || '';
}
2018-12-07 11:23:30 +08:00
interface WeekPickerState {
open: boolean;
value: moment.Moment | null;
}
class WeekPicker extends React.Component<any, WeekPickerState> {
static defaultProps = {
2018-03-01 17:24:06 +08:00
format: 'gggg-wo',
allowClear: true,
};
static getDerivedStateFromProps(nextProps: any) {
2018-12-07 11:23:30 +08:00
if ('value' in nextProps || 'open' in nextProps) {
const state = {} as WeekPickerState;
if ('value' in nextProps) {
state.value = nextProps.value;
}
if ('open' in nextProps) {
state.open = nextProps.open;
}
return state;
}
return null;
}
private input: any;
private prefixCls?: string;
2017-11-21 21:09:24 +08:00
constructor(props: any) {
super(props);
const value = props.value || props.defaultValue;
2018-03-09 15:04:21 +08:00
if (value && !interopDefault(moment).isMoment(value)) {
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,
2018-12-07 11:23:30 +08:00
open: props.open,
};
}
2017-11-21 21:09:24 +08:00
weekDateRender = (current: any) => {
const selectedValue = this.state.value;
const { prefixCls } = this;
if (selectedValue &&
2018-03-29 18:03:14 +08:00
current.year() === selectedValue.year() &&
current.week() === selectedValue.week()) {
return (
<div className={`${prefixCls}-selected-day`}>
<div className={`${prefixCls}-date`}>
{current.date()}
</div>
</div>
);
}
return (
<div className={`${prefixCls}-date`}>
{current.date()}
</div>
);
}
2017-11-21 21:09:24 +08:00
handleChange = (value: moment.Moment | null) => {
if (!('value' in this.props)) {
this.setState({ value });
}
this.props.onChange(value, formatValue(value, this.props.format));
}
handleOpenChange = (open: boolean) => {
const { onOpenChange } = this.props;
if (!('open' in this.props)) {
this.setState({ open });
}
if (onOpenChange) {
onOpenChange(open);
}
if (!open) {
this.focus();
}
};
2017-11-21 21:09:24 +08:00
clearSelection = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
this.handleChange(null);
}
focus() {
this.input.focus();
}
blur() {
this.input.blur();
}
saveInput = (node: any) => {
this.input = node;
}
renderWeekPicker = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
className, disabled, pickerClass, popupStyle,
pickerInputClass, format, allowClear, locale, localeCode, disabledDate,
2018-09-16 19:39:43 +08:00
style, onFocus, onBlur, id, suffixIcon,
} = this.props;
const prefixCls = getPrefixCls('calendar', customizePrefixCls);
// To support old version react.
// Have to add prefixCls on the instance.
// https://github.com/facebook/react/issues/12397
this.prefixCls = prefixCls;
const { open } = this.state;
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
2018-08-24 18:36:08 +08:00
type="close-circle"
className={`${prefixCls}-picker-clear`}
onClick={this.clearSelection}
2018-09-02 18:46:53 +08:00
theme="filled"
/>
) : null;
2018-09-14 22:50:35 +08:00
2018-09-16 19:39:43 +08:00
const inputIcon = suffixIcon && (
React.isValidElement<{ className?: string }>(suffixIcon)
2018-09-14 22:50:35 +08:00
? React.cloneElement(
2018-09-16 19:39:43 +08:00
suffixIcon,
2018-09-14 22:50:35 +08:00
{
className: classNames({
2018-09-16 19:39:43 +08:00
[suffixIcon.props.className!]: suffixIcon.props.className,
2018-09-14 22:50:35 +08:00
[`${prefixCls}-picker-icon`]: true,
}),
},
2018-09-16 19:39:43 +08:00
) : <span className={`${prefixCls}-picker-icon`}>{suffixIcon}</span>) || (
2018-09-14 22:50:35 +08:00
<Icon type="calendar" className={`${prefixCls}-picker-icon`} />
);
const input = ({ value }: { value: moment.Moment | undefined }) => {
return (
<span style={{ display: 'inline-block' }}>
<input
ref={this.saveInput}
disabled={disabled}
readOnly
value={(value && value.format(format)) || ''}
placeholder={placeholder}
className={pickerInputClass}
2018-03-29 18:03:14 +08:00
onFocus={onFocus}
onBlur={onBlur}
/>
{clearIcon}
2018-09-14 22:50:35 +08:00
{inputIcon}
</span>
);
};
return (
2018-08-22 00:41:29 +08:00
<span
className={classNames(className, pickerClass)}
style={style}
id={id}
>
<RcDatePicker
{...this.props}
calendar={calendar}
prefixCls={`${prefixCls}-picker-container`}
value={pickerValue}
onChange={this.handleChange}
open={open}
onOpenChange={this.handleOpenChange}
style={popupStyle}
>
{input}
</RcDatePicker>
</span>
);
}
render() {
return (
<ConfigConsumer>
{this.renderWeekPicker}
</ConfigConsumer>
);
}
}
polyfill(WeekPicker);
export default WeekPicker;