Skip to content

Commit 382a86e

Browse files
committed
feat: 24hr format for Material Timepicker quasarframework#271
1 parent 90100a2 commit 382a86e

File tree

7 files changed

+73
-27
lines changed

7 files changed

+73
-27
lines changed

dev/components/form/datetime.vue

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
</p>
5454
<q-datetime v-model="model" type="time"></q-datetime>
5555

56+
<p class="caption">Time 24hr Format</p>
57+
<q-datetime v-model="model" type="time" format24h></q-datetime>
58+
5659
<p class="caption">Date & Time</p>
5760
<q-datetime v-model="model" type="datetime"></q-datetime>
5861

@@ -143,6 +146,9 @@
143146
</p>
144147
<q-inline-datetime v-model="model" type="time"></q-inline-datetime>
145148

149+
<p class="caption">Time 24hr Format</p>
150+
<q-inline-datetime v-model="model" type="time" format24h></q-inline-datetime>
151+
146152
<p class="caption">Date & Time</p>
147153
<q-inline-datetime v-model="model" type="datetime"></q-inline-datetime>
148154

src/components/datetime/InlineDatetimeMat.vue

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
{{ __pad(minute) }}
5050
</span>
5151
</div>
52-
<div class="q-datetime-ampm column justify-around">
52+
<div v-if="!format24h" class="q-datetime-ampm column justify-around">
5353
<div
5454
:class="{active: am}"
5555
class="q-datetime-link"
@@ -156,12 +156,23 @@
156156
<div class="q-datetime-clock-pointer" :style="clockPointerStyle" :class="{hidden: !value}">
157157
<span></span>
158158
</div>
159-
<div
160-
v-for="n in 12"
161-
class="q-datetime-clock-position"
162-
:class="['q-datetime-clock-pos-' + n, value && n === hour ? 'active' : '']"
163-
>
164-
{{ n }}
159+
<div v-if="format24h">
160+
<div
161+
v-for="n in 24"
162+
class="q-datetime-clock-position fmt24"
163+
:class="[`q-datetime-clock-pos-${n-1}`, value && (n - 1) === hour ? 'active' : '']"
164+
>
165+
{{ n - 1 }}
166+
</div>
167+
</div>
168+
<div v-else>
169+
<div
170+
v-for="n in 12"
171+
class="q-datetime-clock-position"
172+
:class="['q-datetime-clock-pos-' + n, value && n === hour ? 'active' : '']"
173+
>
174+
{{ n }}
175+
</div>
165176
</div>
166177
</div>
167178
</div>
@@ -201,7 +212,7 @@
201212
<script>
202213
import { moment } from '../../deps'
203214
import { inline as props } from './datetime-props'
204-
import { height, width, offset } from '../../utils/dom'
215+
import { height, width, offset, cssTransform } from '../../utils/dom'
205216
import { between } from '../../utils/format'
206217
import { position } from '../../utils/event'
207218
import { QIcon } from '../icon'
@@ -374,7 +385,10 @@ export default {
374385
},
375386
376387
hour () {
377-
return convertToAmPm(this.date.hour())
388+
const h = this.date.hour()
389+
return this.format24h
390+
? h
391+
: convertToAmPm(h)
378392
},
379393
minute () {
380394
return this.date.minute()
@@ -384,14 +398,10 @@ export default {
384398
},
385399
clockPointerStyle () {
386400
let
387-
divider = this.view === 'minute' ? 60 : 12,
401+
divider = this.view === 'minute' ? 60 : (this.format24h ? 24 : 12),
388402
degrees = Math.round((this.view === 'minute' ? this.minute : this.hour) * (360 / divider)) - 180
389403
390-
return {
391-
'-webkit-transform': 'rotate(' + degrees + 'deg)',
392-
'-ms-transform': 'rotate(' + degrees + 'deg)',
393-
'transform': 'rotate(' + degrees + 'deg)'
394-
}
404+
return cssTransform(`rotate(${degrees}deg)`)
395405
},
396406
editable () {
397407
return !this.disable && !this.readonly
@@ -439,11 +449,16 @@ export default {
439449
if (!this.editable) {
440450
return
441451
}
442-
this.view = 'minute'
443-
value = this.__parseTypeValue('hour', value) % 12
452+
value = this.__parseTypeValue('hour', value)
444453
445-
if (!this.am) {
446-
value += 12
454+
if (this.format24h) {
455+
value = value % 24
456+
}
457+
else {
458+
value = value % 12
459+
if (!this.am) {
460+
value += 12
461+
}
447462
}
448463
449464
this.date.hour(value)
@@ -489,6 +504,7 @@ export default {
489504
ev.stopPropagation()
490505
ev.preventDefault()
491506
this.dragging = false
507+
this.view = 'minute'
492508
},
493509
__updateClock (ev) {
494510
let
@@ -508,7 +524,7 @@ export default {
508524
}
509525
510526
if (this.view === 'hour') {
511-
this.setHour(Math.round(angle / 30))
527+
this.setHour(Math.round(angle / (this.format24h ? 15 : 30)))
512528
}
513529
else {
514530
this.setMinute(Math.round(angle / 6))

src/components/datetime/QDatetime.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@
2121
:disable="disable || readonly"
2222
:anchor-click="false"
2323
>
24-
<q-inline-datetime v-model="model" :type="type" :min="min" :max="max" class="no-border">
24+
<q-inline-datetime
25+
v-model="model"
26+
:type="type"
27+
:min="min"
28+
:max="max"
29+
:format24h="format24h"
30+
class="no-border"
31+
>
2532
<div class="modal-buttons row full-width">
2633
<q-btn v-if="!noClear" @click="clear()" class="primary clear" v-html="clearLabel"></q-btn>
2734
<div class="auto"></div>
@@ -40,7 +47,14 @@
4047
:position-classes="position"
4148
:content-css="css"
4249
>
43-
<q-inline-datetime v-model="model" :type="type" :min="min" :max="max" class="no-border full-width">
50+
<q-inline-datetime
51+
v-model="model"
52+
:type="type"
53+
:min="min"
54+
:max="max"
55+
:format24h="format24h"
56+
class="no-border full-width"
57+
>
4458
<div class="modal-buttons row full-width">
4559
<q-btn v-if="!noClear" @click="clear()" class="primary clear" v-html="clearLabel"></q-btn>
4660
<div class="auto"></div>

src/components/datetime/QDatetimeRange.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
:readonly="readonly"
1919
:disable="disable"
2020
:align="align"
21+
:format24h="format24h"
2122
class="inline"
2223
:class="className"
2324
:style="css"
@@ -41,6 +42,7 @@
4142
:readonly="readonly"
4243
:disable="disable"
4344
:align="align"
45+
:format24h="format24h"
4446
class="inline"
4547
:class="className"
4648
:style="css"

src/components/datetime/datetime-props.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const input = {
2525
type: String,
2626
default: 'Cancel'
2727
},
28+
format24h: Boolean,
2829
defaultSelection: String,
2930
floatLabel: String,
3031
stackedLabel: String,
@@ -56,6 +57,7 @@ export const inline = {
5657
type: String,
5758
default: ''
5859
},
60+
format24h: Boolean,
5961
readonly: Boolean,
6062
disable: Boolean
6163
}

src/components/datetime/datetime.mat.styl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ body.desktop .q-datetime-clock-position:not(.active):hover
163163
for $pos in 0..12
164164
$degree = (270 + 30 * $pos)
165165
.q-datetime-clock-pos-{$pos}
166-
top (((1 + sin($degree * 1deg)) * 100%) / 2)
167-
left (((1 + cos($degree * 1deg)) * 100%) / 2)
166+
top round(((1 + sin($degree * 1deg)) * 100%) / 2, 2)
167+
left round(((1 + cos($degree * 1deg)) * 100%) / 2, 2)
168+
169+
for $pos in 0..23
170+
$degree = (270 + 15 * $pos)
171+
.q-datetime-clock-pos-{$pos}.fmt24
172+
top round(((1 + sin($degree * 1deg)) * 100%) / 2, 2)
173+
left round(((1 + cos($degree * 1deg)) * 100%) / 2, 2)
168174

169175
.q-datetime-range
170176
display flex

src/components/layout/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import QLayout from './QLayout.vue'
2-
import QSideLink from './QSideLink.vue'
32
import QFixedPosition from './QFixedPosition.vue'
3+
import QSideLink from './QSideLink.vue'
44

55
export {
66
QLayout,
7-
QSideLink,
8-
QFixedPosition
7+
QFixedPosition,
8+
QSideLink
99
}

0 commit comments

Comments
 (0)