Files
headlessui/packages/@headlessui-react/src/utils/bugs.ts
T
Robin Malfait 5fb605205d apply disabled fix when inside a disabled fieldset (#202)
And if we are in a disabled fieldset, double check that we are not in
the first legend. Because in that case we are visually outside of the
fieldset and according to the spec those elements should **not** be
considered disabled.

Fixes: #194
2021-01-22 15:41:09 +01:00

31 lines
975 B
TypeScript

// See: https://github.com/facebook/react/issues/7711
// See: https://github.com/facebook/react/pull/20612
// See: https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled (2.)
export function isDisabledReactIssue7711(element: Element): boolean {
let parent = element.parentElement
let legend = null
while (parent && !(parent instanceof HTMLFieldSetElement)) {
if (parent instanceof HTMLLegendElement) legend = parent
parent = parent.parentElement
}
let isParentDisabled = parent?.getAttribute('disabled') === '' ?? false
if (isParentDisabled && isFirstLegend(legend)) return false
return isParentDisabled
}
function isFirstLegend(element: HTMLLegendElement | null): boolean {
if (!element) return false
let previous = element.previousElementSibling
while (previous !== null) {
if (previous instanceof HTMLLegendElement) return false
previous = previous.previousElementSibling
}
return true
}