All files / src/components/u-time-picker.vue range.vue

48.14% Statements 13/27
11.11% Branches 2/18
9.09% Functions 1/11
50% Lines 13/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394      7x   7x     7x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
<template>
<span :class="$style.root" :width="width" :height="height">
    <u-range-input
        :class="$style.input"
        :left-value="genDisplayFormatText(startInputTime)"
        :right-value="genDisplayFormatText(endInputTime)"
        :autofocus="autofocus"
        :disabled="!!readonly || disabled"
        ref="input"
        :clearable="clearable" :placeholder="placeholder"
        :placeholder-right="placeholderRight"
        @update:value="onInputChange($event)"
        @left-click="onLeftClick"
        @right-click="onRightClick"
        @focus="onFocus" @blur="onBlur"
        @blur:value="onBlurInputValue($event)"
        @clear="clearValue"
        :prefix="preIcon?preIcon:undefined"
        :suffix="suffixIcon?suffixIcon:undefined"
        :color="formItemVM && formItemVM.color">
        <template #prefix><i-ico v-if="preIcon" :name="preIcon" :class="[$style.preIcon]" notext></i-ico></template>
        <template #suffix><i-ico v-if="suffixIcon" :name="suffixIcon" :class="[$style.suffixIcon]" notext></i-ico></template>
    </u-range-input>
    <u-time-picker-popper
        ref="startPopper"
        :min-unit="minUnit"
        :time="startTime"
        :disabled="disabled"
        :readonly="readonly"
        :min-time="minTime"
        :max-time="maxStartTime"
        :append-to="appendTo"
        :simple-foot="simpleFoot"
        :popper-width="currentPopperWidth"
        :show-right-now-button="showRightNowButton"
        :show-footer-button="showFooterButton"
        :right-now-title="rightNowTitle"
        :cancel-title="cancelTitle"
        :ok-title="okTitle"
        :input-time.sync="startInputTime"
        @change="onStartTimeChange"
        @input="onStartTimeInput"
        @update:time="onUpdateStartTime"
        @blur="onPopperBlur"
        @toggle="onPopperToggle"
    ></u-time-picker-popper>
    <u-time-picker-popper
        ref="endPopper"
        :min-unit="minUnit"
        :time="endTime"
        :disabled="disabled"
        :readonly="readonly"
        :min-time="minEndTime"
        :max-time="maxTime"
        :append-to="appendTo"
        :simple-foot="simpleFoot"
        :popper-width="currentPopperWidth"
        :show-right-now-button="showRightNowButton"
        :show-footer-button="showFooterButton"
        :right-now-title="rightNowTitle"
        :cancel-title="cancelTitle"
        :ok-title="okTitle"
        :input-time.sync="endInputTime"
        @change="onEndTimeChange"
        @input="onEndTimeInput"
        @update:time="onUpdateEndTime"
        @blur="onPopperBlur"
        @toggle="onPopperToggle"
    ></u-time-picker-popper>
</span>
</template>
 
<script>
import dayjs from '../../utils/dayjs';
import DateFormatMixin from '../../mixins/date.format';
import { formatterOptions } from './wrap';
import i18n from './i18n';
import MField from '../m-field.vue';
import UTimePickerPopper from './popper.vue';
import URangeInput from '../u-date-picker.vue/range-input.vue';
import i18nMixin from '../../mixins/i18n';
 
/**
 * @class TimePicker
 * @extend Component
 * @param {object}                  options.data                     =  绑定属性
 * @param {string='00:00'}          options.data.time               <=> 当前的时间值
 * @param {string='00:00'}          options.data.minTime             => 最小时间
 * @param {string='23:59'}          options.data.maxTime             => 最大时间
 * @param {boolean=false}           options.data.autofocus           => 是否自动获得焦点
 * @param {boolean=false}           options.data.readonly            => 是否只读
 * @param {boolean=false}           options.data.disabled            => 是否禁用
 * @param {boolean=true}            options.data.visible             => 是否显示
 * @param {string=''}               options.data.class               => 补充class
 */
 
