Skip to content

Commit 8fb6d1a

Browse files
authored
Merge pull request #6704 from nextcloud-libraries/doc/date-time-picker
docs: improve examples and documentation for `NcDateTimePicker`
2 parents 4858de7 + 3f6f574 commit 8fb6d1a

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

src/components/NcDateTimePicker/NcDateTimePicker.vue

+47-12
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
-->
55

66
<docs>
7-
> We're wrapping the awesome datepicker library here https://github.com/mengxiong10/vue-datepicker-next
8-
> Please check there for all the available options.
7+
In general it is recommended to use the native date picker (see `NcDateTimePickerNative` which is based on `<input type="date">`).
8+
But some use cases are not covered by the native date selector, like selecting ranges or selecting a timezone.
9+
For those cases this component can be used.
910

10-
### Defaults
11-
- cleareable: false
12-
- minute-step: 10
13-
14-
### Example
11+
### General examples
1512
```vue
1613
<template>
1714
<div class="wrapper">
@@ -54,6 +51,11 @@ export default {
5451
```
5552

5653
### Example with confirm button
54+
55+
By default the date is applied as soon as you select the day in the calendar.
56+
Sometimes - especially when selecting date and time - it is required to only emit the selected date when the flow is finished.
57+
For this the `confirm` prop can be used, this will add a confirmation button to the selector.
58+
5759
```vue
5860
<template>
5961
<span>
@@ -76,24 +78,57 @@ export default {
7678
```
7779

7880
### Range picker
81+
82+
The most common use case for the `NcDateTimePicker` is picking a range, as this is not supported by the native date picker.
83+
84+
When selecting the range picker type, the model value type will change from `Date` to `[Date, Date]`.
85+
Meaning an array with two dates is used, the first date is the range start and the second date is the range end.
86+
7987
```vue
8088
<template>
81-
<span>
89+
<div>
90+
<fieldset class="type-select">
91+
<legend>Picker mode</legend>
92+
<NcCheckboxRadioSwitch v-model="type" type="radio" value="range">Date</NcCheckboxRadioSwitch>
93+
<NcCheckboxRadioSwitch v-model="type" type="radio" value="range-datetime">Date and time</NcCheckboxRadioSwitch>
94+
</fieldset>
95+
8296
<NcDateTimePicker
8397
v-model="time"
84-
type="range-datetime" />
85-
{{ time }}
86-
</span>
98+
:type />
99+
<div>
100+
<div>Start: {{ formatDate(time[0]) }}</div>
101+
<div>End: {{ formatDate(time[1]) }}</div>
102+
</div>
103+
</div>
87104
</template>
88105
<script>
89106
export default {
90107
data() {
91108
return {
92-
time: null,
109+
time: [new Date(2025, 3, 18), new Date(2025, 3, 21)],
110+
type: 'range',
93111
}
94112
},
113+
methods: {
114+
formatDate(date) {
115+
const text = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
116+
if (this.type === 'range') {
117+
return text
118+
}
119+
return `${text} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
120+
},
121+
},
95122
}
96123
</script>
124+
125+
<style scoped>
126+
.type-select {
127+
display: flex;
128+
flex-direction: row;
129+
flex-wrap: wrap;
130+
}
131+
</style>
97132
```
98133

99134
### Time zone aware date picker

0 commit comments

Comments
 (0)