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

274 lines
6.7 KiB
Vue
Raw Normal View History

2018-03-11 13:28:13 +00:00
<script>
2019-02-01 09:23:00 +00:00
/* eslint react/no-multi-comp:0, 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
}
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-01-12 03:33:27 +00:00
const timePickerElement = (h) => <TimePickerPanel defaultValue={moment('00:00:00', 'HH:mm:ss')} />;
2018-03-11 13:28:13 +00:00
function disabledTime (date) {
2019-01-12 03:33:27 +00:00
console.log('disabledTime', date);
2018-03-11 13:28:13 +00:00
if (date && (date.date() === 15)) {
return {
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 {
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
}
function disabledDate (current) {
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],
data () {
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: {
onChange (value) {
2019-01-12 03:33:27 +00:00
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
},
onShowTimeChange (e) {
this.setState({
showTime: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
onShowDateInputChange (e) {
this.setState({
showDateInput: e.target.checked,
2019-01-12 03:33:27 +00:00
});
2018-03-11 13:28:13 +00:00
},
toggleDisabled () {
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
},
},
render (h) {
2019-01-12 03:33:27 +00:00
const state = this.$data;
2018-03-11 13:28:13 +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}
2019-01-12 03:33:27 +00:00
/>);
2018-03-11 13:28:13 +00:00
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',
}}
>
<DatePicker
animation='slide-up'
disabled={state.disabled}
calendar={calendar}
value={state.value}
onChange={this.onChange}
scopedSlots={{
default: ({ value }) => {
return (
2018-03-12 14:13:59 +00:00
<span tabIndex='0'>
2018-03-11 13:28:13 +00:00
<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>
2019-01-12 03:33:27 +00:00
);
2018-03-11 13:28:13 +00:00
},
}}
>
</DatePicker>
</div>
2019-01-12 03:33:27 +00:00
</div>);
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: {
onChange (value) {
2019-01-12 03:33:27 +00:00
console.log('Calendar change: ', (value && value.format(format)));
this.value = value;
2018-12-27 14:14:32 +00:00
},
},
render () {
2019-01-12 03:33:27 +00:00
const state = this.$data;
2018-12-27 14:14:32 +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}
2019-01-23 14:47:44 +00:00
focusablePanel={false}
2018-12-27 14:14:32 +00:00
/>
2019-01-12 03:33:27 +00:00
</div>);
2018-12-27 14:14:32 +00:00
},
2019-01-12 03:33:27 +00:00
};
2018-12-27 14:14:32 +00:00
2018-03-11 13:28:13 +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
}
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 {
render (h) {
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-01-23 14:47:44 +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>