-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add today btn #486
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
Merged
Merged
feat: add today btn #486
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
315ad1b
add today btn
meghanmae a7d354a
Add setToday method
meghanmae 472fe15
Disable today button for dates that are not allowed
meghanmae 04fc072
Remove 'show adjacent months' and add prop to toggle today button
meghanmae a2c6eab
Update src/coalesce-vue-vuetify3/src/components/input/c-datetime-pick…
meghanmae 9bd8ee6
Merge remote-tracking branch 'origin/gravatt/next/today-btn' into gra…
meghanmae ea4b29b
Update CSS
meghanmae 2cb4c0b
Merge branch 'dev' into gravatt/next/today-btn
meghanmae ae5e482
Update class name & fix merge conflicts
meghanmae 7375921
Revert
meghanmae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| <v-text-field | ||
| v-else | ||
| class="c-datetime-picker" | ||
| :class="{ 'today-btn': showTodayButton }" | ||
| v-bind="inputBindAttrs" | ||
| :rules="effectiveRules" | ||
| :modelValue="internalTextValue == null ? displayedValue : internalTextValue" | ||
|
|
@@ -76,6 +77,12 @@ | |
| :max="max ? endOfDay(max) : undefined" | ||
| v-bind="datePickerProps" | ||
| > | ||
| <template v-slot:actions v-if="showTodayButton"> | ||
| <v-btn @click="setToday" :disabled="!isDateAllowed(new Date())"> | ||
| Today | ||
meghanmae marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </v-btn> | ||
| <v-spacer /> | ||
| </template> | ||
| </v-date-picker> | ||
|
|
||
| <v-divider vertical></v-divider> | ||
|
|
@@ -108,6 +115,11 @@ | |
| } | ||
| } | ||
| } | ||
|
|
||
| .today-btn .c-time-picker__column { | ||
| max-height: 365px; | ||
| } | ||
|
|
||
| .v-date-picker { | ||
| width: 300px; | ||
| overflow-y: auto; | ||
|
|
@@ -225,6 +237,9 @@ const props = withDefaults( | |
| allowedDates?: null | Date[] | ((date: Date) => boolean); | ||
| // Object containing extra props to pass through to `v-date-picker`. | ||
| datePickerProps?: any; | ||
| /** Determines whether the 'Today' button is displayed in the date picker actions. | ||
| * When enabled, the 'Today' button allows users to quickly select the current date. */ | ||
| showTodayButton?: boolean; | ||
| }>(), | ||
| { closeOnDatePicked: null } | ||
| ); | ||
|
|
@@ -535,25 +550,7 @@ function dateChanged(input: unknown) { | |
|
|
||
| function emitInput(value: Date | null) { | ||
| if (value) { | ||
| if (props.allowedDates) { | ||
| // With validation of allowedDates, we have to just return early without emitting | ||
| // since there's no logic we can apply to clamp the date to a valid date. | ||
|
|
||
| if ( | ||
| Array.isArray(props.allowedDates) && | ||
| !props.allowedDates.includes(startOfDay(value)) | ||
| ) { | ||
| error.value.push("The selected date is not allowed."); | ||
| return; | ||
| } else if ( | ||
| typeof props.allowedDates == "function" && | ||
| !props.allowedDates(value) | ||
| ) { | ||
| error.value.push("The selected date is not allowed."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Clamp value within min and max bounds | ||
| if (props.min && value.valueOf() < props.min.valueOf()) { | ||
| value = props.min; | ||
| } | ||
|
|
@@ -562,6 +559,13 @@ function emitInput(value: Date | null) { | |
| value = props.max; | ||
| } | ||
|
|
||
| if (!isDateAllowed(value)) { | ||
ascott18 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // With validation of allowedDates, we have to just return early without emitting | ||
| // since there's no logic we can apply to clamp the date to a valid date. | ||
| error.value.push("The selected date is not allowed."); | ||
| return; | ||
| } | ||
|
|
||
| if (props.step) { | ||
| const stepMs = props.step * 60 * 1000; | ||
| let newTime = Math.round(value.valueOf() / stepMs) * stepMs; | ||
|
|
@@ -584,6 +588,39 @@ function emitInput(value: Date | null) { | |
| } | ||
| } | ||
|
|
||
| function isDateAllowed(date: Date) { | ||
|
||
| if (!date) return false; | ||
|
|
||
| const minDate = props.min ? startOfDay(props.min) : null; | ||
| const maxDate = props.max ? endOfDay(props.max) : null; | ||
|
|
||
| // Check min and max constraints | ||
| if (minDate && date < minDate) return false; | ||
| if (maxDate && date > maxDate) return false; | ||
|
|
||
| // Check allowedDates array or function | ||
| if (props.allowedDates) { | ||
| if ( | ||
| Array.isArray(props.allowedDates) && | ||
| !props.allowedDates.some( | ||
| (allowedDate) => | ||
| startOfDay(allowedDate).getTime() === startOfDay(date).getTime() | ||
| ) | ||
| ) { | ||
| return false; | ||
| } | ||
| if (typeof props.allowedDates === "function" && !props.allowedDates(date)) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| function setToday() { | ||
| dateChanged(new Date()); | ||
| } | ||
|
|
||
| function close() { | ||
| menu.value = false; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unusual class name. The entire component is not a
today-btn. Only the button itself inside the component's menu would make sense to have atoday-btnclass.Consider a more appropriate class name like
c-datetime-picker__menu--has-today-btnon the menu's contentClass, which also adheres to the (mildly consistent) use of BEM selectors in this project.