Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

[terra-button] Create selectable button #3940

Merged
merged 9 commits into from
Oct 23, 2023
Merged

[terra-button] Create selectable button #3940

merged 9 commits into from
Oct 23, 2023

Conversation

sugan2416
Copy link
Collaborator

@sugan2416 sugan2416 commented Oct 16, 2023

Summary

What was changed:

Add private prop isSelectable to allow terra-button to function as a toggleable button.

Why it was changed:

These changes were requested by Mpages team to have a sub-variant of terra-button which could be toggleable.

Testing

This change was tested using:

  • WDIO
  • Jest
  • Visual testing (please attach a screenshot or recording)
  • Other (please describe below)
  • No tests are needed

Reviews

In addition to engineering reviews, this PR needs:

  • UX review
  • Accessibility review
  • Functional review

Additional Details

This PR resolves:

UXPLATFORM-9749


Thank you for contributing to Terra.
@cerner/terra

Unselected
Screenshot 2023-10-16 at 6 40 42 PM

Selected
Screenshot 2023-10-16 at 6 40 49 PM

@sugan2416 sugan2416 self-assigned this Oct 16, 2023
@sugan2416 sugan2416 marked this pull request as ready for review October 18, 2023 07:14
@sugan2416 sugan2416 requested a review from a team as a code owner October 18, 2023 07:14
* @private
* Whether or not the button can be selected (toggleable button).
*/
isSelectable: PropTypes.bool,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add a console.warn when this prop s used ? something like 'this prop will be deprecated in future and do not recommend it for use'.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added in 3ea7af9

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed in 44abd39 as prop was changed to custom.

@@ -326,6 +350,7 @@ class Button extends React.Component {
onFocus={this.handleFocus}
href={href}
ref={refCallback}
aria-pressed={isSelectable ? this.state.isSelected : undefined}
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the SR reading out when aria-pressed is set to True ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It says "Toggle button pressed, <button_label>"

@@ -46,7 +46,6 @@ exports[`correctly applies the theme context className 1`] = `
>
<button
aria-disabled={false}
aria-pressed={false}
Copy link
Contributor

Choose a reason for hiding this comment

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

why are we removing aria-pressed from button group ..?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was getting removed due to the condition in button. Updated button component to add aria-pressed only when "isSelected" is used so that ButtonGroup would continue to receive aria-pressed as before.

aria-pressed is now back in 54c4c36

@@ -154,6 +163,8 @@ class Button extends React.Component {
this.shouldShowFocus = true;
}

this.handleOnChange(event);
Copy link
Contributor

Choose a reason for hiding this comment

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

let's invoke this only for selectable button

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

check was added in within handleOnChange (54c4c36#diff-39f6c87ccf92298263c905de24e815f96ac1c87e46309e7d88ac4513f8bb7193R152).

Now moved before calling handler in 54c4c36

@@ -174,7 +185,7 @@ class Button extends React.Component {

// Add focus styles for keyboard navigation
if (event.nativeEvent.keyCode === KeyCode.KEY_SPACE || event.nativeEvent.keyCode === KeyCode.KEY_RETURN) {
this.setState({ focused: true });
this.setState(prevState => ({ focused: true, isSelected: !prevState.isSelected }));
Copy link
Contributor

Choose a reason for hiding this comment

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

I would move this out to different block and update only if isSelectable is true to prevent unintended re-render of component

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added check in 54c4c36

@@ -212,6 +223,7 @@ class Button extends React.Component {
if (this.props.onMouseDown) {
this.props.onMouseDown(event);
}
this.setState(prevState => ({ isSelected: !prevState.isSelected }));
Copy link
Contributor

Choose a reason for hiding this comment

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

same here we need to invoke only for selectable button

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added check in 54c4c36

@@ -255,6 +269,7 @@ class Button extends React.Component {
{ compact: isCompact },
{ 'is-active': this.state.active && !isDisabled },
{ 'is-focused': this.state.focused && !isDisabled },
{ 'is-selected': customProps.isSelectable && this.state.isSelected && !isDisabled },
Copy link
Contributor

@supreethmr supreethmr Oct 19, 2023

Choose a reason for hiding this comment

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

let's spread out the isSelectable on top and use it instead of referring it through customProps chaining on multiple place for better readability

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would cause unlisted prop lint err. Instead now moved to the top like this: 54c4c36#diff-39f6c87ccf92298263c905de24e815f96ac1c87e46309e7d88ac4513f8bb7193R132

This way handlers can use it outside of render.

Comment on lines +89 to +94
&.is-selected {
background-color: var(--terra-button-selected-background-color, #52585b);
border-color: var(--terra-button-selected-border-color, #52585b);
box-shadow: var(--terra-button-selected-box-shadow);
color: var(--terra-button-selected-color, #fff);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe is-Selected is the only class we would need rest other styles should be same as default button ( which might be already added on _mixin.scss of button package.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated along with renaming in cfcc950

@cerner cerner deleted a comment from github-actions bot Oct 20, 2023
@sugan2416
Copy link
Collaborator Author

sugan2416 commented Oct 20, 2023

  • Unlisted props seem to cause lint err in ComponenetDidMount as well.
  • Removed on change handler
  • Added wdio test for selected.
  • Updated and renamed style var

@supreethmr supreethmr merged commit 6b6dbdf into main Oct 23, 2023
21 checks passed
@supreethmr supreethmr deleted the isselected-button branch October 23, 2023 11:57
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants