Skip to content

Add values to Checkbox Group's value in order of selection #577

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 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-guests-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@crowdstrike/glide-core': minor
---

The values in Checkbox Group's `value` attribute now appear in the order they were selected.
24 changes: 24 additions & 0 deletions src/checkbox-group.test.interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fixture,
html,
} from '@open-wc/testing';
import { click } from './library/mouse.js';
import GlideCoreCheckboxGroup from './checkbox-group.js';

GlideCoreCheckboxGroup.shadowRootOptions.mode = 'open';
Expand Down Expand Up @@ -111,3 +112,26 @@ it('enables disabled Checkboxes when `value` is set programmatically', async ()
const checkbox = component.querySelector('glide-core-checkbox');
expect(checkbox?.disabled).to.be.false;
});

it('adds values to `value` in the order they were selected or deselected', async () => {
const component = await fixture<GlideCoreCheckboxGroup>(
html`<glide-core-checkbox-group label="Checkbox Group">
<glide-core-checkbox label="One" value="one"></glide-core-checkbox>
<glide-core-checkbox label="Two" value="two"></glide-core-checkbox>
<glide-core-checkbox label="Three" value="three"></glide-core-checkbox>
</glide-core-checkbox-group>`,
);

const checkboxes = component.querySelectorAll('glide-core-checkbox');

await click(checkboxes[1]);
await click(checkboxes[2]);
await click(checkboxes[0]);
expect(component.value).to.deep.equal(['two', 'three', 'one']);

await click(checkboxes[2]);
expect(component.value).to.deep.equal(['two', 'one']);

await click(checkboxes[2]);
expect(component.value).to.deep.equal(['two', 'one', 'three']);
});
27 changes: 20 additions & 7 deletions src/checkbox-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,26 @@ export default class GlideCoreCheckboxGroup extends LitElement {
}
}

#onCheckboxChange() {
this.value = this.#checkboxes
.filter(({ checked, disabled }) => checked && !disabled)
.map(({ value }) => value)
// Disabled because simply filtering by `Boolean` doesn't narrow the type.
// eslint-disable-next-line unicorn/prefer-native-coercion-functions
.filter((value): value is string => Boolean(value));
#onCheckboxChange(event: Event) {
if (
event.target instanceof GlideCoreCheckbox &&
event.target.checked &&
event.target.value
) {
this.value = [...this.value, event.target.value];
} else if (
event.target instanceof GlideCoreCheckbox &&
!event.target.checked
) {
this.value = this.value.filter((value) => {
return (
// No idea why TypeScript thinks `event.target` is possibly `null`
// when filtering given it's narrowed out above.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you may be running into https://stackoverflow.com/a/79173023

Copy link
Collaborator Author

@clintcs clintcs Jan 3, 2025

Choose a reason for hiding this comment

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

Aha. I might just delete the comment. Any objections?

Copy link
Collaborator

Choose a reason for hiding this comment

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

nope!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ad80886

event.target instanceof GlideCoreCheckbox &&
value !== event.target.value
);
});
}
}

#onCheckboxesValueChange(event: CustomEvent<{ new: string; old: string }>) {
Expand Down
Loading