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

prevent harmless error in radio-group when non-radio-button is clicked #1981

Closed
wants to merge 1 commit into from

Conversation

trusktr
Copy link

@trusktr trusktr commented Apr 17, 2024

It is sometimes useful to stick non-radio(-button) elements in radio-group to achieve certain effects. In case the clicked element is not a radio-button or radio, skip the logic (instead of throwing an error reading disabled of null).

Example:

<sl-radio-group>
  <div class="radio-wrapper">
    <sl-radio-button>...</sl-radio-button>
    <sl-copy-button></sl-copy-button>
  </div>
  <div class="radio-wrapper">
    <sl-radio-button>...</sl-radio-button>
    <sl-copy-button></sl-copy-button>
  </div>
  ...
</sl-radio-group>

Actual example screenshot (copy-button is outside of the radio-button so that behaviors of hover effects, tooltips, etc, of the radio-button are not mixed with those of the copy-button (for example we don't want a tooltip from inside the radio-button to also trigger while hovering on the copy-button)):

Screenshot 2024-04-16 at 11 03 03 PM

(its a vertical layout, each rectangle is a radio-button, each one shows a copy-button on hover)

Even with the error, this approach works really well, the sl-radio-group is written in a way that the wrappers don't mess it up. The error is harmless too, everything still works with the error because no logic after the error is necessary (equivalent to returning early and doing nothing).

Copy link

vercel bot commented Apr 17, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
shoelace ❌ Failed (Inspect) Apr 17, 2024 6:05am

const radios = this.getAllRadios();
const oldValue = this.value;

if (target.disabled) {
if (!target || target.disabled) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

good catch! thanks!

@@ -129,11 +129,11 @@ export default class SlRadioGroup extends ShoelaceElement implements ShoelaceFor
}

private handleRadioClick(event: MouseEvent) {
const target = (event.target as HTMLElement).closest<SlRadio | SlRadioButton>('sl-radio, sl-radio-button')!;
const target = event.target.closest<SlRadio | SlRadioButton>('sl-radio, sl-radio-button')!;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like removing the as HTMLElement here broke the TypeScript check. Mind adding it back in and see if CI passes?

@KonnorRogers
Copy link
Collaborator

Closing in favor of #2009 thanks @trusktr !

@claviska claviska closed this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants