Skip to content
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
15 changes: 15 additions & 0 deletions bulma-ui/src/columns/Columns.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,18 @@ export const MultilineCenteredColumns: StoryObj = {
</>
),
};

export const CompoundUsage: StoryObj = {
render: () => (
<Columns>
<Columns.Column>
<Notification color="primary">
First column via dot notation
</Notification>
</Columns.Column>
<Columns.Column>
<Notification color="info">Second column via dot notation</Notification>
</Columns.Column>
</Columns>
),
};
10 changes: 9 additions & 1 deletion bulma-ui/src/columns/Columns.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import { classNames, usePrefixedClassNames } from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
useBulmaClasses,
BulmaClassesProps,
validColors,
} from '../helpers/useBulmaClasses';
import type { BulmaGapValue } from '../grid/Grid';
import { Column } from './Column';

/**
* Possible values for the Bulma columns gap size.
Expand Down Expand Up @@ -87,7 +89,7 @@ export interface ColumnsProps
* @returns {JSX.Element} The rendered columns container.
* @see {@link https://bulma.io/documentation/columns/ | Bulma Columns documentation}
*/
export const Columns: React.FC<ColumnsProps> = ({
const ColumnsComponent: React.FC<ColumnsProps> = ({
className,
textColor,
color: _fieldColor,
Expand Down Expand Up @@ -162,3 +164,9 @@ export const Columns: React.FC<ColumnsProps> = ({
</div>
);
};

export const Columns = withSubComponents(
ColumnsComponent,
{ Column },
'Columns'
);
18 changes: 18 additions & 0 deletions bulma-ui/src/columns/__tests__/Columns.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from '@testing-library/react';
import { Columns } from '../Columns';
import { Column } from '../Column';
import { ConfigProvider } from '../../helpers/Config';

describe('Columns', () => {
Expand Down Expand Up @@ -227,3 +228,20 @@ describe('Columns', () => {
});
});
});

describe('Compound components', () => {
test('Columns.Column is the Column component', () => {
expect(Columns.Column).toBe(Column);
});

test('renders columns through the dot path', () => {
const { container } = render(
<Columns>
<Columns.Column>First</Columns.Column>
<Columns.Column>Second</Columns.Column>
</Columns>
);
expect(container.querySelector('.columns')).toBeInTheDocument();
expect(container.querySelectorAll('.column')).toHaveLength(2);
});
});
10 changes: 7 additions & 3 deletions bulma-ui/src/components/Avatars.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { classNames, usePrefixedClassNames } from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import { useBulmaClasses, BulmaClassesProps } from '../helpers/useBulmaClasses';
import { Avatar, AvatarProps } from './Avatar';

Expand Down Expand Up @@ -73,7 +74,7 @@ function flattenChildren(
* {members.map(m => <Avatar key={m.id} src={m.photo} name={m.name} />)}
* </Avatars>
*/
export const Avatars: React.FC<AvatarsProps> & { Avatar: typeof Avatar } = ({
const AvatarsComponent: React.FC<AvatarsProps> = ({
className,
max,
size,
Expand Down Expand Up @@ -153,7 +154,10 @@ export const Avatars: React.FC<AvatarsProps> & { Avatar: typeof Avatar } = ({
);
};

Avatars.displayName = 'Avatars';
Avatars.Avatar = Avatar;
export const Avatars = withSubComponents(
AvatarsComponent,
{ Avatar },
'Avatars'
);

export default Avatars;
50 changes: 19 additions & 31 deletions bulma-ui/src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
usePrefixedClassNames,
prefixedClassNames,
} from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
useBulmaClasses,
BulmaClassesProps,
Expand Down Expand Up @@ -463,37 +464,24 @@ const CardFooterItem: React.FC<CardFooterItemProps> = ({
</span>
);

/** Card component type with Header, Image, Content, Footer, and FooterItem sub-components. */
type CardWithCompounds = typeof CardComponent & {
Header: typeof CardHeader & {
Title: typeof CardHeaderTitle;
Icon: typeof CardHeaderIcon;
};
Image: typeof CardImage;
Content: typeof CardContent;
Footer: typeof CardFooter;
FooterItem: typeof CardFooterItem;
};

// Cast Card to the compound type and assign compound components
const CardWithSubComponents = CardComponent as CardWithCompounds;

// Create CardHeader with nested Title and Icon components
const CardHeaderWithTitle = CardHeader as typeof CardHeader & {
Title: typeof CardHeaderTitle;
Icon: typeof CardHeaderIcon;
};
CardHeaderWithTitle.Title = CardHeaderTitle;
CardHeaderWithTitle.Icon = CardHeaderIcon;

CardWithSubComponents.Header = CardHeaderWithTitle;
CardWithSubComponents.Image = CardImage;
CardWithSubComponents.Content = CardContent;
CardWithSubComponents.Footer = CardFooter;
CardWithSubComponents.FooterItem = CardFooterItem;

// Export the compound component
export { CardWithSubComponents as Card };
// Attach nested Title and Icon components to CardHeader
const CardHeaderCompound = withSubComponents(CardHeader, {
Title: CardHeaderTitle,
Icon: CardHeaderIcon,
});

/** Card component with Header, Image, Content, Footer, and FooterItem sub-components. */
export const Card = withSubComponents(
CardComponent,
{
Header: CardHeaderCompound,
Image: CardImage,
Content: CardContent,
Footer: CardFooter,
FooterItem: CardFooterItem,
},
'Card'
);

/** Internal test-only exports. Not part of the public API. */
export const __test_exports__ = { renderFooter };
13 changes: 9 additions & 4 deletions bulma-ui/src/components/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useRef, useEffect } from 'react';
import { classNames, usePrefixedClassNames } from '../helpers/classNames';
import { useBulmaClasses, BulmaClassesProps } from '../helpers/useBulmaClasses';
import { withSubComponents } from '../helpers/withSubComponents';

/**
* Checks if code is running in a browser environment.
Expand Down Expand Up @@ -242,9 +243,13 @@ export const DropdownDivider: React.FC = () => (
);

/** Bulma Dropdown component with Item and Divider sub-components. */
export const Dropdown = Object.assign(DropdownComponent, {
Item: DropdownItem,
Divider: DropdownDivider,
});
export const Dropdown = withSubComponents(
DropdownComponent,
{
Item: DropdownItem,
Divider: DropdownDivider,
},
'Dropdown'
);

export default Dropdown;
15 changes: 10 additions & 5 deletions bulma-ui/src/components/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { createContext, useContext } from 'react';
import { classNames, usePrefixedClassNames } from '../helpers/classNames';
import { useBulmaClasses, BulmaClassesProps } from '../helpers/useBulmaClasses';
import { withSubComponents } from '../helpers/withSubComponents';

// Context to track MenuList nesting level
const MenuListLevelContext = createContext(0);
Expand Down Expand Up @@ -216,10 +217,14 @@ export const MenuItem: React.FC<MenuItemProps> = ({
};

// Attach static subcomponents
export const Menu = Object.assign(MenuComponent, {
Label: MenuLabel,
List: MenuList,
Item: MenuItem,
});
export const Menu = withSubComponents(
MenuComponent,
{
Label: MenuLabel,
List: MenuList,
Item: MenuItem,
},
'Menu'
);

export default Menu;
25 changes: 11 additions & 14 deletions bulma-ui/src/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
usePrefixedClassNames,
prefixedClassNames,
} from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
useBulmaClasses,
BulmaClassesProps,
Expand Down Expand Up @@ -166,18 +167,14 @@ const MessageBody: React.FC<MessageBodyProps> = ({
</div>
);

// Create a type that extends the Message component with compound components
type MessageWithCompounds = typeof MessageComponent & {
Header: typeof MessageHeader;
Body: typeof MessageBody;
};

// Cast Message to the compound type and assign compound components
const MessageWithSubComponents = MessageComponent as MessageWithCompounds;
MessageWithSubComponents.Header = MessageHeader;
MessageWithSubComponents.Body = MessageBody;

// Export the compound component
export { MessageWithSubComponents as Message };
// Attach compound components
export const Message = withSubComponents(
MessageComponent,
{
Header: MessageHeader,
Body: MessageBody,
},
'Message'
);

export default MessageWithSubComponents;
export default Message;
44 changes: 22 additions & 22 deletions bulma-ui/src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
usePrefixedClassNames,
prefixedClassNames,
} from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
useBulmaClasses,
BulmaClassesProps,
Expand Down Expand Up @@ -227,20 +228,20 @@ const ModalCardFoot: React.FC<ModalCardFootProps> = ({
* @param {ModalCardProps} props - Component props.
* @returns {JSX.Element} Modal card element.
*/
const ModalCard: React.FC<ModalCardProps> & {
Head: typeof ModalCardHead;
Title: typeof ModalCardTitle;
Body: typeof ModalCardBody;
Foot: typeof ModalCardFoot;
} = ({ className, ...props }) => {
const ModalCardComponent: React.FC<ModalCardProps> = ({
className,
...props
}) => {
const classes = classNames(usePrefixedClassNames('modal-card'), className);
return <div className={classes} {...props} />;
};

ModalCard.Head = ModalCardHead;
ModalCard.Title = ModalCardTitle;
ModalCard.Body = ModalCardBody;
ModalCard.Foot = ModalCardFoot;
const ModalCard = withSubComponents(ModalCardComponent, {
Head: ModalCardHead,
Title: ModalCardTitle,
Body: ModalCardBody,
Foot: ModalCardFoot,
});

/**
* Modal.Close - Renders modal close button with two variant styles.
Expand Down Expand Up @@ -303,12 +304,7 @@ const ModalClose: React.FC<ModalCloseProps> = ({
*
* @see {@link https://bulma.io/documentation/components/modal/ | Bulma Modal documentation}
*/
const ModalRoot: React.FC<ModalProps> & {
Background: typeof ModalBackground;
Content: typeof ModalContent;
Card: typeof ModalCard;
Close: typeof ModalClose;
} = ({
const ModalRoot: React.FC<ModalProps> = ({
active,
isActive,
onClose,
Expand Down Expand Up @@ -429,10 +425,14 @@ const ModalRoot: React.FC<ModalProps> & {
);
};

ModalRoot.Background = ModalBackground;
ModalRoot.Content = ModalContent;
ModalRoot.Card = ModalCard;
ModalRoot.Close = ModalClose;

export const Modal = ModalRoot;
export const Modal = withSubComponents(
ModalRoot,
{
Background: ModalBackground,
Content: ModalContent,
Card: ModalCard,
Close: ModalClose,
},
'Modal'
);
export default Modal;
40 changes: 18 additions & 22 deletions bulma-ui/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { classNames, usePrefixedClassNames } from '../helpers/classNames';
import { withSubComponents } from '../helpers/withSubComponents';
import {
useBulmaClasses,
BulmaClassesProps,
Expand Down Expand Up @@ -48,18 +49,7 @@ export interface NavbarProps
* @returns {JSX.Element} The rendered navbar.
* @see {@link https://bulma.io/documentation/components/navbar/ | Bulma Navbar documentation}
*/
export const Navbar: React.FC<NavbarProps> & {
Brand: typeof NavbarBrand;
Item: typeof NavbarItem;
Link: typeof NavbarLink;
Burger: typeof NavbarBurger;
Menu: typeof NavbarMenu;
Start: typeof NavbarStart;
End: typeof NavbarEnd;
Dropdown: typeof NavbarDropdown;
DropdownMenu: typeof NavbarDropdownMenu;
Divider: typeof NavbarDivider;
} = ({
const NavbarComponent: React.FC<NavbarProps> = ({
className,
textColor,
bgColor,
Expand Down Expand Up @@ -573,15 +563,21 @@ export const NavbarDivider: React.FC<
);

// Attach subcomponents
Navbar.Brand = NavbarBrand;
Navbar.Item = NavbarItem;
Navbar.Link = NavbarLink;
Navbar.Burger = NavbarBurger;
Navbar.Menu = NavbarMenu;
Navbar.Start = NavbarStart;
Navbar.End = NavbarEnd;
Navbar.Dropdown = NavbarDropdown;
Navbar.DropdownMenu = NavbarDropdownMenu;
Navbar.Divider = NavbarDivider;
export const Navbar = withSubComponents(
NavbarComponent,
{
Brand: NavbarBrand,
Item: NavbarItem,
Link: NavbarLink,
Burger: NavbarBurger,
Menu: NavbarMenu,
Start: NavbarStart,
End: NavbarEnd,
Dropdown: NavbarDropdown,
DropdownMenu: NavbarDropdownMenu,
Divider: NavbarDivider,
},
'Navbar'
);

export default Navbar;
Loading
Loading