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

Fix tag-list-item clearable functionality #3772

Merged
merged 8 commits into from
Jun 27, 2023

Conversation

bearfriend
Copy link
Contributor

@bearfriend bearfriend commented Jun 21, 2023

DE53055: Fixer > User Tag List Items > Fix clearable functionality

Many consumers of d2l-tag-list-item and TagListItemMixin use custom (usually data) attributes on tags to identify them. This PR adds a key property to the tag-list-item-mixin to act as a unique identifier for each tag, and includes it in the d2l-tag-list-item-clear event (though I'm not sure how valuable it really is there since e.target.key is already available).

Sometimes there are additional attributes added to denote relationships to other objects/entities. This seems like a reasonable and appropriate practice to me that does not require facilitation through the component/mixin.

Additionally, and probably more importantly, I've added a displayText option to _renderTag which allows consumers to give the tag a simple label for use within various langterms and screenreader announcements. This is useful when the contents of the tag are not a simple string.

The only place that uses _renderTag with non-string contents already doesn't announce or localize properly, so this should be updated to pass displayText, but it can be done at any time:
BrightspaceHypermediaComponents/users

Changing the event detail is technically a breaking change, but nothing is actually using the existing detail.value, so these uses of d2l-tag-list-item-clear can be updated to use key but are not required:
Brightspace/insights-adoption-dashboard
Brightspace/d2l-outcomes
Brightspace/cds-reflection-tool

🤷 Weird things I kinda wish I didn't see:
Brightspace/nova
Brightspace/nova
Brightspace/cds-custom-pacing-tool-ui

@bearfriend bearfriend requested a review from a team as a code owner June 21, 2023 23:41
@github-actions
Copy link
Contributor

Thanks for the PR! 🎉

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

URL https://pr-3772-brightspace-ui-core.d2l.dev/

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

components/tag-list/tag-list-item-mixin.js Outdated Show resolved Hide resolved
components/tag-list/test/tag-list.test.js Show resolved Hide resolved

/** Dispatched when a user selects to delete an individual tag list item. The consumer must handle the actual element deletion and focus behaviour if there are no remaining list items. */
this.dispatchEvent(new CustomEvent(
'd2l-tag-list-item-clear',
{ bubbles: true, composed: true, detail: { value: this.text } }
{ bubbles: true, composed: true, detail: { key: this.key } }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there's much value in this.

Suggested change
{ bubbles: true, composed: true, detail: { key: this.key } }
{ bubbles: true, composed: true }

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I agree, but do you think it'd help people "do the right thing"? Encourages using the event detail to get the data from the data array, rather than hooking up an anonymous function to each item passing in data like a few places are doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I guess it could, and it doesn't hurt any to leave it here.

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 not sure it's going to prevent that. If they're not inclined to use e.target.key then I suspect they won't use e.detail.key either. I usually opt for not duplicating the data that just as easily accessed, but as you said, it doesn't really hurt.

Copy link
Contributor

Choose a reason for hiding this comment

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

To close the loop on this - per conversation offline, it looks like there are a number of other components that do provide the key on the event detail as a matter of convenience. I think we decided to follow suit, yeah?

components/tag-list/tag-list-item-mixin.js Show resolved Hide resolved
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.

This is looking good to be. @svanherk , do you have anything further for this PR?

@@ -71,8 +71,8 @@ <h2>Clearable Tag List</h2>
import { getNextFocusable } from '../../../helpers/focus.js';

document.addEventListener('d2l-tag-list-item-clear', (e) => {
console.log(`d2l-tag-list-item-clear event dispatched. Value: ${e.target.text}`);
Copy link
Contributor

@svanherk svanherk Jun 27, 2023

Choose a reason for hiding this comment

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

Should we add keys to the demo, as a good example? And then we can add it here alongside the value, to show how to get both

* Acts as a unique identifier for the tag
* @type {string}
*/
key: { type: String },
Copy link
Contributor

@svanherk svanherk Jun 27, 2023

Choose a reason for hiding this comment

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

Do we want to mark this as "Required" in the docs? 🤔 It doesn't really break anything if you don't have it, but the event does expect it.

@@ -91,6 +91,24 @@ describe('d2l-tag-list-item', () => {
});
});

describe('display text', () => {
it('should default to tagContent', async() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
it('should default to tagContent', async() => {
it('should use text for tag-list-item', async() => {

Looks like this isn't really accurate anymore, maybe something like this?

@bearfriend bearfriend force-pushed the dgleckler/tag-list-item-clear branch 2 times, most recently from d83c7d4 to 6a4f913 Compare June 27, 2023 18:27
@github-actions
Copy link
Contributor

Could not generate new goldens - your code changes will update golden files that you do not have the latest version of. Please rebase or merge main into your branch.

@github-actions
Copy link
Contributor

Visual diff tests failed - pull request #3789 has been opened with the updated goldens.

_renderTag(tagContent, options) {
if (!options) options = {};
_renderTag(tagContent, options = {}) {
if (options.labelText?.constructor !== String) throw new TypeError('options.labelText must be a string');
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I guess we'll want to have the users PR ready to go to merge right after this? Or the user tag items will start throwing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yikes! Yep.

@bearfriend bearfriend merged commit 6358425 into main Jun 27, 2023
@bearfriend bearfriend deleted the dgleckler/tag-list-item-clear branch June 27, 2023 21:52
@ghost
Copy link

ghost commented Jun 27, 2023

🎉 This PR is included in version 2.131.7 🎉

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