Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/Separate date and time picker #6423

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@change="changeEnd"
@change-timezone="changeEndTimezone" />
</div>
<TimePicker :initial-date="startDate" />
<div v-if="isReadOnly"
class="property-title-time-picker__time-pickers property-title-time-picker__time-pickers--readonly">
<div class="property-title-time-picker-read-only-wrapper property-title-time-picker-read-only-wrapper--start-date">
Expand Down Expand Up @@ -68,6 +69,7 @@
<script>
import moment from '@nextcloud/moment'
import DatePicker from '../../Shared/DatePicker.vue'
import TimePicker from '../../Shared/TimePicker.vue'
import IconTimezone from 'vue-material-design-icons/Web.vue'
import CalendarIcon from 'vue-material-design-icons/Calendar.vue'
import { NcCheckboxRadioSwitch } from '@nextcloud/vue'
Expand All @@ -78,6 +80,7 @@ export default {
name: 'PropertyTitleTimePicker',
components: {
DatePicker,
TimePicker,
IconTimezone,
CalendarIcon,
NcCheckboxRadioSwitch,
Expand Down Expand Up @@ -280,3 +283,9 @@ export default {
},
}
</script>

<style scoped>
:deep(.button-vue--icon-only), :deep(.button-vue__icon) {
width: 7rem !important;
}
</style>
87 changes: 7 additions & 80 deletions src/components/Shared/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,20 @@
-->

<template>
<DateTimePicker :lang="lang"
:first-day-of-week="firstDay"
:format="'YYYY-MM-DD HH:mm'"
:formatter="formatter"
<DateTimePicker id="date-time-picker-input"
:min="min"
:max="max"
:value="date"
:type="actualType"
:clearable="false"
:minute-step="5"
:disabled-date="disabledDate"
:show-second="false"
:show-time-panel="showTimePanel"
:show-week-number="showWeekNumbers"
:use12h="showAmPm"
:append-to-body="appendToBody"
v-bind="$attrs"
confirm
type="date"
class="date-time-picker"
v-on="$listeners"
@close="close"
@change="change"
@pick="pickDate">
<template #icon-calendar>
<IconNewCalendar v-if="isAllDay" :size="20" class="date-time-picker__icon" />
<NcPopover v-else open-class="timezone-popover-wrapper">
<template #trigger>
<NcButton type="tertiary-no-background"
:aria-label="t('calendar', 'Select a time zone')"
@mousedown="(e) => e.stopPropagation()">
<template #icon>
<IconTimezone :size="20"
class="date-time-picker__icon"
:class="{ 'date-time-picker__icon--highlight': highlightTimezone }" />
</template>
</NcButton>
</template>
<template>
<div class="timezone-popover-wrapper__title">
<strong>
{{ $t('calendar', 'Please select a time zone:') }}
</strong>
</div>
<TimezonePicker class="timezone-popover-wrapper__timezone-select"
:value="timezoneId"
@input="changeTimezone" />
</template>
</NcPopover>
</template>
<template v-if="!isAllDay"
#footer>
<NcButton v-if="!showTimePanel"
class="mx-btn mx-btn-text"
@click="toggleTimePanel">
{{ $t('calendar', 'Pick a time') }}
</NcButton>
<NcButton v-else
class="mx-btn mx-btn-text"
@click="toggleTimePanel">
{{ $t('calendar', 'Pick a date') }}
</NcButton>
</template>
</DateTimePicker>
@input="change"
@pick="pickDate" />
</template>

<script>
import {
NcButton,
NcDateTimePicker as DateTimePicker,
NcDateTimePickerNative as DateTimePicker,
NcPopover,
NcTimezonePicker as TimezonePicker,
} from '@nextcloud/vue'
Expand All @@ -92,12 +39,12 @@
export default {
name: 'DatePicker',
components: {
NcButton,

Check failure on line 42 in src/components/Shared/DatePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The "NcButton" component has been registered but not used
DateTimePicker,
NcPopover,

Check failure on line 44 in src/components/Shared/DatePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The "NcPopover" component has been registered but not used
TimezonePicker,

Check failure on line 45 in src/components/Shared/DatePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The "TimezonePicker" component has been registered but not used
IconTimezone,

Check failure on line 46 in src/components/Shared/DatePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The "IconTimezone" component has been registered but not used
IconNewCalendar,

Check failure on line 47 in src/components/Shared/DatePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

The "IconNewCalendar" component has been registered but not used
},
props: {
date: {
Expand Down Expand Up @@ -175,19 +122,6 @@

return this.timezoneId !== this.userTimezoneId
},
/**
* Type of the DatePicker.
* Ether date if allDay or datetime
*
* @return {string}
*/
actualType() {
if (this.type === 'datetime' && this.isAllDay) {
return 'date'
}

return this.type
},
/**
* The earliest date a user is allowed to pick in the timezone
*
Expand Down Expand Up @@ -245,13 +179,6 @@
changeTimezone(timezoneId) {
this.$emit('change-timezone', timezoneId)
},
/**
* Reset to time-panel on close of datepicker
*/
close() {
this.showTimePanel = true
this.$emit('close')
},
/**
* Toggles the time-picker
*/
Expand Down
114 changes: 80 additions & 34 deletions src/components/Shared/TimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,53 @@
-->

<template>
<DateTimePicker ::clearable="false"
:first-day-of-week="firstDay"
:format="format"
:lang="lang"
:minute-step="5"
:show-second="false"
type="time"
:use12h="showAmPm"
:value="date"
v-bind="$attrs"
v-on="$listeners"
@change="change" />
<NcActions :manual-open="true"
:open="isListOpen"
@click="isListOpen = !isListOpen">
<template #icon>
<NcTextField :value.sync="date" :error="isInvalidTime">
<ClockOutline/>

Check failure on line 12 in src/components/Shared/TimePicker.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Expected a space before '/>', but not found
</NcTextField>
</template>
<NcActionButton v-for="time in timeList" :key="time" @click="changeFromList(parse(time))">
<template #icon></template>
{{ time }}
</NcActionButton>
</NcActions>
</template>

<script>
import { NcDateTimePicker as DateTimePicker } from '@nextcloud/vue'
import { NcActions, NcTextField, NcActionButton } from '@nextcloud/vue'
import ClockOutline from 'vue-material-design-icons/ClockOutline.vue'
import moment from '@nextcloud/moment'
import { mapState } from 'pinia'
import {
getFirstDay,
} from '@nextcloud/l10n'
import { getLangConfigForVue2DatePicker } from '../../utils/localization.js'
import useSettingsStore from '../../store/settings.js'

export default {
name: 'TimePicker',
components: {
DateTimePicker,
NcActions,
NcTextField,
NcActionButton,
ClockOutline,
},
props: {
date: {
initialDate: {

Check warning on line 38 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L38

Added line #L38 was not covered by tests
type: Date,
required: true,
},
},
data() {
return {
firstDay: getFirstDay() === 0 ? 7 : getFirstDay(),
format: {
stringify: this.stringify,
parse: this.parse,
},
date: '',
isInvalidTime: false,

Check warning on line 46 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L45-L46

Added lines #L45 - L46 were not covered by tests
isListOpen: false,
}
},
computed: {
...mapState(useSettingsStore, {
locale: 'momentLocale',
}),
/**
* Returns the lang config for vue2-datepicker
*
* @return {object}
*/
lang() {
return getLangConfigForVue2DatePicker(this.locale)
},
/**
* Whether or not to offer am/pm in the timepicker
*
Expand All @@ -71,14 +62,51 @@

return timeFormat.indexOf('a') !== -1
},

timeList() {
const times = []
let currentTime = moment(this.initialDate)

Check warning on line 69 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L68-L69

Added lines #L68 - L69 were not covered by tests
for (let i = 0; i < 10; i++) {
times.push(currentTime.format('LT'))
currentTime = currentTime.add(15, 'minutes')
}

return times
},
},
watch: {
date(value) {
let isValidTime = false
isValidTime = !isValidTime ? moment(value, 'LT', true).isValid() : isValidTime

Check warning on line 81 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L77-L81

Added lines #L77 - L81 were not covered by tests
isValidTime = !isValidTime ? moment(value, 'HH:mm', true).isValid() : isValidTime
isValidTime = !isValidTime ? moment(value, 'H:mm', true).isValid() : isValidTime

// Meaning it was changed through textfield
if (!(value instanceof Date) && isValidTime) {
this.isInvalidTime = false

const parsedDate = this.parse(value)
this.$emit('change', parsedDate)

Check warning on line 90 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L89-L90

Added lines #L89 - L90 were not covered by tests
} else if (!(value instanceof Date)) {
this.isInvalidTime = true
}
},
},
mounted() {
this.date = this.stringify(this.initialDate)
},
methods: {
/**
* Emits a change event for the Date
*
* @param {Date} date The new Date object
*/
change(date) {
changeFromList(date) {
this.isInvalidTime = false
this.isListOpen = false

this.date = this.stringify(date)

Check warning on line 109 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L108-L109

Added lines #L108 - L109 were not covered by tests
this.$emit('change', date)
},
/**
Expand All @@ -97,8 +125,26 @@
* @return {Date}
*/
parse(value) {
return moment(value, 'LT', this.locale).toDate()
try {
return moment(value, 'LT', this.locale).toDate()
} catch (e) {
console.error(e)

Check warning on line 131 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L130-L131

Added lines #L130 - L131 were not covered by tests
}
},
},
}
</script>

<style scoped>
:deep(.action-button__icon) {
display: none;
}

:deep(.action-button__text) {
margin: 0 8px;
}

:deep(.input-field__icon--trailing) {

Check warning on line 147 in src/components/Shared/TimePicker.vue

View check run for this annotation

Codecov / codecov/patch

src/components/Shared/TimePicker.vue#L146-L147

Added lines #L146 - L147 were not covered by tests
display: none;
}
</style>
Loading