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

227 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';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
import { CloseCircleFilled } from '@ant-design/icons';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2018-03-09 15:04:21 +08:00
import interopDefault from '../_util/interopDefault';
import InputIcon from './InputIcon';
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;
2019-08-05 18:38:10 +08:00
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 WeekPicker must be ' +
2018-12-07 20:02:01 +08:00
'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,
};
}
2019-01-04 22:34:25 +08:00
componentDidUpdate(_: any, prevState: WeekPickerState) {
if (!('open' in this.props) && prevState.open && !this.state.open) {
2019-01-04 16:55:07 +08:00
this.focus();
}
}
2019-08-05 18:38:10 +08:00
saveInput = (node: any) => {
this.input = node;
};
2017-11-21 21:09:24 +08:00
weekDateRender = (current: any) => {
const selectedValue = this.state.value;
const { prefixCls } = this;
const { dateRender } = this.props;
const dateNode = dateRender ? dateRender(current) : current.date();
2018-12-07 20:02:01 +08:00
if (
selectedValue &&
2018-03-29 18:03:14 +08:00
current.year() === selectedValue.year() &&
2018-12-07 20:02:01 +08:00
current.week() === selectedValue.week()
) {
return (
<div className={`${prefixCls}-selected-day`}>
<div className={`${prefixCls}-date`}>{dateNode}</div>
</div>
);
}
return <div className={`${prefixCls}-date`}>{dateNode}</div>;
2018-12-07 20:02:01 +08:00
};
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));
2018-12-07 20:02:01 +08:00
};
handleOpenChange = (open: boolean) => {
const { onOpenChange } = this.props;
if (!('open' in this.props)) {
this.setState({ open });
}
if (onOpenChange) {
onOpenChange(open);
}
};
2017-11-21 21:09:24 +08:00
clearSelection = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
this.handleChange(null);
2018-12-07 20:02:01 +08:00
};
focus() {
this.input.focus();
}
blur() {
this.input.blur();
}
2019-08-05 18:38:10 +08:00
renderFooter = (...args: any[]) => {
const { prefixCls, renderExtraFooter } = this.props;
return renderExtraFooter ? (
<div className={`${prefixCls}-footer-extra`}>{renderExtraFooter(...args)}</div>
) : null;
2018-12-07 20:02:01 +08:00
};
renderWeekPicker = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
2018-12-07 20:02:01 +08:00
className,
disabled,
pickerClass,
popupStyle,
pickerInputClass,
format,
allowClear,
locale,
localeCode,
disabledDate,
style,
onFocus,
onBlur,
id,
suffixIcon,
defaultPickerValue,
} = 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, value: pickerValue } = this.state;
if (pickerValue && localeCode) {
pickerValue.locale(localeCode);
}
2018-12-07 20:02:01 +08:00
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}
renderFooter={this.renderFooter}
defaultValue={defaultPickerValue}
/>
);
2018-12-07 20:02:01 +08:00
const clearIcon =
!disabled && allowClear && this.state.value ? (
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
<CloseCircleFilled className={`${prefixCls}-picker-clear`} onClick={this.clearSelection} />
2018-12-07 20:02:01 +08:00
) : null;
const inputIcon = <InputIcon suffixIcon={suffixIcon} prefixCls={prefixCls} />;
2018-09-14 22:50:35 +08:00
const input = ({ value }: { value: moment.Moment | undefined }) => (
<span style={{ display: 'inline-block', width: '100%' }}>
<input
ref={this.saveInput}
disabled={disabled}
readOnly
value={(value && value.format(format)) || ''}
placeholder={placeholder}
className={pickerInputClass}
onFocus={onFocus}
onBlur={onBlur}
/>
{clearIcon}
{inputIcon}
</span>
);
return (
2018-12-07 20:02:01 +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>
);
2018-12-07 20:02:01 +08:00
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderWeekPicker}</ConfigConsumer>;
}
}
polyfill(WeekPicker);
export default WeekPicker;