ant-design-vue/components/vc-calendar/demo/antd-calendar.vue

271 lines
6.8 KiB
Vue
Raw Normal View History

2018-03-11 13:28:13 +00:00
<script>
2019-12-21 10:37:33 +00:00
/* eslint no-console:0, no-unused-vars:0 */
2018-03-11 13:28:13 +00:00
2019-01-12 03:33:27 +00:00
import '../assets/index.less';
import PropTypes from '@/components/_util/vue-types';
import Calendar from '../';
import DatePicker from '../src/Picker';
import zhCN from '../src/locale/zh_CN';
import enUS from '../src/locale/en_US';
import '../../vc-time-picker/assets/index.less';
import TimePickerPanel from '../../vc-time-picker/Panel';
import BaseMixin from '@/components/_util/BaseMixin';
2018-03-11 13:28:13 +00:00
2019-01-12 03:33:27 +00:00
import moment from 'moment';
import 'moment/locale/zh-cn';
import 'moment/locale/en-gb';
2018-03-11 13:28:13 +00:00
2019-01-12 03:33:27 +00:00
const format = 'YYYY-MM-DD HH:mm:ss';
const cn = window.location.search.indexOf('cn') !== -1;
const now = moment();
2018-03-11 13:28:13 +00:00
if (cn) {
2019-01-12 03:33:27 +00:00
now.locale('zh-cn').utcOffset(8);
2018-03-11 13:28:13 +00:00
} else {
2019-01-12 03:33:27 +00:00
now.locale('en-gb').utcOffset(0);
2018-03-11 13:28:13 +00:00
}
2019-09-28 12:45:07 +00:00
function getFormat(time) {
2019-01-12 03:33:27 +00:00
return time ? format : 'YYYY-MM-DD';
2018-03-11 13:28:13 +00:00
}
2019-01-12 03:33:27 +00:00
const defaultCalendarValue = now.clone();
defaultCalendarValue.add(-1, 'month');
2018-03-11 13:28:13 +00:00
2019-09-28 12:45:07 +00:00
const timePickerElement = h => <TimePickerPanel defaultValue={moment('00:00:00', 'HH:mm:ss')} />;
2018-03-11 13:28:13 +00:00
2019-09-28 12:45:07 +00:00
function disabledTime(date) {
2019-01-12 03:33:27 +00:00
console.log('disabledTime', date);
2019-09-28 12:45:07 +00:00
if (date && date.date() === 15) {
2018-03-11 13:28:13 +00:00
return {
2019-09-28 12:45:07 +00:00
disabledHours() {
2019-01-12 03:33:27 +00:00
return [3, 4];
2018-03-11 13:28:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-11 13:28:13 +00:00
}
return {
2019-09-28 12:45:07 +00:00
disabledHours() {
2019-01-12 03:33:27 +00:00
return [1, 2];
2018-03-11 13:28:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-11 13:28:13 +00:00
}
2019-09-28 12:45:07 +00:00
function disabledDate(current) {
2018-03-11 13:28:13 +00:00
if (!current) {
// allow empty select
2019-01-12 03:33:27 +00:00
return false;
2018-03-11 13:28:13 +00:00
}
2019-01-12 03:33:27 +00:00
const date = moment();
date.hour(0);
date.minute(0);
date.second(0);
return current.valueOf() < date.valueOf(); // can not select days before today
2018-03-11 13:28:13 +00:00
}
const Demo = {
props: {
defaultValue: PropTypes.object,
defaultCalendarValue: PropTypes.object,
},
mixins: [BaseMixin],
2019-09-28 12:45:07 +00:00
data() {
2018-03-11 13:28:13 +00:00
return {
showTime: true,
showDateInput: true,
disabled: false,
value: this.defaultValue,
2019-01-12 03:33:27 +00:00
};
2018-03-11 13:28:13 +00:00
},
methods: {
2019-09-28 12:45:07 +00:00
onChange(value) {
console.log('DatePicker change: ', value && value.format(format));
2018-03-11 13:28:13 +00:00
this.setState({
value,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
2019-09-28 12:45:07 +00:00
onShowTimeChange(e) {
2018-03-11 13:28:13 +00:00
this.setState({
showTime: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
2019-09-28 12:45:07 +00:00
onShowDateInputChange(e) {
2018-03-11 13:28:13 +00:00
this.setState({
showDateInput: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
2019-09-28 12:45:07 +00:00
toggleDisabled() {
2018-03-11 13:28:13 +00:00
this.setState({
2018-03-12 14:13:59 +00:00
disabled: !this.disabled,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
},
2019-09-28 12:45:07 +00:00
render(h) {
2019-01-12 03:33:27 +00:00
const state = this.$data;
2019-09-28 12:45:07 +00:00
const calendar = (
<Calendar
locale={cn ? zhCN : enUS}
style={{ zIndex: 1000 }}
dateInputPlaceholder="please input"
formatter={getFormat(state.showTime)}
disabledTime={state.showTime ? disabledTime : null}
timePicker={state.showTime ? timePickerElement(h) : null}
defaultValue={this.defaultCalendarValue}
showDateInput={state.showDateInput}
disabledDate={disabledDate}
/>
);
return (
<div style={{ width: '400px', margin: '20px' }}>
<div style={{ marginBottom: '10px' }}>
<label>
<input type="checkbox" checked={state.showTime} onChange={this.onShowTimeChange} />
showTime
</label>
&nbsp;&nbsp;&nbsp;&nbsp;
<label>
<input
type="checkbox"
checked={state.showDateInput}
onChange={this.onShowDateInputChange}
/>
showDateInput
</label>
&nbsp;&nbsp;&nbsp;&nbsp;
<label>
<input checked={state.disabled} onChange={this.toggleDisabled} type="checkbox" />
disabled
</label>
</div>
<div
style={{
boxSizing: 'border-box',
position: 'relative',
display: 'block',
lineHeight: 1.5,
marginBottom: '22px',
2018-03-11 13:28:13 +00:00
}}
>
2019-09-28 12:45:07 +00:00
<DatePicker
animation="slide-up"
disabled={state.disabled}
calendar={calendar}
value={state.value}
onChange={this.onChange}
scopedSlots={{
default: ({ value }) => {
return (
<span tabIndex="0">
<input
placeholder="please select"
style={{ width: '250px' }}
disabled={state.disabled}
readOnly
tabIndex="-1"
class="ant-calendar-picker-input ant-input"
value={(value && value.format(getFormat(state.showTime))) || ''}
/>
</span>
);
},
}}
></DatePicker>
</div>
2018-03-11 13:28:13 +00:00
</div>
2019-09-28 12:45:07 +00:00
);
2018-03-11 13:28:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-11 13:28:13 +00:00
2019-01-12 03:33:27 +00:00
const multiFormats = ['DD/MM/YYYY', 'DD/MM/YY', 'DDMMYY', 'D/M/YY'];
2018-12-27 14:14:32 +00:00
const DemoMultiFormat = {
data: () => ({
value: now,
}),
methods: {
2019-09-28 12:45:07 +00:00
onChange(value) {
console.log('Calendar change: ', value && value.format(format));
2019-01-12 03:33:27 +00:00
this.value = value;
2018-12-27 14:14:32 +00:00
},
},
2019-09-28 12:45:07 +00:00
render() {
2019-01-12 03:33:27 +00:00
const state = this.$data;
2019-09-28 12:45:07 +00:00
return (
<div style={{ width: '400px', margin: '20px' }}>
<div style={{ marginBottom: '10px' }}>
Accepts multiple input formats
<br />
<small>{multiFormats.join(', ')}</small>
</div>
<Calendar
locale={cn ? zhCN : enUS}
style={{ zIndex: 1000 }}
dateInputPlaceholder="please input"
format={multiFormats}
value={state.value}
onChange={this.onChange}
focusablePanel={false}
/>
2018-12-27 14:14:32 +00:00
</div>
2019-09-28 12:45:07 +00:00
);
2018-12-27 14:14:32 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-12-27 14:14:32 +00:00
2019-09-28 12:45:07 +00:00
function onStandaloneSelect(value) {
2019-01-12 03:33:27 +00:00
console.log('onStandaloneSelect');
console.log(value && value.format(format));
2018-03-11 13:28:13 +00:00
}
2019-09-28 12:45:07 +00:00
function onStandaloneChange(value) {
2019-01-12 03:33:27 +00:00
console.log('onStandaloneChange');
console.log(value && value.format(format));
2018-03-11 13:28:13 +00:00
}
export default {
2019-09-28 12:45:07 +00:00
render(h) {
2018-03-11 13:28:13 +00:00
return (
<div
style={{
zIndex: 1000,
position: 'relative',
width: '900px',
margin: '20px auto',
}}
>
<div>
<div style={{ margin: '10px' }}>
<Calendar
showWeekNumber={false}
locale={cn ? zhCN : enUS}
defaultValue={now}
disabledTime={disabledTime}
showToday
2018-12-27 14:14:32 +00:00
format={getFormat(true)}
2018-03-11 13:28:13 +00:00
showOk={false}
timePicker={timePickerElement(h)}
onChange={onStandaloneChange}
disabledDate={disabledDate}
onSelect={onStandaloneSelect}
2019-09-28 12:45:07 +00:00
renderFooter={mode => <span>{mode} extra footer</span>}
2018-03-11 13:28:13 +00:00
/>
</div>
<div style={{ float: 'left', width: '300px' }}>
<Demo defaultValue={now} />
</div>
<div style={{ float: 'right', width: '300px' }}>
<Demo defaultCalendarValue={defaultCalendarValue} />
</div>
<div style={{ clear: 'both' }}></div>
2018-12-27 14:14:32 +00:00
<div>
<DemoMultiFormat />
</div>
2018-03-11 13:28:13 +00:00
</div>
</div>
2019-01-12 03:33:27 +00:00
);
2018-03-11 13:28:13 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-03-11 13:28:13 +00:00
</script>