Skip to content

Commit

Permalink
fixup! feat(TimePicker): recreate without library
Browse files Browse the repository at this point in the history
Signed-off-by: Grigory Vodyanov <scratchx@gmx.com>
  • Loading branch information
GVodyanov committed Oct 15, 2024
1 parent 36c58c1 commit 5c9ffd0
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions src/components/Shared/TimePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
-->

<template>
<NcActions>
<NcActions :manual-open="true"
:open="isListOpen"
@click="isListOpen = !isListOpen">
<template #icon>
<NcTextField :value.sync="date">
<template #trailing-button-icon>
<ClockOutline/>
</template>
<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="change(parse(time))">
<NcActionButton v-for="time in timeList" :key="time" @click="changeFromList(parse(time))">
<template #icon></template>
{{ time }}
</NcActionButton>
Expand Down Expand Up @@ -43,11 +43,10 @@ export default {
data() {
return {
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,
}
},
mounted() {
this.date = this.stringify(this.initialDate)
},
computed: {
...mapState(useSettingsStore, {
locale: 'momentLocale',
Expand Down Expand Up @@ -76,13 +75,37 @@ export default {
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 @@ -102,8 +125,26 @@ export default {
* @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>

0 comments on commit 5c9ffd0

Please sign in to comment.