Skip to content

Commit 9cf870a

Browse files
committed
Improve disabled support
1 parent b064ad4 commit 9cf870a

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

packages/react/src/ActionBar/ActionBar.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,18 @@ describe('ActionBar', () => {
3232
const results = await axe.run(container)
3333
expect(results).toHaveNoViolations()
3434
})
35+
36+
it('should not trigger disabled button', () => {
37+
const onClick = jest.fn()
38+
const {getByRole} = HTMLRender(
39+
<ActionBar aria-label="Toolbar">
40+
<ActionBar.IconButton icon={BoldIcon} aria-label="Default" onClick={onClick} disabled></ActionBar.IconButton>
41+
</ActionBar>,
42+
)
43+
44+
const button = getByRole('button')
45+
button.click()
46+
47+
expect(onClick).not.toHaveBeenCalled()
48+
})
3549
})

packages/react/src/ActionBar/ActionBar.tsx

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,39 @@ export const ActionBar: React.FC<React.PropsWithChildren<ActionBarProps>> = prop
252252
)
253253
}
254254

255-
export const ActionBarIconButton = forwardRef((props: ActionBarIconButtonProps, forwardedRef) => {
256-
const backupRef = useRef<HTMLElement>(null)
257-
const ref = (forwardedRef ?? backupRef) as RefObject<HTMLAnchorElement>
258-
const {size, setChildrenWidth} = React.useContext(ActionBarContext)
259-
useIsomorphicLayoutEffect(() => {
260-
const text = props['aria-label'] ? props['aria-label'] : ''
261-
const domRect = (ref as MutableRefObject<HTMLElement>).current.getBoundingClientRect()
262-
setChildrenWidth({text, width: domRect.width})
263-
}, [ref, setChildrenWidth])
264-
return <IconButton aria-disabled={props.disabled} ref={ref} size={size} {...props} variant="invisible" />
265-
})
255+
export const ActionBarIconButton = forwardRef(
256+
({disabled, onClick, ...props}: ActionBarIconButtonProps, forwardedRef) => {
257+
const backupRef = useRef<HTMLElement>(null)
258+
const ref = (forwardedRef ?? backupRef) as RefObject<HTMLAnchorElement>
259+
const {size, setChildrenWidth} = React.useContext(ActionBarContext)
260+
useIsomorphicLayoutEffect(() => {
261+
const text = props['aria-label'] ? props['aria-label'] : ''
262+
const domRect = (ref as MutableRefObject<HTMLElement>).current.getBoundingClientRect()
263+
setChildrenWidth({text, width: domRect.width})
264+
}, [ref, setChildrenWidth])
265+
266+
const clickHandler = useCallback(
267+
(event: React.MouseEvent<HTMLButtonElement>) => {
268+
if (disabled) {
269+
return
270+
}
271+
onClick?.(event)
272+
},
273+
[disabled, onClick],
274+
)
275+
276+
return (
277+
<IconButton
278+
aria-disabled={disabled}
279+
ref={ref}
280+
size={size}
281+
onClick={clickHandler}
282+
{...props}
283+
variant="invisible"
284+
/>
285+
)
286+
},
287+
)
266288

267289
export const VerticalDivider = () => {
268290
const ref = useRef<HTMLDivElement>(null)

0 commit comments

Comments
 (0)