4
4
-->
5
5
6
6
<template >
7
- <NcActions :manual-open =" true"
8
- :open =" isListOpen"
9
- @click =" isListOpen = !isListOpen" >
10
- <template #icon >
11
- <NcTextField :value.sync =" date" :error =" isInvalidTime" >
12
- <ClockOutline />
13
- </NcTextField >
14
- </template >
15
- <NcActionButton v-for =" time in timeList" :key =" time" @click =" changeFromList(parse(time))" >
16
- <template #icon ></template >
17
- {{ time }}
18
- </NcActionButton >
19
- </NcActions >
7
+ <DateTimePicker ::clearable =" false"
8
+ :first-day-of-week =" firstDay"
9
+ :format =" format"
10
+ :lang =" lang"
11
+ :minute-step =" 5"
12
+ :show-second =" false"
13
+ type =" time"
14
+ :use12h =" showAmPm"
15
+ :value =" date"
16
+ v-bind =" $attrs"
17
+ v-on =" $listeners"
18
+ @change =" change" />
20
19
</template >
21
20
22
21
<script >
23
- import { NcActions , NcTextField , NcActionButton } from ' @nextcloud/vue'
24
- import ClockOutline from ' vue-material-design-icons/ClockOutline.vue'
22
+ import { NcDateTimePicker as DateTimePicker } from ' @nextcloud/vue'
25
23
import moment from ' @nextcloud/moment'
26
24
import { mapState } from ' pinia'
25
+ import {
26
+ getFirstDay ,
27
+ } from ' @nextcloud/l10n'
28
+ import { getLangConfigForVue2DatePicker } from ' ../../utils/localization.js'
27
29
import useSettingsStore from ' ../../store/settings.js'
28
30
29
31
export default {
30
32
name: ' TimePicker' ,
31
33
components: {
32
- NcActions,
33
- NcTextField,
34
- NcActionButton,
35
- ClockOutline,
34
+ DateTimePicker,
36
35
},
37
36
props: {
38
- initialDate : {
37
+ date : {
39
38
type: Date ,
40
39
required: true ,
41
40
},
42
41
},
43
42
data () {
44
43
return {
45
- date: ' ' ,
46
- isInvalidTime: false ,
47
- isListOpen: false ,
44
+ firstDay: getFirstDay () === 0 ? 7 : getFirstDay (),
45
+ format: {
46
+ stringify: this .stringify ,
47
+ parse: this .parse ,
48
+ },
48
49
}
49
50
},
50
51
computed: {
51
52
... mapState (useSettingsStore, {
52
53
locale: ' momentLocale' ,
53
54
}),
55
+ /**
56
+ * Returns the lang config for vue2-datepicker
57
+ *
58
+ * @return {object}
59
+ */
60
+ lang () {
61
+ return getLangConfigForVue2DatePicker (this .locale )
62
+ },
54
63
/**
55
64
* Whether or not to offer am/pm in the timepicker
56
65
*
@@ -62,51 +71,14 @@ export default {
62
71
63
72
return timeFormat .indexOf (' a' ) !== - 1
64
73
},
65
-
66
- timeList () {
67
- const times = []
68
- let currentTime = moment (this .initialDate )
69
-
70
- for (let i = 0 ; i < 10 ; i++ ) {
71
- times .push (currentTime .format (' LT' ))
72
- currentTime = currentTime .add (15 , ' minutes' )
73
- }
74
-
75
- return times
76
- },
77
- },
78
- watch: {
79
- date (value ) {
80
- let isValidTime = false
81
- isValidTime = ! isValidTime ? moment (value, ' LT' , true ).isValid () : isValidTime
82
- isValidTime = ! isValidTime ? moment (value, ' HH:mm' , true ).isValid () : isValidTime
83
- isValidTime = ! isValidTime ? moment (value, ' H:mm' , true ).isValid () : isValidTime
84
-
85
- // Meaning it was changed through textfield
86
- if (! (value instanceof Date ) && isValidTime) {
87
- this .isInvalidTime = false
88
-
89
- const parsedDate = this .parse (value)
90
- this .$emit (' change' , parsedDate)
91
- } else if (! (value instanceof Date )) {
92
- this .isInvalidTime = true
93
- }
94
- },
95
- },
96
- mounted () {
97
- this .date = this .stringify (this .initialDate )
98
74
},
99
75
methods: {
100
76
/**
101
77
* Emits a change event for the Date
102
78
*
103
79
* @param {Date} date The new Date object
104
80
*/
105
- changeFromList (date ) {
106
- this .isInvalidTime = false
107
- this .isListOpen = false
108
-
109
- this .date = this .stringify (date)
81
+ change (date ) {
110
82
this .$emit (' change' , date)
111
83
},
112
84
/**
@@ -125,26 +97,8 @@ export default {
125
97
* @return {Date}
126
98
*/
127
99
parse (value ) {
128
- try {
129
- return moment (value, ' LT' , this .locale ).toDate ()
130
- } catch (e) {
131
- console .error (e)
132
- }
100
+ return moment (value, ' LT' , this .locale ).toDate ()
133
101
},
134
102
},
135
103
}
136
104
</script >
137
-
138
- <style scoped>
139
- :deep(.action-button__icon ) {
140
- display : none ;
141
- }
142
-
143
- :deep(.action-button__text ) {
144
- margin : 0 8px ;
145
- }
146
-
147
- :deep(.input-field__icon--trailing ) {
148
- display : none ;
149
- }
150
- </style >
0 commit comments