export default {
    name: 'u-time-range-picker',
    // i18n,
    components: { URangeInput, UTimePickerPopper },
    mixins: [MField, DateFormatMixin, i18nMixin('u-time-picker')],
    props: {
        minUnit: { type: String, default: 'second' },
        startTime: { type: String, default: '' },
        endTime: { type: String, default: '' },
        autofocus: [String, Boolean],
        disabled: [String, Boolean],
        readonly: [String, Boolean],
        minTime: { type: String, default: '00:00:00' },
        maxTime: { type: String, default: '23:59:59' },
        appendTo: {
            type: String,
            default: 'body',
            validator: (value) => ['body', 'reference'].includes(value),
        },
        simpleFoot: { type: Boolean, default: false },
        preIcon: {
            type: String,
            default: 'time',
        },
        suffixIcon: {
            type: String,
            default: '',
        },
        clearable: { type: Boolean, default: true },
        width: String,
        height: String,
        popperWidth: { type: String, default: '' },
        showRightNowButton: { type: Boolean, default: true },
        showFooterButton: { type: Boolean, default: true },
        rightNowTitle: { type: String, default: '' },
        cancelTitle: { type: String, default: '' },
        okTitle: { type: String, default: '' },
        placeholder: {
            type: String,
            default() {
                return this.$tt('selectTimeText');
            },
        },
        placeholderRight: {
            type: String,
        },
    },
    data() {
        return {
            startInputTime: this.startTime,
            endInputTime: this.endTime,
            currentPopperWidth: this.popperWidth,
            editTarget: '', // 标明当前编辑的是起始/结束值
        };
    },
    computed: {
        maxStartTime() {
            return this.endInputTime || this.maxTime;
        },
        minEndTime() {
            return this.startInputTime || this.minTime;
        },
        validShowFormatters() {
            return formatterOptions[this.minUnit];
        },
    },
    created() {
        const value = this.startInputTime && this.endInputTime ? [this.startInputTime, this.endInputTime] : '';
        this.$emit('update', value);
    },
    methods: {
        getFormatString() {
            if (this.minUnit === 'second') {
                return 'HH:mm:ss';
            }
 
            if (this.minUnit === 'minute') {
                return 'HH:mm';
            }
 
            return 'HH:mm:ss';
        },
        getDisplayFormatString() {
            let formatter;
 
            if (this.advancedFormat && this.advancedFormat.enable && this.advancedFormat.value) { // 高级格式化开启
                formatter = this.advancedFormat.value;
            } else if (this.validShowFormatters.includes(this.showFormatter)) { // 配置的展示格式满足
                formatter = this.showFormatter;
            }
 
            if (formatter) {
                return formatter;
            }
 
            return this.getFormatString();
        },
        genDisplayFormatText(value) {
            if (!value)
                return value;
 
            let text = value;
            try {
                const showFormatter = this.getDisplayFormatString();
                const valueFormatter = this.getFormatString();
 
                if (showFormatter && showFormatter !== valueFormatter) {
                    // 拼凑出今天日期
                    const today = dayjs().format('YYYY-MM-DD');
                    const time = new Date(`${today} ${value}`);
                    text = dayjs(time).format(showFormatter);
                }
            } catch (error) {
                console.log(error);
            }
 
            return text;
        },
        callPopperMethod(methodName, ...args) {
            const refName = this.editTarget === 'start' ? 'startPopper' : 'endPopper';
            if (this.$refs[refName] && this.$refs[refName][methodName]) {
                this.$refs[refName][methodName](...args);
            }
 
            this.setPopperWidth();
        },
        onLeftClick() {
            this.editTarget = 'start';
            this.$refs.endPopper && this.$refs.endPopper.close();
            this.callPopperMethod('open');
        },
        onRightClick() {
            this.editTarget = 'end';
            this.$refs.startPopper && this.$refs.startPopper.close();
            this.callPopperMethod('open');
        },
        onInputChange(value) {
            const changedValue = this.editTarget === 'start' ? value.leftValue : value.rightValue;
            this.callPopperMethod('onInputChange', changedValue);
        },
        onBlur() {
            // 先不抛出事件,内部处理掉
            this.callPopperMethod('onBlur');
        },
        onBlurInputValue(value) {
            this.callPopperMethod('onBlurInputValue', value);
            this.$nextTick(() => {
                this.$refs.input.updateCurrentValue({
                    leftValue: this.genDisplayFormatText(this.startInputTime),
                    rightValue: this.genDisplayFormatText(this.endInputTime),
                });
            });
        },
        onFocus(e) {
            this.$emit('focus', e, this);
        },
        onStartTimeChange(e) {
            this.$emit('change', { sender: this, startTime: e.value }, this);
        },
        onEndTimeChange(e) {
            this.$emit('change', { sender: this, endTime: e.value }, this);
        },
        onStartTimeInput(inputValue) {
            const value = inputValue && this.endInputTime ? [inputValue, this.endInputTime] : '';
            this.$emit('input', value, this);
        },
        onEndTimeInput(inputValue) {
            const value = inputValue && this.startInputTime ? [this.startInputTime, inputValue] : '';
            this.$emit('input', value, this);
        },
        onUpdateStartTime(value) {
            this.$emit('update:startTime', value);
        },
        onUpdateEndTime(value) {
            this.$emit('update:endTime', value);
        },
        // blur 有很多种情况,这里放到 popper 内部统一处理
        onPopperBlur() {
            this.$emit('blur', this);
        },
        onPopperToggle(e) {
            this.$emit('toggle', e, this);
        },
        clearValue() {
            this.$refs.startPopper && this.$refs.startPopper.clearValue();
            this.$refs.endPopper && this.$refs.endPopper.clearValue();
        },
        setPopperWidth() {
            if (this.appendTo !== 'body') {
                this.currentPopperWidth = this.popperWidth || '100%';
                return;
            }
            if (this.popperWidth) {
                this.currentPopperWidth = this.popperWidth;
                return;
            }
            const rect = this.$el.getBoundingClientRect();
            this.currentPopperWidth = rect.width;
        },
    },
};
</script>
 
