ant-design-vue/components/vc-time-picker/Panel.jsx

187 lines
5.3 KiB
React
Raw Normal View History

2018-03-19 02:16:27 +00:00
2018-03-08 15:02:04 +00:00
import PropTypes from '../_util/vue-types'
import BaseMixin from '../_util/BaseMixin'
2018-03-07 14:21:55 +00:00
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
}
2018-03-08 15:02:04 +00:00
const Panel = {
mixins: [BaseMixin],
props: {
2018-03-07 14:21:55 +00:00
clearText: PropTypes.string,
2018-03-08 15:02:04 +00:00
prefixCls: PropTypes.string.def('rc-time-picker-panel'),
defaultOpenValue: {
type: Object,
default: () => {
return moment()
},
},
2018-03-17 13:38:29 +00:00
value: PropTypes.any,
defaultValue: PropTypes.any,
2018-03-07 14:21:55 +00:00
placeholder: PropTypes.string,
format: PropTypes.string,
2018-03-08 15:02:04 +00:00
inputReadOnly: PropTypes.bool.def(false),
disabledHours: PropTypes.func.def(noop),
disabledMinutes: PropTypes.func.def(noop),
disabledSeconds: PropTypes.func.def(noop),
2018-03-07 14:21:55 +00:00
hideDisabledOptions: PropTypes.bool,
2018-03-08 15:02:04 +00:00
// onChange: PropTypes.func,
// onEsc: PropTypes.func,
2018-03-07 14:21:55 +00:00
allowEmpty: PropTypes.bool,
showHour: PropTypes.bool,
showMinute: PropTypes.bool,
showSecond: PropTypes.bool,
2018-03-08 15:02:04 +00:00
// onClear: PropTypes.func,
use12Hours: PropTypes.bool.def(false),
2018-03-07 14:21:55 +00:00
hourStep: PropTypes.number,
minuteStep: PropTypes.number,
secondStep: PropTypes.number,
focusOnOpen: PropTypes.bool,
2018-03-08 15:02:04 +00:00
// onKeydown: PropTypes.func,
},
data () {
return {
sValue: this.value,
2018-03-07 14:21:55 +00:00
selectionRange: [],
2018-03-08 15:02:04 +00:00
currentSelectPanel: '',
showStr: true,
2018-03-07 14:21:55 +00:00
}
2018-03-08 15:02:04 +00:00
},
watch: {
value (val) {
if (val) {
this.setState({
sValue: val,
showStr: true,
})
} else {
this.setState({
showStr: false,
})
}
},
},
2018-03-07 14:21:55 +00:00
2018-03-08 15:02:04 +00:00
methods: {
onChange (newValue) {
this.setState({ sValue: newValue })
this.__emit('change', newValue)
},
2018-03-07 14:21:55 +00:00
2018-03-08 15:02:04 +00:00
onCurrentSelectPanelChange (currentSelectPanel) {
this.setState({ currentSelectPanel })
},
2018-03-07 14:21:55 +00:00
2018-03-08 15:02:04 +00:00
// https://github.com/ant-design/ant-design/issues/5829
close () {
this.__emit('esc')
},
2018-03-07 14:21:55 +00:00
2018-03-08 15:02:04 +00:00
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))
}
2018-03-07 14:21:55 +00:00
}
2018-03-08 15:02:04 +00:00
return disabledOptions
},
2018-03-07 14:21:55 +00:00
2018-03-08 15:02:04 +00:00
isAM () {
const value = this.sValue || this.defaultOpenValue
return value.hour() >= 0 && value.hour() < 12
},
},
2018-03-07 14:21:55 +00:00
render () {
const {
2018-03-08 15:02:04 +00:00
prefixCls, placeholder, disabledMinutes,
2018-03-07 14:21:55 +00:00
disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
2018-03-08 15:02:04 +00:00
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)
2018-03-07 14:21:55 +00:00
const hourOptions = generateOptions(
24, disabledHourOptions, hideDisabledOptions, hourStep
)
const minuteOptions = generateOptions(
60, disabledMinuteOptions, hideDisabledOptions, minuteStep
)
const secondOptions = generateOptions(
60, disabledSecondOptions, hideDisabledOptions, secondStep
)
return (
2018-03-08 15:02:04 +00:00
<div class={`${prefixCls}-inner`}>
2018-03-07 14:21:55 +00:00
<Header
clearText={clearText}
prefixCls={prefixCls}
defaultOpenValue={defaultOpenValue}
2018-03-08 15:02:04 +00:00
value={sValue}
2018-03-07 14:21:55 +00:00
currentSelectPanel={currentSelectPanel}
2018-03-08 15:02:04 +00:00
onEsc={esc}
2018-03-07 14:21:55 +00:00
format={format}
placeholder={placeholder}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
2018-03-08 15:02:04 +00:00
disabledHours={this.disabledHours2}
2018-03-07 14:21:55 +00:00
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
onChange={this.onChange}
2018-03-08 15:02:04 +00:00
onClear={clear}
2018-03-07 14:21:55 +00:00
allowEmpty={allowEmpty}
focusOnOpen={focusOnOpen}
2018-03-08 15:02:04 +00:00
onKeydown={keydown}
2018-03-07 14:21:55 +00:00
inputReadOnly={inputReadOnly}
2018-03-08 15:02:04 +00:00
showStr={showStr}
2018-03-07 14:21:55 +00:00
/>
<Combobox
prefixCls={prefixCls}
2018-03-08 15:02:04 +00:00
value={sValue}
2018-03-07 14:21:55 +00:00
defaultOpenValue={defaultOpenValue}
format={format}
onChange={this.onChange}
showHour={showHour}
showMinute={showMinute}
showSecond={showSecond}
hourOptions={hourOptions}
minuteOptions={minuteOptions}
secondOptions={secondOptions}
2018-03-08 15:02:04 +00:00
disabledHours={this.disabledHours2}
2018-03-07 14:21:55 +00:00
disabledMinutes={disabledMinutes}
disabledSeconds={disabledSeconds}
onCurrentSelectPanelChange={this.onCurrentSelectPanelChange}
use12Hours={use12Hours}
isAM={this.isAM()}
/>
2018-03-08 15:02:04 +00:00
{this.$slots.default}
2018-03-07 14:21:55 +00:00
</div>
)
2018-03-08 15:02:04 +00:00
},
2018-03-07 14:21:55 +00:00
}
export default Panel
2018-03-19 02:16:27 +00:00