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(ui5-date-picker): introduce value-state-change event #8133

Merged
merged 10 commits into from
Jan 26, 2024
43 changes: 38 additions & 5 deletions packages/main/src/DatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ type DatePickerChangeEventDetail = {
valid: boolean,
}

type ValueStateChangeEventDetail = {
hinzzx marked this conversation as resolved.
Show resolved Hide resolved
valueState: `${ValueState}`,
valid: boolean,
}

type DatePickerInputEventDetail = {
value: string,
valid: boolean,
Expand Down Expand Up @@ -217,6 +222,31 @@ type DatePickerInputEventDetail = {
},
},
})
/**
* Fired before the value state of the component is updated internally.
* The event is preventable, meaning that if it's default action is
* prevented, the component will not update the value state.
*
* @allowPreventDefault
* @public
* @param {string} valueState The new <code>valueState</code> that will be set.
*/
@event<ValueStateChangeEventDetail>("value-state-change", {
detail: {
/**
* @public
*/
valueState: {
type: String,
},
/**
* @public
*/
valid: {
type: Boolean,
},
},
})
class DatePicker extends DateComponentBase implements IFormElement {
/**
* Defines a formatted date value.
Expand Down Expand Up @@ -542,12 +572,15 @@ class DatePicker extends DateComponentBase implements IFormElement {
}

_updateValueState() {
const isValid = this._checkValueValidity(this.value);
const valid = this._checkValueValidity(this.value);
const previousValueState = this.valueState;

this.valueState = valid ? ValueState.None : ValueState.Error;

const eventPrevented = !this.fireEvent<ValueStateChangeEventDetail>("value-state-change", { valueState: this.valueState, valid }, true);

if (isValid && this.valueState === ValueState.Error) { // If not valid - always set Error regardless of the current value state
this.valueState = ValueState.None;
} else if (!isValid) { // However if valid, change only Error (but not the others) to None
this.valueState = ValueState.Error;
if (eventPrevented) {
this.valueState = previousValueState;
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/main/test/pages/DatePicker.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
</head>
<body class="datepicker1auto">
<div style='width:600px;'>

<h3> DatePicker with value-state-change event prevented </h3>
<ui5-date-picker id="dpVsChangePrevented" value-state="Error"></ui5-date-picker>

<div>
<h3> DatePicker with no format pattern & min-max dates in ISO format</h3>
<ui5-date-picker id="dpMinMax-dates" min-date="2019-09-01" max-date="2019-11-01"></ui5-date-picker>
Expand Down Expand Up @@ -199,6 +203,12 @@ <h3>DatePicker with format `yyyy` should open picker on years</h3>
dp11.setAttribute("value", dp11.formatValue(new Date(2018, 11, 11)));
});

const datePickerValueStateChangePrevented = document.getElementById("dpVsChangePrevented");

datePickerValueStateChangePrevented.addEventListener("value-state-change", function(e) {
e.preventDefault();
});

</script>
</body>
</html>
8 changes: 8 additions & 0 deletions packages/main/test/pages/DatePicker_test_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<ui5-date-picker id="dp13"></ui5-date-picker>
<ui5-date-picker id="dp16" format-pattern="long"></ui5-date-picker>
<ui5-date-picker value="Invalid value" id="dp20"></ui5-date-picker>
<h3> DatePicker value-state-change event prevented </h3>
<ui5-date-picker id="dpVsChangePrevented" value-state="Error"></ui5-date-picker>
<ui5-button id="b1" design="Default">Set date</ui5-button>


Expand Down Expand Up @@ -102,6 +104,12 @@ <h3>Test accessibleName and accessibleNameRef</h3>
document.getElementById('b1').addEventListener("click", function(e) {
dp16.setAttribute("value", dp16.formatValue(new Date(2018, 11, 11)));
});

const datePickerValueStateChangePrevented = document.getElementById("dpVsChangePrevented");

datePickerValueStateChangePrevented.addEventListener("value-state-change", function(e) {
e.preventDefault();
});
</script>
</body>
</html>
14 changes: 14 additions & 0 deletions packages/main/test/specs/DatePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,4 +1330,18 @@ describe("Date Picker Tests", () => {
let displayedYear = await datepicker.getDisplayedYear(11);
assert.notOk(await displayedYear.hasClass("ui5-yp-item--disabled"), "Year 2025 is not disabled");
});

it("Value state is not changed, when value-state-change is prevented", async () => {
datepicker.id = "#dpVsChangePrevented";

const input = await datepicker.getInput();

const valueState = await input.getProperty("valueState");
await input.click();
await browser.keys("Jan 29, 2019");

await browser.$("#dpVsChangePrevented").shadow$("ui5-input").shadow$("input").click(); // click elsewhere to focusout

assert.strictEqual(await input.getProperty("valueState"), valueState, "value state is not changed");
});
});