-
Notifications
You must be signed in to change notification settings - Fork 27
/
timepicker.vue
85 lines (81 loc) · 2.48 KB
/
timepicker.vue
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
<template>
<ui-textbox
type="time"
placeholder="12:00 AM"
v-model="timeValue"
:invalid="isInvalid"
:required="isRequired"
:label="label"
:help="help"
:error="errorMessage"
:disabled="isDisabled || disabled"
iconPosition="right"
@input="update">
<attached-button slot="icon" :name="name" :data="value" :schema="schema" :args="args" @disable="disableInput" @enable="enableInput"></attached-button>
</ui-textbox>
</template>
<script>
import _ from 'lodash';
import dateFormat from 'date-fns/format';
import { parseDate as parseNaturalDate } from 'chrono-node';
import { shouldBeRequired, getValidationError } from '../forms/field-helpers';
import { isEmpty } from '../utils/comparators';
import UiTextbox from 'keen/UiTextbox';
import attachedButton from '../../inputs/attached-button.vue';
export default {
props: ['value', 'label', 'help', 'name', 'schema', 'args', 'disabled'],
data() {
return {
isDisabled: false,
timeValue: ''
};
},
computed: {
isRequired() {
return _.get(this, 'args.validate') ? _.get(this, 'args.validate.required') === true || shouldBeRequired(this.args.validate, this.$store, this.name) : false;
},
errorMessage() {
if (_.get(this, 'args.validate')) {
const validationError = getValidationError(this.timeValue, this.args.validate, this.$store, this.name),
parsed = parseNaturalDate(this.timeValue);
if (validationError) {
return validationError;
} else if (!parsed && !isEmpty(this.timeValue)) {
// only display the time parsing error if the timepicker has data in it
return `${this.help} (Please enter a valid time)`;
}
}
},
isInvalid() {
return !!this.errorMessage;
}
},
mounted() {
this.timeValue = _.isString(this.value) ? this.value : '';
},
methods: {
// every time the value of the input changes, update the store
update(val) {
const parsed = parseNaturalDate(val);
if (parsed) {
this.$emit('update', dateFormat(parsed, 'HH:mm'));
}
},
disableInput() {
this.$emit('disable');
this.isDisabled = true;
},
enableInput() {
this.$emit('enable');
this.isDisabled = false;
},
clear() {
this.timeValue = '';
}
},
components: {
UiTextbox,
attachedButton
}
};
</script>