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.jsx

213 lines
5.7 KiB

import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import Header from './Header';
import Combobox from './Combobox';
import moment from 'moment';
import { getComponentFromProp } from '../_util/props-util';
function noop() {}
7 years ago
function generateOptions(length, disabledOptions, hideDisabledOptions, step = 1) {
const arr = [];
7 years ago
for (let value = 0; value < length; value += step) {
if (!disabledOptions || disabledOptions.indexOf(value) < 0 || !hideDisabledOptions) {
arr.push(value);
7 years ago
}
}
return arr;
7 years ago
}
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,
addon: PropTypes.func.def(noop),
7 years ago
focusOnOpen: PropTypes.bool,
// onKeydown: PropTypes.func,
clearIcon: PropTypes.any,
},
data() {
return {
sValue: this.value,
7 years ago
selectionRange: [],
currentSelectPanel: '',
showStr: true,
};
},
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() {
7 years ago
const {
prefixCls,
placeholder,
disabledMinutes,
addon,
disabledSeconds,
hideDisabledOptions,
allowEmpty,
showHour,
showMinute,
showSecond,
format,
defaultOpenValue,
clearText,
use12Hours,
focusOnOpen,
hourStep,
minuteStep,
secondStep,
inputReadOnly,
sValue,
currentSelectPanel,
showStr,
$listeners = {},
} = this;
const clearIcon = getComponentFromProp(this, 'clearIcon');
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,
);
const hourOptions = generateOptions(24, disabledHourOptions, hideDisabledOptions, hourStep);
7 years ago
const minuteOptions = generateOptions(
60,
disabledMinuteOptions,
hideDisabledOptions,
minuteStep,
);
7 years ago
const secondOptions = generateOptions(
60,
disabledSecondOptions,
hideDisabledOptions,
secondStep,
);
7 years ago
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}
clearIcon={clearIcon}
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()}
/>
{addon(this)}
7 years ago
</div>
);
},
};
export default Panel;