-
Couldn't load subscription status.
- Fork 280
feat(ui5-dynamic-date-range): introduce From/To (Date & Time) option #12341
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
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
990c766
feat(ui5-dynamic-date-range): Introduce Date/Time Range Option
didip1000 28007af
test: add cypress tests and fix issues
didip1000 1fb87d4
Merge branch 'main' into ddr-dtr
didip1000 521c167
fix: issue with selecting dates
didip1000 0a8b6c3
Merge branch 'main' into ddr-dtr
didip1000 05157c3
Merge branch 'main' into ddr-dtr
ilhan007 abbb095
Merge branch 'main' into ddr-dtr
ilhan007 21bad69
Merge branch 'ddr-dtr' of github.com:SAP/ui5-webcomponents into ddr-dtr
didip1000 91b6488
Merge branch 'main' into ddr-dtr
didip1000 4c7512b
docs: add option to basic playground sample
didip1000 4189fee
fix: address review comments
didip1000 dd634bf
chore: add early returns
didip1000 ff5b73e
Merge branch 'main' into ddr-dtr
didip1000 b2c220a
test: improve merged tests
didip1000 0cd3905
test: reorder tests
didip1000 fc7f81e
chore: fix lint error
didip1000 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
135 changes: 135 additions & 0 deletions
135
packages/main/cypress/support/commands/DynamicDateRange.commands.ts
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| Cypress.Commands.add("ui5DynamicDateRangeOpen", { prevSubject: true }, (prevSubject) => { | ||
| cy.wrap(prevSubject) | ||
| .as("ddr") | ||
| .shadow() | ||
| .find("[ui5-input]") | ||
| .as("input"); | ||
|
|
||
| cy.get("@input") | ||
| .shadow() | ||
| .find("input") | ||
| .as("innerInput"); | ||
|
|
||
| cy.get("@input") | ||
| .find('[ui5-icon]') | ||
| .as("icon"); | ||
|
|
||
| cy.get("@icon") | ||
| .realClick(); | ||
|
|
||
| cy.get("@ddr") | ||
| .ui5DynamicDateRangeOpened(); | ||
|
|
||
| return cy.wrap(prevSubject); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("ui5DynamicDateRangeOpened", { prevSubject: true }, (subject) => { | ||
| cy.wrap(subject) | ||
| .as("ddr"); | ||
|
|
||
| cy.get("@ddr") | ||
| .should("have.attr", "open"); | ||
|
|
||
| cy.get("@ddr") | ||
| .shadow() | ||
| .find("[ui5-responsive-popover]") | ||
| .as("popover") | ||
| .should("have.attr", "open"); | ||
|
|
||
| cy.get("@popover") | ||
| .find("[ui5-list]") | ||
| .as("list") | ||
| .should("be.visible"); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("ui5DynamicDateRangeGetOptionsList", { prevSubject: true }, (subject) => { | ||
| cy.wrap(subject) | ||
| .as("ddr"); | ||
|
|
||
| cy.get("@ddr") | ||
| .shadow() | ||
| .find("[ui5-responsive-popover]") | ||
| .as("popover"); | ||
|
|
||
| cy.get("@popover") | ||
| .should('exist'); | ||
|
|
||
| cy.get("@popover") | ||
| .find("[ui5-list]") | ||
| .as("list"); | ||
|
|
||
| return cy.get('@list') | ||
| .find("[ui5-li]"); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("ui5DynamicDateRangeSelectOption", { prevSubject: true }, (prevSubject, index?: number) => { | ||
| const optionIndex = index ?? 0; | ||
|
|
||
| cy.wrap(prevSubject) | ||
| .as("ddr"); | ||
|
|
||
| cy.get("@ddr") | ||
| .ui5DynamicDateRangeGetOptionsList() | ||
| .eq(optionIndex) | ||
| .realClick(); | ||
|
|
||
| return cy.wrap(prevSubject); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("ui5DynamicDateRangeSetDateTime", { prevSubject: true }, (prevSubject, pickerId: string, dateTimeValue: string) => { | ||
| cy.wrap(prevSubject) | ||
| .as("ddr"); | ||
|
|
||
| cy.get("@ddr") | ||
| .shadow() | ||
| .find("[ui5-responsive-popover]") | ||
| .find(`[ui5-datetime-picker]#${pickerId}`) | ||
| .as("picker"); | ||
|
|
||
| cy.get("@picker") | ||
| .shadow() | ||
| .find("[ui5-datetime-input]") | ||
| .as("input"); | ||
|
|
||
| cy.get("@input") | ||
| .shadow() | ||
| .find("input") | ||
| .as("innerInput"); | ||
|
|
||
| cy.get("@innerInput") | ||
| .clear() | ||
| .realType(dateTimeValue) | ||
| .realPress("Enter"); | ||
|
|
||
| return cy.wrap(prevSubject); | ||
| }); | ||
|
|
||
| Cypress.Commands.add("ui5DynamicDateRangeSubmit", { prevSubject: true }, (prevSubject) => { | ||
| cy.wrap(prevSubject) | ||
| .as("ddr"); | ||
|
|
||
| cy.get("@ddr") | ||
| .shadow() | ||
| .find("[ui5-responsive-popover]") | ||
| .as("popover"); | ||
|
|
||
| cy.get("@popover") | ||
| .find("[ui5-button][design='Emphasized']") | ||
| .as("submitButton"); | ||
|
|
||
| cy.get("@submitButton") | ||
| .realClick(); | ||
| }); | ||
|
|
||
| declare global { | ||
| namespace Cypress { | ||
| interface Chainable { | ||
| ui5DynamicDateRangeOpen(): Chainable<JQuery<HTMLElement>> | ||
| ui5DynamicDateRangeOpened(): Chainable<void> | ||
| ui5DynamicDateRangeGetOptionsList(): Chainable<JQuery<HTMLElement>> | ||
| ui5DynamicDateRangeSelectOption(index?: number): Chainable<JQuery<HTMLElement>> | ||
| ui5DynamicDateRangeSetDateTime(pickerId: string, dateTimeValue: string): Chainable<JQuery<HTMLElement>> | ||
| ui5DynamicDateRangeSubmit(): Chainable<void> | ||
| } | ||
| } | ||
| } |
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
112 changes: 112 additions & 0 deletions
112
packages/main/src/dynamic-date-range-options/DateTimeRange.ts
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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import DateTimeRangeTemplate from "./DateTimeRangeTemplate.js"; | ||
| import type { DynamicDateRangeValue, IDynamicDateRangeOption } from "../DynamicDateRange.js"; | ||
| import DateFormat from "@ui5/webcomponents-localization/dist/DateFormat.js"; | ||
| import type { JsxTemplate } from "@ui5/webcomponents-base/dist/index.js"; | ||
| import { | ||
| DYNAMIC_DATE_TIME_RANGE_TEXT, | ||
| } from "../generated/i18n/i18n-defaults.js"; | ||
| import { dateTimeRangeOptionToDates } from "./toDates.js"; | ||
| import DynamicDateRange from "../DynamicDateRange.js"; | ||
| import getCachedLocaleDataInstance from "@ui5/webcomponents-localization/dist/getCachedLocaleDataInstance.js"; | ||
| import getLocale from "@ui5/webcomponents-base/dist/locale/getLocale.js"; | ||
|
|
||
| const DEFAULT_DELIMITER = "-"; | ||
|
|
||
| /** | ||
| * @class | ||
| * @constructor | ||
| * @public | ||
| * @since 2.16.0 | ||
| */ | ||
|
|
||
| class DateTimeRange implements IDynamicDateRangeOption { | ||
| template: JsxTemplate; | ||
|
|
||
| constructor() { | ||
| this.template = DateTimeRangeTemplate; | ||
| } | ||
|
|
||
| parse(value: string): DynamicDateRangeValue { | ||
| const returnValue = { operator: this.operator, values: [] } as DynamicDateRangeValue; | ||
|
|
||
| if (!value) { | ||
| return returnValue; | ||
| } | ||
| const splitValue = value.split(DEFAULT_DELIMITER); | ||
| const startDate = this._parseDate(splitValue[0].trim()) as Date; | ||
| const endDate = this._parseDate(splitValue[1].trim()) as Date; | ||
|
|
||
| returnValue.values = [startDate, endDate]; | ||
|
|
||
| if (returnValue.values[0].getTime() > returnValue.values[1].getTime()) { | ||
| returnValue.values.reverse(); | ||
| } | ||
|
|
||
| return returnValue; | ||
| } | ||
|
|
||
| _parseDate(value: string): Date | undefined { | ||
| return this.getFormat().parse(value) as Date; | ||
| } | ||
|
|
||
| format(value: DynamicDateRangeValue) { | ||
| const valuesArray = value?.values as Array<Date>; | ||
|
|
||
| if (!valuesArray || valuesArray.length !== 2 || !valuesArray[0] || !valuesArray[1]) { | ||
| return ""; | ||
| } | ||
|
|
||
| const startDate = this._formatDate(valuesArray[0]); | ||
| const endDate = this._formatDate(valuesArray[1]); | ||
|
|
||
| return `${startDate} ${DEFAULT_DELIMITER} ${endDate}`; | ||
| } | ||
|
|
||
| _formatDate(date: Date) { | ||
| return this.getFormat().format(date); | ||
| } | ||
|
|
||
| toDates(value: DynamicDateRangeValue): Array<Date> { | ||
| return dateTimeRangeOptionToDates(value); | ||
| } | ||
|
|
||
| get text(): string { | ||
| return DynamicDateRange.i18nBundle.getText(DYNAMIC_DATE_TIME_RANGE_TEXT); | ||
| } | ||
|
|
||
| get operator() { | ||
| return "DATETIMERANGE"; | ||
| } | ||
|
|
||
| get icon() { | ||
| return "appointment-2"; | ||
| } | ||
|
|
||
| isValidString(value: string): boolean { | ||
| const splitValue = value.split(DEFAULT_DELIMITER); | ||
| const startDate = this._parseDate(splitValue[0].trim()) as Date; | ||
| const endDate = this._parseDate(splitValue[1].trim()) as Date; | ||
|
|
||
| if (!startDate || !endDate || Number.isNaN(startDate.getTime()) || Number.isNaN(endDate.getTime())) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| getFormatPattern() { | ||
| const localeData = getCachedLocaleDataInstance(getLocale()); | ||
| return localeData.getCombinedDateTimePattern("medium", "medium"); | ||
| } | ||
|
|
||
| getFormat(): DateFormat { | ||
| return DateFormat.getDateInstance({ | ||
| strictParsing: true, | ||
| pattern: this.getFormatPattern(), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| DynamicDateRange.register("DATETIMERANGE", DateTimeRange); | ||
|
|
||
| export default DateTimeRange; |
62 changes: 62 additions & 0 deletions
62
packages/main/src/dynamic-date-range-options/DateTimeRangeTemplate.tsx
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import DynamicDateRange from "../DynamicDateRange.js"; | ||
| import DateTimePicker from "../DateTimePicker.js"; | ||
| import Label from "../Label.js"; | ||
| import { | ||
| DYNAMIC_DATE_TIME_RANGE_TEXT_TO_LABEL, | ||
| DYNAMIC_DATE_TIME_RANGE_TEXT_FROM_LABEL, | ||
| } from "../generated/i18n/i18n-defaults.js"; | ||
|
|
||
| export default function DateTimeRangeTemplate(this: DynamicDateRange) { | ||
| const currentOperator = this.currentValue?.operator || "DATETIMERANGE"; | ||
|
|
||
| const getDateFromValue = (index = 0) => { | ||
| if (this.value?.operator === currentOperator && this.value.values && this.value.values.length === 2) { | ||
| return this.getOption(this.value.operator)?.format(this.value)?.split("-")[index].trim(); | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| const handleSelectionChange = () => { | ||
| const fromPicker = this.shadowRoot?.querySelector("[ui5-datetime-picker]#from-picker") as DateTimePicker; | ||
| const toPicker = this.shadowRoot?.querySelector("[ui5-datetime-picker]#to-picker") as DateTimePicker; | ||
|
|
||
| const fromDateValue = fromPicker.dateValue; | ||
| const toDateValue = toPicker.dateValue; | ||
|
|
||
| // If there are no dates selected, clear the value | ||
| if (!(fromDateValue && toDateValue)) { | ||
| this.currentValue = { | ||
| operator: currentOperator, | ||
| values: [] | ||
| }; | ||
| return; | ||
| } | ||
|
|
||
| const newValue = [fromDateValue, toDateValue]; | ||
|
|
||
| if (newValue[0] && newValue[1] && newValue[0].getTime() > newValue[1].getTime()) { | ||
| newValue.reverse(); | ||
| } | ||
|
|
||
| this.currentValue = { | ||
| operator: currentOperator, | ||
| values: newValue, | ||
| }; | ||
| }; | ||
| return ( | ||
| <div class="ui5-last-next-container ui5-last-next-container-padded"> | ||
| <Label class="ui5-ddr-label">{DynamicDateRange.i18nBundle.getText(DYNAMIC_DATE_TIME_RANGE_TEXT_FROM_LABEL)}</Label> | ||
| <DateTimePicker | ||
| id="from-picker" | ||
| onChange={handleSelectionChange} | ||
| value={getDateFromValue()}> | ||
| </DateTimePicker> | ||
| <Label class="ui5-ddr-label">{DynamicDateRange.i18nBundle.getText(DYNAMIC_DATE_TIME_RANGE_TEXT_TO_LABEL)}</Label> | ||
| <DateTimePicker | ||
| id="to-picker" | ||
| onChange={handleSelectionChange} | ||
| value={getDateFromValue(1)}> | ||
| </DateTimePicker> | ||
| </div> | ||
| ); | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.