ant-design-vue/components/date-picker/WeekPicker.jsx
tangjinzhou 518cc852af Merge remote-tracking branch 'origin/master' into 2.0
# Conflicts:
#	antdv-demo
#	components/alert/__tests__/__snapshots__/demo.test.js.snap
#	components/auto-complete/__tests__/__snapshots__/demo.test.js.snap
#	components/avatar/__tests__/__snapshots__/demo.test.js.snap
#	components/badge/__tests__/__snapshots__/demo.test.js.snap
#	components/calendar/__tests__/__snapshots__/demo.test.js.snap
#	components/calendar/__tests__/__snapshots__/index.test.js.snap
#	components/card/__tests__/__snapshots__/demo.test.js.snap
#	components/cascader/__tests__/__snapshots__/demo.test.js.snap
#	components/cascader/__tests__/__snapshots__/index.test.js.snap
#	components/collapse/__tests__/__snapshots__/demo.test.js.snap
#	components/date-picker/__tests__/__snapshots__/DatePicker.test.js.snap
#	components/date-picker/__tests__/__snapshots__/demo.test.js.snap
#	components/date-picker/__tests__/__snapshots__/other.test.js.snap
#	components/dropdown/__tests__/__snapshots__/demo.test.js.snap
#	components/empty/__tests__/__snapshots__/demo.test.js.snap
#	components/form-model/__tests__/__snapshots__/demo.test.js.snap
#	components/form/__tests__/__snapshots__/demo.test.js.snap
#	components/icon/__tests__/__snapshots__/index.test.js.snap
#	components/icon/index.js
#	components/input-number/__tests__/__snapshots__/demo.test.js.snap
#	components/input/__tests__/__snapshots__/demo.test.js.snap
#	components/input/__tests__/__snapshots__/index.test.js.snap
#	components/locale-provider/__tests__/__snapshots__/demo.test.js.snap
#	components/locale-provider/__tests__/__snapshots__/index.test.js.snap
#	components/modal/__tests__/__snapshots__/Modal.test.js.snap
#	components/page-header/__tests__/__snapshots__/demo.test.js.snap
#	components/page-header/index.jsx
#	components/pagination/__tests__/__snapshots__/demo.test.js.snap
#	components/rate/__tests__/__snapshots__/demo.test.js.snap
#	components/select/__tests__/__snapshots__/demo.test.js.snap
#	components/select/__tests__/__snapshots__/index.test.js.snap
#	components/slider/__tests__/__snapshots__/demo.test.js.snap
#	components/spin/__tests__/__snapshots__/demo.test.js.snap
#	components/steps/__tests__/__snapshots__/demo.test.js.snap
#	components/switch/__tests__/__snapshots__/demo.test.js.snap
#	components/table/__tests__/__snapshots__/Table.filter.test.js.snap
#	components/table/__tests__/__snapshots__/Table.sorter.test.js.snap
#	components/table/__tests__/__snapshots__/demo.test.js.snap
#	components/tabs/__tests__/__snapshots__/demo.test.js.snap
#	components/tabs/__tests__/__snapshots__/index.test.js.snap
#	components/time-picker/__tests__/__snapshots__/demo.test.js.snap
#	components/time-picker/__tests__/__snapshots__/index.test.js.snap
#	components/timeline/__tests__/__snapshots__/demo.test.js.snap
#	components/tree-select/__tests__/__snapshots__/demo.test.js.snap
#	components/tree/__tests__/__snapshots__/demo.test.js.snap
#	components/upload/__tests__/__snapshots__/demo.test.js.snap
#	components/upload/__tests__/__snapshots__/uploadlist.test.js.snap
#	types/message.d.ts
2020-04-05 15:09:21 +08:00

231 lines
6.2 KiB
Vue

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