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/vc-time-picker/Panel.vue

187 lines
5.3 KiB

7 years ago
<script>
import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
7 years ago
import Header from './Header'
import Combobox from './Combobox'
import moment from 'moment'
function noop () {
}
function generateOptions (length, disabledOptions, hideDisabledOptions, step = 1) {
const arr = []
for (let value = 0; value < length; value += step) {
if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
arr.push(value)
}
}
return arr
}
const Panel = {
mixins: [BaseMixin],
props: {
7 years ago
clearText: PropTypes.string,
prefixCls: PropTypes.string.def('rc-time-picker-panel'),
defaultOpenValue: {
type: Object,
default: () => {
return moment()
},
},
value: PropTypes.any,
defaultValue: PropTypes.any,
7 years ago
placeholder: PropTypes.string,
format: PropTypes.string,
inputReadOnly: PropTypes.bool.def(false),
disabledHours: PropTypes.func.def(noop),
disabledMinutes: PropTypes.func.def(noop),
disabledSeconds: PropTypes.func.def(noop),
7 years ago
hideDisabledOptions: PropTypes.bool,
// onChange: PropTypes.func,
// onEsc: PropTypes.func,
7 years ago
allowEmpty: PropTypes.bool,
showHour: PropTypes.bool,
showMinute: PropTypes.bool,
showSecond: PropTypes.bool,
// onClear: PropTypes.func,
use12Hours: PropTypes.bool.def(false),
7 years ago
hourStep: PropTypes.number,
minuteStep: PropTypes.number,
secondStep: PropTypes.number,
focusOnOpen: PropTypes.bool,
// onKeydown: PropTypes.func,
},
data () {
return {
sValue: this.value,
7 years ago
selectionRange: [],
currentSelectPanel: '',
showStr: true,
7 years ago
}
},
watch: {
value (val) {
if (val) {
this.setState({
sValue: val,
showStr: true,
})
} else {
this.setState({
showStr: false,
})
}
},
},
7 years ago
methods: {
onChange (newValue) {
this.setState({ sValue: newValue })
this.__emit('change', newValue)
},
7 years ago
onCurrentSelectPanelChange (currentSelectPanel) {
this.setState({ currentSelectPanel })
},
7 years ago
// https://github.com/ant-design/ant-design/issues/5829
close () {
this.__emit('esc')
},
7 years ago
disabledHours2 () {
const { use12Hours, disabledHours } = this
let disabledOptions = disabledHours()
if (use12Hours && Array.isArray(disabledOptions)) {
if (this.isAM()) {
disabledOptions = disabledOptions.filter(h => h < 12).map(h => (h === 0 ? 12 : h))
} else {
disabledOptions = disabledOptions.map(h => (h === 12 ? 12 : h - 12))
}
7 years ago
}
return disabledOptions
},
7 years ago
isAM () {
const value = this.sValue || this.defaultOpenValue
return value.hour() >= 0 && value.hour() < 12
},
},
7 years ago
render () {
const {
prefixCls, placeholder, disabledMinutes,
7 years ago
disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
format, defaultOpenValue, clearText, use12Hours,
focusOnOpen, hourStep, minuteStep, secondStep, inputReadOnly,
sValue, currentSelectPanel, showStr, $listeners = {},
} = this
const { esc = noop, clear = noop, keydown = noop } = $listeners
const disabledHourOptions = this.disabledHours2()
const disabledMinuteOptions = disabledMinutes(sValue ? sValue.hour() : null)
const disabledSecondOptions = disabledSeconds(sValue ? sValue.hour() : null,
sValue ? sValue.minute() : null)
7 years ago
const hourOptions = generateOptions(
24, disabledHourOptions, hideDisabledOptions, hourStep
)
const minuteOptions = generateOptions(
60, disabledMinuteOptions, hideDisabledOptions, minuteStep
)
const secondOptions = generateOptions(
60, disabledSecondOptions, hideDisabledOptions, secondStep
)
return (
<div class={`${prefixCls}-inner`}>
7 years ago
<Header
clearText={clearText}
prefixCls={prefixCls}
defaultOpenValue={defaultOpenValue}
value={sValue}
7 years ago
currentSelectPanel={currentSelectPanel}
onEsc={esc}
7 years ago
format={format}
placeholder={placeholder}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
disabledHours={this.disabledHours2}
7 years ago
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
onChange={this.onChange}
onClear={clear}
7 years ago
allowEmpty={allowEmpty}
focusOnOpen={focusOnOpen}
onKeydown={keydown}
7 years ago
inputReadOnly={inputReadOnly}
showStr={showStr}
7 years ago
/>
<Combobox
prefixCls={prefixCls}
value={sValue}
7 years ago
defaultOpenValue={defaultOpenValue}
format={format}
onChange={this.onChange}
showHour={showHour}
showMinute={showMinute}
showSecond={showSecond}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
disabledHours={this.disabledHours2}
7 years ago
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
use12Hours={use12Hours}
isAM={this.isAM()}
/>
{this.$slots.default}
7 years ago
</div>
)
},
7 years ago
}
export default Panel
</script>