ant-design-vue/components/date-picker/wrapPicker.js

157 lines
4.0 KiB
JavaScript
Raw Normal View History

2018-03-16 03:19:02 +00:00
2018-03-17 13:38:29 +00:00
import TimePickerPanel from '../vc-time-picker/Panel'
2018-03-15 13:40:34 +00:00
import classNames from 'classnames'
import LocaleReceiver from '../locale-provider/LocaleReceiver'
import { generateShowHourMinuteSecond } from '../time-picker'
import enUS from './locale/en_US'
2018-03-17 13:38:29 +00:00
import { getOptionProps, initDefaultProps } from '../_util/props-util'
2018-03-15 13:40:34 +00:00
function getColumns ({ showHour, showMinute, showSecond, use12Hours }) {
let column = 0
if (showHour) {
column += 1
}
if (showMinute) {
column += 1
}
if (showSecond) {
column += 1
}
if (use12Hours) {
column += 1
}
return column
}
2018-03-16 03:19:02 +00:00
export default function wrapPicker (Picker, props, defaultFormat) {
2018-03-15 13:40:34 +00:00
return {
2018-04-08 13:17:20 +00:00
name: Picker.name,
2018-03-17 13:38:29 +00:00
props: initDefaultProps(props, {
format: defaultFormat || 'YYYY-MM-DD',
transitionName: 'slide-up',
popupStyle: {},
locale: {},
prefixCls: 'ant-calendar',
inputPrefixCls: 'ant-input',
}),
2018-03-15 13:40:34 +00:00
mounted () {
const { autoFocus, disabled } = this
if (autoFocus && !disabled) {
this.$nextTick(() => {
this.focus()
})
}
},
methods: {
handleOpenChange (open) {
this.$emit('openChange', open)
},
handleFocus (e) {
this.$emit('focus', e)
},
handleBlur (e) {
this.$emit('blur', e)
},
focus () {
this.$refs.picker.focus()
},
blur () {
this.$refs.picker.blur()
},
getDefaultLocale () {
const result = {
...enUS,
...this.locale,
}
result.lang = {
...result.lang,
...(this.locale || {}).lang,
}
return result
},
renderPicker (locale, localeCode) {
const props = getOptionProps(this)
const { prefixCls, inputPrefixCls, size, showTime, disabled } = props
const pickerClass = classNames(`${prefixCls}-picker`, {
[`${prefixCls}-picker-${size}`]: !!size,
})
const pickerInputClass = classNames(`${prefixCls}-picker-input`, inputPrefixCls, {
[`${inputPrefixCls}-lg`]: size === 'large',
[`${inputPrefixCls}-sm`]: size === 'small',
[`${inputPrefixCls}-disabled`]: disabled,
})
const timeFormat = (showTime && showTime.format) || 'HH:mm:ss'
const vcTimePickerProps = {
...generateShowHourMinuteSecond(timeFormat),
format: timeFormat,
use12Hours: (showTime && showTime.use12Hours),
}
const columns = getColumns(vcTimePickerProps)
const timePickerCls = `${prefixCls}-time-picker-column-${columns}`
const timePickerPanelProps = {
props: {
...vcTimePickerProps,
...showTime,
prefixCls: `${prefixCls}-time-picker`,
placeholder: locale.timePickerLocale.placeholder,
transitionName: 'slide-up',
},
class: timePickerCls,
}
const timePicker = showTime ? (
<TimePickerPanel
{...timePickerPanelProps}
/>
) : null
const pickerProps = {
props: {
...props,
pickerClass,
pickerInputClass,
locale,
localeCode,
timePicker,
},
on: {
...this.$listeners,
openChange: this.handleOpenChange,
focus: this.handleFocus,
blur: this.handleBlur,
},
ref: 'picker',
2018-03-17 13:38:29 +00:00
scopedSlots: this.$scopedSlots || {},
2018-03-15 13:40:34 +00:00
}
return (
<Picker
{...pickerProps}
2018-03-18 14:05:55 +00:00
>
<template slot='renderExtraFooter'>
{this.$slots.renderExtraFooter}
</template>
</Picker>
2018-03-15 13:40:34 +00:00
)
},
},
render () {
return (
<LocaleReceiver
componentName='DatePicker'
defaultLocale={this.getDefaultLocale}
scopedSlots={
{ default: this.renderPicker }
}
/>
)
},
}
}