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

PropertyRequiredMixin #4272

Merged
merged 9 commits into from
Nov 23, 2023
Merged

PropertyRequiredMixin #4272

merged 9 commits into from
Nov 23, 2023

Conversation

dlockhart
Copy link
Member

@dlockhart dlockhart commented Nov 21, 2023

This is a mixin that can be leveraged whenever a component has a property/attribute that they want to treat as "required" -- something the developer consuming the component must set. This typically comes up on the accessibility side where we want to ensure labels are provided.

I've only implemented this for String properties, because I wasn't sure what it would mean for a Boolean (present or not?) or a Number to be "required". If we have actual use cases, I'm happy to add support for that.

Copy link
Contributor

Thanks for the PR! 🎉

We've deployed an automatic preview for this PR - you can see your changes here:

URL https://live.d2l.dev/prs/BrightspaceUI/core/pr-4272/

Note

The build needs to finish before your changes are deployed.
Changes to the PR will automatically update the instance.

@@ -277,8 +276,6 @@ class InputColor extends FocusMixin(FormElementMixin(LocalizeCoreElement(LitElem

super.updated(changedProperties);

if (changedProperties.has('label') || changedProperties.has('type')) this._validateLabel();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use case forced me to add support for "dependent properties" -- essentially other properties to watch and re-validate when they change.

@@ -128,4 +129,35 @@ describe('d2l-input-color', () => {

});

describe('validation', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of how a component can now test that the validation is set up properly.

mixins/labelled/labelled-mixin.js Outdated Show resolved Hide resolved
mixins/property-required/property-required-mixin.js Outdated Show resolved Hide resolved
.to.throw(TypeError, createDefaultMessage(tag, 'attr1'));
});

it('should work in a subclass/mixin', async() => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a crazy case. When mixins or inheritence are present, the static properties need to be collected by traversing the inheritence tree. Loosly inspired by @eKoopmans' provider stuff.


_initProperties(base) {
if (base === null) return;
this._initProperties(Object.getPrototypeOf(base));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat, I figured there had to be a better/cleaner option than __proto__.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah was going to mention this option to you, as when I was looking up __proto__ the Mozilla docs had all kinds of scary deprecation warnings!

@dlockhart dlockhart marked this pull request as ready for review November 23, 2023 18:22
@dlockhart dlockhart requested a review from a team as a code owner November 23, 2023 18:22
@dlockhart
Copy link
Member Author

This is ready for review. As I mentioned in standup, I'm going to do a follow-up that converts more things to use this.

helpers/error.js Outdated
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After chatting with @dbatiste, we decided that it would be useful to centralize the formatting of errors originating from custom elements. That way we can ensure they're formatted consistently, always include the tag name in the same way and can optionally include the entire composed path.

This error now looks like:

TypeError: : "label" attribute is required. Path: "d2l-demo-page > main > div > slot > d2l-demo-snippet > div > div > div > slot > div > d2l-input-color". (@brightspace-ui/core:PropertyRequiredMixin)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing there isn't actually a double : :... or is there?

Anyway, I think this path will be really helpful to locate the source of some issues. 🎉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha there is not a double :, not sure where that copy-paste mistake came from!

Copy link
Contributor

@dbatiste dbatiste left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@dlockhart dlockhart merged commit c0b9615 into main Nov 23, 2023
5 checks passed
@dlockhart dlockhart deleted the dlockhart/property-required-mixin branch November 23, 2023 21:23
Copy link

🎉 This PR is included in version 2.163.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants