From 29b0b73f972f0e464c4b2c889088f07976e9349c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matuzal=C3=A9m=20Teles?= Date: Thu, 14 Mar 2024 16:58:14 -0500 Subject: [PATCH] feat(@clayui/core): moves Breadcrumb implementation to package --- .../clay-core/src/breadcrumb/Breadcrumb.tsx | 136 ++++++++++++++++++ packages/clay-core/src/breadcrumb/Item.tsx | 54 +++++++ packages/clay-core/src/breadcrumb/index.ts | 6 + packages/clay-core/src/index.ts | 1 + 4 files changed, 197 insertions(+) create mode 100644 packages/clay-core/src/breadcrumb/Breadcrumb.tsx create mode 100644 packages/clay-core/src/breadcrumb/Item.tsx create mode 100644 packages/clay-core/src/breadcrumb/index.ts diff --git a/packages/clay-core/src/breadcrumb/Breadcrumb.tsx b/packages/clay-core/src/breadcrumb/Breadcrumb.tsx new file mode 100644 index 0000000000..a79726d278 --- /dev/null +++ b/packages/clay-core/src/breadcrumb/Breadcrumb.tsx @@ -0,0 +1,136 @@ +/** + * SPDX-FileCopyrightText: © 2019 Liferay, Inc. + * SPDX-License-Identifier: BSD-3-Clause + */ + +import {ClayButtonWithIcon} from '@clayui/button'; +import classNames from 'classnames'; +import React, {useState} from 'react'; +import warning from 'warning'; + +import {Item} from './Item'; + +type TItem = React.ComponentProps; +type TItems = Array; + +export type Props = React.HTMLAttributes & { + /** + * Defines the aria label of component elements. + */ + ariaLabels?: { + breadcrumb: string; + open: string; + close: string; + }; + + /** + * The number of Breadcrumb Items to show on each side of the active Breadcrumb Item before + * using an ellipsis dropdown. + * @deprecated since v3.91.0 - It is no longer necessary. + */ + ellipsisBuffer?: number; + + /** + * Use this property for defining `otherProps` that will be passed to ellipsis dropdown trigger. + * @deprecated since v3.91.0 - It is no longer necessary. + */ + ellipsisProps?: Object; + + /** + * Property to define Breadcrumb's items. + */ + items: TItems; + + /** + * Path to the location of the spritemap resource. + */ + spritemap?: string; +}; + +const findActiveItems = (items: TItems) => { + return items.filter((item) => { + return item.active; + }); +}; + +export const Breadcrumb = ({ + ariaLabels = { + breadcrumb: 'Breadcrumb', + close: 'Partially nest breadcrumbs', + open: 'See full nested', + }, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ellipsisBuffer = 1, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + ellipsisProps = {}, + className, + items, + spritemap, + ...otherProps +}: Props) => { + warning( + findActiveItems(items).length === 1, + 'ClayBreadcrumb expects at least one `active` item on `items`.' + ); + + const [collapsed, setCollapsed] = useState(false); + + return ( + + ); +}; + +type ItemsProps = { + items: TItems; +}; + +function Items({items}: ItemsProps) { + return ( + <> + {items.map((item: TItem | React.ReactNode, i: number) => + React.isValidElement(item) ? ( + React.cloneElement(item, {key: `ellipsis${i}`}) + ) : ( + + ) + )} + + ); +} diff --git a/packages/clay-core/src/breadcrumb/Item.tsx b/packages/clay-core/src/breadcrumb/Item.tsx new file mode 100644 index 0000000000..c6babb00e7 --- /dev/null +++ b/packages/clay-core/src/breadcrumb/Item.tsx @@ -0,0 +1,54 @@ +/** + * SPDX-FileCopyrightText: © 2019 Liferay, Inc. + * SPDX-License-Identifier: BSD-3-Clause + */ + +import Link from '@clayui/link'; +import classNames from 'classnames'; +import React from 'react'; + +type Props = React.HTMLAttributes & { + /** + * Flag to indicate if the Breadcrumb item is active or not. + */ + active?: boolean; + + /** + * This value is used to be the target of the link. + */ + href?: string; + + /** + * Label of the Breadcrumb item + */ + label: string; + + /** + * Callback for when a Breadcrumb item is clicked. + */ + onClick?: (event: React.SyntheticEvent) => void; +}; + +export const Item = ({active, href, label, onClick, ...otherProps}: Props) => ( +
  • + { + if (onClick) { + event.preventDefault(); + onClick(event); + } + }} + > + {label} + +
  • +); diff --git a/packages/clay-core/src/breadcrumb/index.ts b/packages/clay-core/src/breadcrumb/index.ts new file mode 100644 index 0000000000..e8402ebf82 --- /dev/null +++ b/packages/clay-core/src/breadcrumb/index.ts @@ -0,0 +1,6 @@ +/** + * SPDX-FileCopyrightText: © 2024 Liferay, Inc. + * SPDX-License-Identifier: BSD-3-Clause + */ + +export * from './Breadcrumb'; diff --git a/packages/clay-core/src/index.ts b/packages/clay-core/src/index.ts index 1abdb9192a..bcff0fd11a 100644 --- a/packages/clay-core/src/index.ts +++ b/packages/clay-core/src/index.ts @@ -17,6 +17,7 @@ export {Provider, useProvider} from '@clayui/provider'; export {Alert} from './alert'; export {Badge} from './badge'; +export {Breadcrumb} from './breadcrumb'; export {Heading, Text, TextHighlight} from './typography'; export {OverlayMask} from './overlay-mask'; export {TreeView} from './tree-view';