<style module>
.root {
    display: inline-block;
    position: relative;
    width: var(--timepicker-range-input-width);
}
 
.input {
    border: var(--timepicker-input-border-width) solid var(--timepicker-input-border-color);
    color: var(--timepicker-input-color);
    background: var(--timepicker-input-background);
    border-radius: var(--timepicker-input-border-radius);
    height: var(--timepicker-input-height);
}
.input [class^="u-input_placeholder__"] {
    color: var(--timepicker-input-placeholder-color);
}
 
.root[width="mini"] {
    width: var(--timepicker-input-width-mini);
}
 
.root[height="mini"] .input {
    height: var(--timepicker-input-height-mini);
    line-height: calc(var(--timepicker-input-height-mini) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="small"] {
    width: var(--timepicker-input-width-small);
}
 
.root[height="small"] .input {
    height: var(--timepicker-input-height-small);
    line-height: calc(var(--timepicker-input-height-small) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="normal"] {
    width: var(--timepicker-input-width);
}
 
.root[height="normal"] .input {
    height: var(--timepicker-input-height);
    line-height: calc(var(--timepicker-input-height) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="medium"] {
    width: var(--timepicker-input-width-medium);
}
 
.root[height="medium"] .input {
    height: var(--timepicker-input-height-medium);
    line-height: calc(var(--timepicker-input-height-medium) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="large"] {
    width: var(--timepicker-input-width-large);
}
 
.root[height="large"] .input {
    height: var(--timepicker-input-height-large);
    line-height: calc(var(--timepicker-input-height-large) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="huge"] {
    width: var(--timepicker-input-width-huge);
}
 
.root[height="huge"] .input {
    height: var(--timepicker-input-height-huge);
    line-height: calc(var(--timepicker-input-height-huge) - var(--timepicker-input-border-width) * 2);
}
 
.root[width="full"] {
    width: 100%;
    padding-right: var(--timepicker-input-padding-x-full);
}
 
.root[height="full"] .input {
    height: 100%;
}
 
.root .dot {
    padding: 0 5px;
    user-select: none;
}
 
.preIcon {
    color: var(--timepicker-input-pre-icon-color);
}
 
.suffixIcon {
    color: var(--timepicker-input-after-icon-color);
}
</style>