File tree Expand file tree Collapse file tree 3 files changed +35
-1
lines changed
packages/@headlessui-react/src Expand file tree Collapse file tree 3 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import { useId } from '../../hooks/use-id'
12
12
import { Keys } from '../keyboard'
13
13
import { Focus , calculateActiveIndex } from '../../utils/calculate-active-index'
14
14
import { resolvePropValue } from '../../utils/resolve-prop-value'
15
+ import { isDisabledReactIssue7711 } from '../../utils/bugs'
15
16
16
17
enum MenuStates {
17
18
Open ,
@@ -413,7 +414,8 @@ function Item<TTag extends React.ElementType = typeof DEFAULT_ITEM_TAG>(
413
414
} , [ bag , id ] )
414
415
415
416
const handleClick = React . useCallback (
416
- ( event : { preventDefault : Function } ) => {
417
+ ( event : React . MouseEvent ) => {
418
+ if ( isDisabledReactIssue7711 ( event . currentTarget ) ) return event . preventDefault ( )
417
419
if ( disabled ) return event . preventDefault ( )
418
420
dispatch ( { type : ActionTypes . CloseMenu } )
419
421
disposables ( ) . nextFrame ( ( ) => state . buttonRef . current ?. focus ( { preventScroll : true } ) )
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { render } from '../../utils/render'
5
5
import { useId } from '../../hooks/use-id'
6
6
import { Keys } from '../keyboard'
7
7
import { resolvePropValue } from '../../utils/resolve-prop-value'
8
+ import { isDisabledReactIssue7711 } from '../../utils/bugs'
8
9
9
10
type StateDefinition = {
10
11
switch : HTMLButtonElement | null
@@ -84,6 +85,7 @@ export function Switch<TTag extends React.ElementType = typeof DEFAULT_SWITCH_TA
84
85
const toggle = React . useCallback ( ( ) => onChange ( ! checked ) , [ onChange , checked ] )
85
86
const handleClick = React . useCallback (
86
87
( event : React . MouseEvent ) => {
88
+ if ( isDisabledReactIssue7711 ( event . currentTarget ) ) return event . preventDefault ( )
87
89
event . preventDefault ( )
88
90
toggle ( )
89
91
} ,
Original file line number Diff line number Diff line change
1
+ // See: https://github.com/facebook/react/issues/7711
2
+ // See: https://github.com/facebook/react/pull/20612
3
+ // See: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled (2.)
4
+ export function isDisabledReactIssue7711 ( element : Element ) : boolean {
5
+ let parent = element . parentElement
6
+ let legend = null
7
+
8
+ while ( parent && ! ( parent instanceof HTMLFieldSetElement ) ) {
9
+ if ( parent instanceof HTMLLegendElement ) legend = parent
10
+ parent = parent . parentElement
11
+ }
12
+
13
+ let isParentDisabled = parent ?. getAttribute ( 'disabled' ) === '' ?? false
14
+ if ( isParentDisabled && isFirstLegend ( legend ) ) return false
15
+
16
+ return isParentDisabled
17
+ }
18
+
19
+ function isFirstLegend ( element : HTMLLegendElement | null ) : boolean {
20
+ if ( ! element ) return false
21
+
22
+ let previous = element . previousElementSibling
23
+
24
+ while ( previous !== null ) {
25
+ if ( previous instanceof HTMLLegendElement ) return false
26
+ previous = previous . previousElementSibling
27
+ }
28
+
29
+ return true
30
+ }
You can’t perform that action at this time.
0 commit comments