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

UI: Remove Checkbox bug workaround #3283

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/core-react",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/core-react"
}
33 changes: 6 additions & 27 deletions ui/core-react/src/core-react/tree/Node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export interface TreeNodeProps extends CommonProps {
* @public
*/
export class TreeNode extends React.Component<TreeNodeProps> {
private checkboxWrapperRef = React.createRef<HTMLDivElement>();

constructor(props: TreeNodeProps) {
super(props);
}
Expand Down Expand Up @@ -117,13 +115,11 @@ export class TreeNode extends React.Component<TreeNodeProps> {
checkbox = this.props.renderOverrides.renderCheckbox(props);
} else {
checkbox = (
<div ref={this.checkboxWrapperRef}>
<Checkbox
{...props}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => this._onCheckboxChange(e.target.checked)}
data-testid={this.createSubComponentTestId("checkbox")}
/>
</div>
<Checkbox
{...props}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => this._onCheckboxChange(e.target.checked)}
data-testid={this.createSubComponentTestId("checkbox")}
/>
);
}
}
Expand Down Expand Up @@ -192,23 +188,6 @@ export class TreeNode extends React.Component<TreeNodeProps> {

private _onClick = (e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation();

// ITwinUI-react has a bug that prevents us from stopping checkbox click event propagation to the parent element
if (!this.checkboxWrapperRef.current
|| (e.target instanceof Element && !isDescendant(this.checkboxWrapperRef.current, e.target))) {
this.props.onClick?.(e);
}
this.props.onClick?.(e);
};
}

function isDescendant(ancestor: Element, descendant: Element): boolean {
while (descendant.parentElement !== null) {
if (descendant === ancestor) {
return true;
}

descendant = descendant.parentElement;
}

return false;
}
2 changes: 1 addition & 1 deletion ui/core-react/src/test/tree/Node.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe("<Node />", () => {
it("does not call node onClick callback when checkbox is clicked", () => {
const handleOnClick = sinon.fake();
const { getByRole } = render(<Node label="a" level={0} onClick={handleOnClick} checkboxProps={{}} />);
fireEvent.click(getByRole("checkbox").parentElement!);
fireEvent.click(getByRole("checkbox"));
expect(handleOnClick).to.not.have.been.called;
});

Expand Down