You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/date-picker/WeekPicker.jsx

241 lines
6.4 KiB

import * as moment from 'moment';
import Calendar from '../vc-calendar';
import VcDatePicker from '../vc-calendar/src/Picker';
import Icon from '../icon';
import { ConfigConsumerProps } from '../config-provider';
import {
hasProp,
getOptionProps,
initDefaultProps,
getComponentFromProp,
isValidElement,
getListeners,
} from '../_util/props-util';
import BaseMixin from '../_util/BaseMixin';
import { WeekPickerProps } from './interface';
import interopDefault from '../_util/interopDefault';
import { cloneElement } from '../_util/vnode';
function formatValue(value, format) {
return (value && value.format(format)) || '';
7 years ago
}
function noop() {}
7 years ago
export default {
// static defaultProps = {
// format: 'YYYY-wo',
// allowClear: true,
// };
// private input: any;
name: 'AWeekPicker',
7 years ago
mixins: [BaseMixin],
model: {
prop: 'value',
event: 'change',
},
props: initDefaultProps(WeekPickerProps(), {
format: 'gggg-wo',
allowClear: true,
}),
inject: {
configProvider: { default: () => ConfigConsumerProps },
},
data() {
const value = this.value || this.defaultValue;
7 years ago
if (value && !interopDefault(moment).isMoment(value)) {
7 years ago
throw new Error(
'The value/defaultValue of DatePicker or MonthPicker must be ' + 'a moment object',
);
7 years ago
}
return {
_value: value,
_open: this.open,
};
7 years ago
},
watch: {
value(val) {
const state = { _value: val };
this.setState(state);
this.prevState = { ...this.$data, ...state };
},
open(val) {
const state = { _open: val };
this.setState(state);
this.prevState = { ...this.$data, ...state };
7 years ago
},
5 years ago
_open(val, oldVal) {
this.$nextTick(() => {
if (!hasProp(this, 'open') && oldVal && !val) {
this.focus();
}
});
},
7 years ago
},
mounted() {
this.prevState = { ...this.$data };
},
updated() {
this.$nextTick(() => {
if (!hasProp(this, 'open') && this.prevState._open && !this._open) {
this.focus();
}
});
},
7 years ago
methods: {
weekDateRender(current) {
const selectedValue = this.$data._value;
const { _prefixCls: prefixCls, $scopedSlots } = this;
const dateRender = this.dateRender || $scopedSlots.dateRender;
const dateNode = dateRender ? dateRender(current) : current.date();
if (
selectedValue &&
7 years ago
current.year() === selectedValue.year() &&
current.week() === selectedValue.week()
) {
7 years ago
return (
<div class={`${prefixCls}-selected-day`}>
<div class={`${prefixCls}-date`}>{dateNode}</div>
7 years ago
</div>
);
7 years ago
}
return <div class={`${prefixCls}-date`}>{dateNode}</div>;
7 years ago
},
handleChange(value) {
7 years ago
if (!hasProp(this, 'value')) {
this.setState({ _value: value });
7 years ago
}
this.$emit('change', value, formatValue(value, this.format));
},
handleOpenChange(open) {
if (!hasProp(this, 'open')) {
this.setState({ _open: open });
}
this.$emit('openChange', open);
7 years ago
},
clearSelection(e) {
e.preventDefault();
e.stopPropagation();
this.handleChange(null);
7 years ago
},
renderFooter(...args) {
const { _prefixCls: prefixCls, $scopedSlots } = this;
const renderExtraFooter = this.renderExtraFooter || $scopedSlots.renderExtraFooter;
return renderExtraFooter ? (
<div class={`${prefixCls}-footer-extra`}>{renderExtraFooter(...args)}</div>
) : null;
},
focus() {
this.$refs.input.focus();
7 years ago
},
blur() {
this.$refs.input.blur();
7 years ago
},
},
render() {
const props = getOptionProps(this);
let suffixIcon = getComponentFromProp(this, 'suffixIcon');
suffixIcon = Array.isArray(suffixIcon) ? suffixIcon[0] : suffixIcon;
7 years ago
const {
prefixCls: customizePrefixCls,
disabled,
pickerClass,
popupStyle,
pickerInputClass,
format,
allowClear,
locale,
localeCode,
disabledDate,
$data,
$scopedSlots,
} = this;
const listeners = getListeners(this);
const getPrefixCls = this.configProvider.getPrefixCls;
const prefixCls = getPrefixCls('calendar', customizePrefixCls);
this._prefixCls = prefixCls;
const { _value: pickerValue, _open: open } = $data;
const { focus = noop, blur = noop } = listeners;
7 years ago
if (pickerValue && localeCode) {
pickerValue.locale(localeCode);
7 years ago
}
const placeholder = hasProp(this, 'placeholder') ? this.placeholder : locale.lang.placeholder;
const weekDateRender = this.dateRender || $scopedSlots.dateRender || this.weekDateRender;
7 years ago
const calendar = (
<Calendar
showWeekNumber
dateRender={weekDateRender}
7 years ago
prefixCls={prefixCls}
format={format}
locale={locale.lang}
showDateInput={false}
showToday={false}
disabledDate={disabledDate}
renderFooter={this.renderFooter}
7 years ago
/>
);
const clearIcon =
!disabled && allowClear && $data._value ? (
<Icon
type="close-circle"
class={`${prefixCls}-picker-clear`}
onClick={this.clearSelection}
theme="filled"
/>
) : null;
const inputIcon = (suffixIcon &&
(isValidElement(suffixIcon) ? (
cloneElement(suffixIcon, {
class: `${prefixCls}-picker-icon`,
})
) : (
<span class={`${prefixCls}-picker-icon`}>{suffixIcon}</span>
))) || <Icon type="calendar" class={`${prefixCls}-picker-icon`} />;
7 years ago
const input = ({ value }) => {
return (
<span style={{ display: 'inline-block', width: '100%' }}>
7 years ago
<input
ref="input"
7 years ago
disabled={disabled}
readOnly
value={(value && value.format(format)) || ''}
placeholder={placeholder}
class={pickerInputClass}
onFocus={focus}
onBlur={blur}
/>
{clearIcon}
{inputIcon}
7 years ago
</span>
);
};
7 years ago
const vcDatePickerProps = {
props: {
...props,
calendar,
prefixCls: `${prefixCls}-picker-container`,
value: pickerValue,
open,
7 years ago
},
on: {
...listeners,
7 years ago
change: this.handleChange,
openChange: this.handleOpenChange,
7 years ago
},
style: popupStyle,
};
7 years ago
return (
<span class={pickerClass}>
<VcDatePicker {...vcDatePickerProps}>{input}</VcDatePicker>
7 years ago
</span>
);
7 years ago
},
};