Skip to content

Commit

Permalink
feat:日付のautocompleteのロジックを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
mathsuky committed Dec 2, 2024
1 parent d4e0d58 commit e764440
Showing 1 changed file with 60 additions and 14 deletions.
74 changes: 60 additions & 14 deletions src/components/event/EventFormTimeAndPlaceInstant.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$rules.eventTimeInstant(timeStartInput, timeEndInput)
"
type="date"
@blur="setDefaultDateEnd"
@blur="autoFillDateEnd"
/>
<v-text-field
v-model="timeStartMem"
Expand All @@ -33,6 +33,7 @@
$rules.eventTimeInstant(timeStartInput, timeEndInput)
"
type="date"
@blur="calcDateDiff"
/>
<v-text-field
v-model="timeEndMem"
Expand Down Expand Up @@ -73,6 +74,13 @@ export default class EventFormTimeAndPlaceInstant extends Vue {
private dateEndMem = ''
private timeStartMem = ''
private timeEndMem = ''
private yearDiff = 0
private monthDiff = 0
private dateDiff = 0
private hourDiff = 1
@Ref()
readonly form!: { validate(): void }
created() {
this.dateStartMem = this.timeStartInput && getDate(this.timeStartInput)
Expand All @@ -81,8 +89,16 @@ export default class EventFormTimeAndPlaceInstant extends Vue {
this.timeEndMem = this.timeEndInput && getTime(this.timeEndInput)
}
@Ref()
readonly form!: { validate(): void }
get dateMin(): string {
return today()
}
get valid(): boolean {
return this.value
}
set valid(value: boolean) {
this.$emit('input', value)
}
@Watch('timeStartInput')
@Watch('timeEndInput')
Expand All @@ -102,10 +118,6 @@ export default class EventFormTimeAndPlaceInstant extends Vue {
}
}
public setDefaultDateEnd() {
if (!this.dateEndMem) this.dateEndMem = this.dateStartMem
}
@Watch('dateEndMem')
@Watch('timeEndMem')
private onTimeEndMemChange() {
Expand All @@ -114,15 +126,49 @@ export default class EventFormTimeAndPlaceInstant extends Vue {
}
}
get dateMin(): string {
return today()
}
public autoFillDateEnd() {
if (!this.dateEndMem) {
this.dateEndMem = this.dateStartMem
this.yearDiff = 0
this.monthDiff = 0
this.dateDiff = 0
} else {
const startDate = new Date(this.dateStartMem)
const endDate = new Date(this.dateEndMem)
get valid(): boolean {
return this.value
endDate.setFullYear(startDate.getFullYear() + this.yearDiff)
endDate.setMonth(startDate.getMonth() + this.monthDiff)
endDate.setDate(startDate.getDate() + this.dateDiff)
this.dateEndMem = endDate.toISOString().split('T')[0]
}
}
set valid(value: boolean) {
this.$emit('input', value)
public calcDateDiff() {
if (this.dateStartMem && this.dateEndMem) {
const startDate = new Date(this.dateStartMem)
const endDate = new Date(this.dateEndMem)
this.yearDiff = endDate.getFullYear() - startDate.getFullYear()
if (this.yearDiff < 0) {
this.yearDiff = 0
}
this.monthDiff =
endDate.getMonth() -
startDate.getMonth() +
12 * (endDate.getFullYear() - startDate.getFullYear())
if (this.monthDiff < 0) {
this.monthDiff = 0
}
this.dateDiff = Math.floor(
(endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)
)
if (this.dateDiff < 0) {
this.dateDiff = 0
}
}
}
}
</script>

0 comments on commit e764440

Please sign in to comment.