Skip to content

fix: issue with tabs and accordion for react 19 #4030

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

Merged
merged 10 commits into from
Apr 14, 2025
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
onMount,
onUpdate,
Show,
Slot,
useDefaultProps,
useMetadata,
useRef,
useStore
useStore,
useTarget
} from '@builder.io/mitosis';
import { DBAccordionItemProps, DBAccordionItemState } from './model';
import { cls, getBooleanAsString, uuid } from '../../utils';
Expand All @@ -22,6 +24,19 @@ export default function DBAccordionItem(props: DBAccordionItemProps) {
const state = useStore<DBAccordionItemState>({
_id: DEFAULT_ID,
_open: false,
_name: undefined,
initialized: false,
handleNameAttribute: () => {
if (_ref) {
const setAttribute = _ref.setAttribute;
_ref.setAttribute = (attribute: string, value: string) => {
setAttribute.call(_ref, attribute, value);
if (attribute === 'name') {
state._name = value;
}
};
}
},
toggle: (event: ClickEvent<HTMLElement>) => {
// We need this for react https://github.com/facebook/react/issues/15486#issuecomment-488028431
event?.preventDefault();
Expand All @@ -38,14 +53,31 @@ export default function DBAccordionItem(props: DBAccordionItemProps) {
if (props.defaultOpen) {
state._open = props.defaultOpen;
}

state.initialized = true;
});

onUpdate(() => {
if (_ref && state.initialized) {
useTarget({ react: () => state.handleNameAttribute() });
}
}, [_ref, state.initialized]);

onUpdate(() => {
if (props.name) {
state._name = props.name;
}
}, [props.name]);

// jscpd:ignore-end

return (
<li id={state._id} class={cls('db-accordion-item', props.className)}>
<details
aria-disabled={getBooleanAsString(props.disabled)}
ref={_ref}
/* @ts-expect-error This is a new api for details */
name={state._name}
open={state._open}>
<summary onClick={(event) => state.toggle(event)}>
<Show when={props.headlinePlain}>
Expand Down
10 changes: 8 additions & 2 deletions packages/components/src/components/accordion-item/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
GlobalProps,
GlobalState,
InitializedState,
NameProps,
NameState,
TextProps,
ToggleEventProps,
ToggleEventState
Expand All @@ -27,12 +30,15 @@ export type DBAccordionItemDefaultProps = {

export type DBAccordionItemProps = DBAccordionItemDefaultProps &
GlobalProps &
ToggleEventProps;
ToggleEventProps &
NameProps;

export type DBAccordionItemDefaultState = {
_open: boolean;
};

export type DBAccordionItemState = DBAccordionItemDefaultState &
GlobalState &
ToggleEventState<HTMLElement>;
ToggleEventState<HTMLElement> &
InitializedState &
NameState;
8 changes: 6 additions & 2 deletions packages/components/src/components/tab-item/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
IconAfterProps,
IconProps,
InitializedState,
NameProps,
NameState,
ShowIconProps
} from '../../shared/model';

Expand Down Expand Up @@ -38,7 +40,8 @@ export type DBTabItemProps = GlobalProps &
ActiveProps &
AriaControlsProps &
ChangeEventProps<HTMLInputElement> &
ShowIconProps;
ShowIconProps &
NameProps;

export type DBTabItemDefaultState = {
_selected: boolean;
Expand All @@ -47,4 +50,5 @@ export type DBTabItemDefaultState = {
export type DBTabItemState = DBTabItemDefaultState &
GlobalState &
ChangeEventState<HTMLInputElement> &
InitializedState;
InitializedState &
NameState;
29 changes: 26 additions & 3 deletions packages/components/src/components/tab-item/tab-item.lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ export default function DBTabItem(props: DBTabItemProps) {
const _ref = useRef<HTMLInputElement | null>(null);
// jscpd:ignore-start
const state = useStore<DBTabItemState>({
initialized: false,
_selected: false,
_name: undefined,
initialized: false,
handleNameAttribute: () => {
if (_ref) {
const setAttribute = _ref.setAttribute;
_ref.setAttribute = (attribute: string, value: string) => {
setAttribute.call(_ref, attribute, value);
if (attribute === 'name') {
state._name = value;
}
};
}
},
handleChange: (event: any) => {
if (props.onChange) {
props.onChange(event);
Expand Down Expand Up @@ -63,12 +75,22 @@ export default function DBTabItem(props: DBTabItemProps) {
// jscpd:ignore-end

onUpdate(() => {
if (props.active && state.initialized && _ref) {
_ref.click();
if (state.initialized && _ref) {
if (props.active) {
_ref.click();
}

useTarget({ react: () => state.handleNameAttribute() });
state.initialized = false;
}
}, [_ref, state.initialized]);

onUpdate(() => {
if (props.name) {
state._name = props.name;
}
}, [props.name]);

return (
<li class={cls('db-tab-item', props.className)} role="none">
<label
Expand All @@ -86,6 +108,7 @@ export default function DBTabItem(props: DBTabItemProps) {
ref={_ref}
type="radio"
role="tab"
name={state._name}
id={props.id}
onInput={(event: any) => state.handleChange(event)}
/>
Expand Down
12 changes: 12 additions & 0 deletions packages/components/src/shared/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ export type PopoverState = {
handleAutoPlacement: () => void;
};

export type NameProps = {
/**
* The name attribute gives the name of the element to group it.
*/
name?: string;
};

export type NameState = {
_name?: string;
handleNameAttribute: () => void;
};

export type ContentSlotProps = {
/**
* Default slot which is used to pass in additional content.
Expand Down
Loading