@wonderflow/react-components 17.0.0 → 17.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -75,6 +75,32 @@ export declare type ProductCardKpisProps = PropsWithClass<{
75
75
  * Set the space between Kpis items.
76
76
  */
77
77
  kpisRowGap?: TokensTypes['space'];
78
+ /**
79
+ * Show a toggle to reveal all the available Kpis, when more than `kpiItems` have a value.
80
+ *
81
+ * @default false
82
+ */
83
+ expandable?: boolean;
84
+ /**
85
+ * Set the label of the toggle when the Kpis are collapsed.
86
+ *
87
+ * @default 'Show more'
88
+ */
89
+ expandLabel?: string;
90
+ /**
91
+ * Set the label of the toggle when the Kpis are expanded.
92
+ *
93
+ * @default 'Show less'
94
+ */
95
+ collapseLabel?: string;
96
+ /**
97
+ * Control the expanded state. When not set, the state is handled internally.
98
+ */
99
+ isExpanded?: boolean;
100
+ /**
101
+ * Callback called when the toggle is pressed.
102
+ */
103
+ onExpandedChange?: (expanded: boolean) => void;
78
104
  /**
79
105
  * Set the loading state showing a skeleton.
80
106
  */
@@ -1 +1 @@
1
- {"version":3,"file":"product-card-kpis.d.ts","sourceRoot":"","sources":["../../../../src/components/product-card/product-card-kpis/product-card-kpis.tsx"],"names":[],"mappings":";AAgBA,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAe/D,oBAAY,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE7J,oBAAY,oBAAoB,GAAG,cAAc,CAAC;IAChD;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;MAEE;IACF,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB;;MAEE;IACF,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;MAEE;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;MAEE;IACF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;MAEE;IACF,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;MAEE;IACF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,UAAU,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;IAClC;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC,CAAA;AAEF,oBAAY,wBAAwB,GAAG,KAAK,CAAC,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;AAS7F,eAAO,MAAM,eAAe,0BAsIE,CAAC"}
1
+ {"version":3,"file":"product-card-kpis.d.ts","sourceRoot":"","sources":["../../../../src/components/product-card/product-card-kpis/product-card-kpis.tsx"],"names":[],"mappings":";AAgBA,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAC;AAmB/D,oBAAY,QAAQ,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,G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,OAAO,CAAC;IACrB;;OAEG;IACH,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,IAAI,CAAC;IAC/C;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC,CAAA;AAEF,oBAAY,wBAAwB,GAAG,KAAK,CAAC,yBAAyB,CAAC,oBAAoB,CAAC,CAAC;AAS7F,eAAO,MAAM,eAAe,0BA2LE,CAAC"}
@@ -1,11 +1,11 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import tkns from '@wonderflow/tokens/platforms/web/tokens.json';
3
3
  import clsx from 'clsx';
4
- import { forwardRef, useMemo } from 'react';
5
- import { Skeleton, Stack, Symbol, Text, } from '../../..';
4
+ import { forwardRef, useCallback, useMemo, useState, } from 'react';
5
+ import { Button, Skeleton, Symbol, Text, } from '../../..';
6
6
  import { formatKpiValue, formatPriceRangeValues, isGreaterThan, isValueOverCap, } from '../../../utils/formatting';
7
7
  import * as styles from './product-card-kpis.module.css';
8
- export const ProductCardKpis = forwardRef(({ rating, feedbackCount, votesCount, votesRating, sentiment, nps, groups, tgw, priceMin, priceMax, currency = 'EUR', currencyDecimals = 0, users, usersCap, skus, skusCap, kpiItems = 3, kpisRowGap = 8, isLoading = false, className, style, }, forwardedRef) => {
8
+ export const ProductCardKpis = forwardRef(({ rating, feedbackCount, votesCount, votesRating, sentiment, nps, groups, tgw, priceMin, priceMax, currency = 'EUR', currencyDecimals = 0, users, usersCap, skus, skusCap, kpiItems = 3, kpisRowGap = 8, expandable = false, expandLabel = 'Show more', collapseLabel = 'Show less', isExpanded: isExpandedProp, onExpandedChange, isLoading = false, className, style, }, forwardedRef) => {
9
9
  const config = useMemo(() => ([
10
10
  {
11
11
  property: 'feedback-rating',
@@ -89,11 +89,24 @@ export const ProductCardKpis = forwardRef(({ rating, feedbackCount, votesCount,
89
89
  skus,
90
90
  skusCap
91
91
  ]);
92
+ const [isExpandedState, setIsExpandedState] = useState(false);
93
+ const isExpanded = isExpandedProp ?? isExpandedState;
94
+ const availableKpis = config.filter(el => el.value);
95
+ const hasToggle = expandable && availableKpis.length > kpiItems;
96
+ const isShowingAll = isExpanded && hasToggle;
97
+ const defaultKpis = availableKpis.slice(0, kpiItems);
98
+ const extraKpis = hasToggle ? availableKpis.slice(kpiItems) : [];
99
+ const rowGap = kpisRowGap ? tkns.space[kpisRowGap] : 0;
100
+ const renderKpi = (el) => (_jsx(Text, { className: styles.Kpi, variant: "body-2", decoratorStart: _jsx(Symbol, { source: el.icon, color: el.iconColor, weight: "solid" }), decoratorSize: "medium", children: el.value }, el.property));
101
+ const handleToggle = useCallback((event) => {
102
+ event.stopPropagation();
103
+ setIsExpandedState(!isExpanded);
104
+ onExpandedChange?.(!isExpanded);
105
+ }, [isExpanded, onExpandedChange]);
92
106
  return (_jsx("div", { className: clsx(styles.Kpis, className), style: style, ref: forwardedRef, children: isLoading
93
- ? (_jsx(Stack, { children: _jsx(Skeleton, { height: "20px", width: "50%", count: kpiItems }) }))
94
- : (_jsx(Stack, { rowGap: kpisRowGap, children: config
95
- .filter(el => el.value)
96
- .slice(0, kpiItems)
97
- .map(el => (_jsx(Text, { className: styles.Kpi, variant: "body-2", decoratorStart: _jsx(Symbol, { source: el.icon, color: el.iconColor, weight: "solid" }), decoratorSize: "medium", children: el.value }, el.property))) })) }));
107
+ ? (_jsx("div", { className: styles.Grid, style: { rowGap }, children: Array.from({ length: kpiItems }).map((_, i) => (
108
+ // eslint-disable-next-line react/no-array-index-key
109
+ _jsx(Skeleton, { height: "20px", width: "100%", inline: true }, i))) }))
110
+ : (_jsxs(_Fragment, { children: [_jsx("div", { className: styles.Grid, style: { rowGap }, children: defaultKpis.map(renderKpi) }), extraKpis.length > 0 && (_jsx("div", { className: styles.Extras, "data-extras-visible": isShowingAll, "aria-hidden": !isShowingAll, "data-inner-element": "ProductCard-KpisExtras", children: _jsx("div", { className: styles.ExtrasContent, children: _jsx("div", { className: styles.Grid, style: { rowGap, paddingBlockStart: rowGap }, children: extraKpis.map(renderKpi) }) }) })), hasToggle && (_jsx(Button, { kind: "flat", dimension: "small", icon: isExpanded ? 'chevron-up' : 'chevron-down', iconPosition: "right", className: styles.Toggle, "aria-expanded": isExpanded, "data-inner-element": "ProductCard-KpisToggle", onClick: handleToggle, children: isExpanded ? collapseLabel : expandLabel }))] })) }));
98
111
  });
99
112
  ProductCardKpis.displayName = 'ProductCardKpis';
@@ -1 +1 @@
1
- @layer components{.Kpis{padding:.5rem 1rem 1rem}.Kpis .Kpi[data-text-decorator=true]{color:var(--dimmed-7);gap:6px}}
1
+ @layer components{.Kpis{padding:.5rem 1rem 1rem}.Grid{column-gap:.5rem;display:grid;grid-template-columns:repeat(2,minmax(0,1fr))}.Extras{display:grid;grid-template-rows:0fr;transition:grid-template-rows .2s cubic-bezier(.3,.07,.34,1) .1s,visibility 0s linear .3s;visibility:hidden}.Extras[data-extras-visible=true]{grid-template-rows:1fr;transition-delay:0s,0s;visibility:visible}.Extras .Grid{opacity:0;transition:opacity .1s cubic-bezier(.3,.07,.34,1)}.Extras[data-extras-visible=true] .Grid{opacity:1;transition-delay:.2s}@media (prefers-reduced-motion:reduce){.Extras,.Extras .Grid{transition:none}}.ExtrasContent{min-height:0;overflow:hidden}.Kpis .Kpi[data-text-decorator=true]{color:var(--dimmed-7);gap:6px}.Kpis .Toggle[data-button-kind=flat]{block-size:auto;color:var(--dimmed-5);font-weight:400;letter-spacing:token(--wds-letter-spacing-14);margin-top:.5rem;padding:0}}
@@ -16,7 +16,8 @@ export declare type ProductCardProps = PropsWithClass<PropsWithChildren<{
16
16
  */
17
17
  highlightOnHover?: boolean;
18
18
  /**
19
- * Set the content to be displayed on the overlay layer.
19
+ * Set the content to be displayed over the cover (image or colored cover) on hover.
20
+ * Ignored when the card is clickable or loading.
20
21
  */
21
22
  overlayActions?: React.ReactNode;
22
23
  /**
@@ -27,10 +28,6 @@ export declare type ProductCardProps = PropsWithClass<PropsWithChildren<{
27
28
  * Set the loading state showing a skeleton.
28
29
  */
29
30
  isLoading?: boolean;
30
- /**
31
- * Shows a colored cover with the first two letters of the title.
32
- */
33
- hasColoredCover?: boolean;
34
31
  /**
35
32
  * Set an action to be performed when clicked.
36
33
  */
@@ -46,10 +43,10 @@ export declare type ProductCardProps = PropsWithClass<PropsWithChildren<{
46
43
  sourceIcon?: ProductCardTopBarProps['sourceIcon'];
47
44
  /**
48
45
  * Set a label (e.g. "New") displayed as a badge over the top-right corner of the cover.
49
- * It is not displayed while loading or when the card has no cover.
46
+ * It is not displayed while loading.
50
47
  */
51
48
  badge?: string;
52
- }>> & Pick<ProductCardMediaProps, 'ratio' | 'source'> & Pick<ProductCardHeaderProps, 'title' | 'titleRows' | 'subtitle' | 'description' | 'descriptionRows'> & Pick<ProductCardKpisProps, 'rating' | 'feedbackCount' | 'votesCount' | 'votesRating' | 'users' | 'skus' | 'sentiment' | 'priceMin' | 'priceMax' | 'nps' | 'kpisRowGap' | 'kpiItems' | 'groups' | 'tgw' | 'skusCap' | 'usersCap' | 'currency' | 'currencyDecimals'>;
49
+ }>> & Pick<ProductCardMediaProps, 'ratio' | 'source'> & Pick<ProductCardHeaderProps, 'title' | 'titleRows' | 'subtitle' | 'description' | 'descriptionRows'> & Pick<ProductCardKpisProps, 'rating' | 'feedbackCount' | 'votesCount' | 'votesRating' | 'users' | 'skus' | 'sentiment' | 'priceMin' | 'priceMax' | 'nps' | 'kpisRowGap' | 'kpiItems' | 'expandable' | 'expandLabel' | 'collapseLabel' | 'groups' | 'tgw' | 'skusCap' | 'usersCap' | 'currency' | 'currencyDecimals'>;
53
50
  export declare type ProductCardComponent = React.ForwardRefExoticComponent<ProductCardProps> & {
54
51
  Media: ProductCardMediaComponent;
55
52
  Kpis: ProductCardKpisComponent;
@@ -1 +1 @@
1
- {"version":3,"file":"product-card.d.ts","sourceRoot":"","sources":["../../../src/components/product-card/product-card.tsx"],"names":[],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M,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IACzB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB;;;OAGG;IACH,WAAW,CAAC,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;IACpD;;OAEG;IACH,UAAU,CAAC,EAAE,sBAAsB,CAAC,YAAY,CAAC,CAAC;IAClD;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC,CAAC,GACD,IAAI,CAAC,qBAAqB,EAAE,OAAO,GAAG,QAAQ,CAAC,GAC/C,IAAI,CAAC,sBAAsB,EAAE,OAAO,GAAG,WAAW,GAAG,UAAU,GAAG,aAAa,GAAG,iBAAiB,CAAC,GACpG,IAAI,CAAC,oBAAoB,EAAE,QAAQ,GAAG,eAAe,GAAG,YAAY,GAAG,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,GAAG,UAAU,GAAG,KAAK,GAAG,YAAY,GAAG,UAAU,GAAG,YAAY,GAAG,aAAa,GAAG,eAAe,GAAG,QAAQ,GAAG,KAAK,GAAG,SAAS,GAAG,UAAU,GAAG,UAAU,GAC/R,kBAAkB,CAAC,CAAA;AA0BnB,oBAAY,oBAAoB,GAAG,KAAK,CAAC,yBAAyB,CAAC,gBAAgB,CAAC,GAAG;IACrF,KAAK,EAAE,yBAAyB,CAAC;IACjC,IAAI,EAAE,wBAAwB,CAAC;IAC/B,MAAM,EAAE,0BAA0B,CAAC;IACnC,MAAM,EAAE,0BAA0B,CAAC;IACnC,MAAM,EAAE,0BAA0B,CAAC;CACpC,CAAC;AAEF,eAAO,MAAM,WAAW,sBAwPE,CAAC"}
@@ -1,14 +1,17 @@
1
1
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import tkns from '@wonderflow/tokens/platforms/web/tokens.json';
3
3
  import clsx from 'clsx';
4
- import { forwardRef } from 'react';
5
- import { Chip, Elevator, Separator, Stack, Text, } from '../..';
4
+ import { forwardRef, useCallback, useEffect, useRef, useState, } from 'react';
5
+ import { mergeRefs } from 'react-merge-refs';
6
+ import { AspectRatio, Chip, Elevator, Separator, Skeleton, Stack, Text, } from '../..';
6
7
  import * as styles from './product-card.module.css';
7
8
  import { ProductCardFooter } from './product-card-footer/product-card-footer';
8
9
  import { ProductCardHeader } from './product-card-header/product-card-header';
9
10
  import { ProductCardKpis } from './product-card-kpis/product-card-kpis';
10
11
  import { ProductCardMedia } from './product-card-media/product-card-media';
11
12
  import { ProductCardTopBar } from './product-card-top-bar/product-card-top-bar';
13
+ // Must be longer than the KPIs collapse animation (fade + slide).
14
+ const LOCK_RELEASE_FALLBACK_MS = 500;
12
15
  const getColorFromString = (input) => {
13
16
  const colorPalette = tkns.color;
14
17
  // Generate a hash from the input string
@@ -26,17 +29,61 @@ const getColorFromString = (input) => {
26
29
  const firstShade = Object.values(shades)[5];
27
30
  return `hsl(${firstShade})`;
28
31
  };
29
- export const ProductCard = forwardRef(({ bordered = true, highlightOnHover = false, title, titleRows = 3, subtitle, description, descriptionRows, rating, feedbackCount, votesCount, votesRating, sentiment, nps, groups, tgw, priceMin, priceMax, currency, currencyDecimals, users, usersCap, skus, skusCap, kpiItems, kpisRowGap, ratio, source, footer, overlayActions, onClick, sourceLabel, sourceIcon, badge, isLoading = false, hasColoredCover = false, className, style, children, ...otherProps }, forwardedRef) => {
32
+ export const ProductCard = forwardRef(({ bordered = true, highlightOnHover = false, title, titleRows = 3, subtitle, description, descriptionRows, rating, feedbackCount, votesCount, votesRating, sentiment, nps, groups, tgw, priceMin, priceMax, currency, currencyDecimals, users, usersCap, skus, skusCap, kpiItems, kpisRowGap, expandable, expandLabel, collapseLabel, ratio, source, footer, overlayActions, onClick, sourceLabel, sourceIcon, badge, isLoading = false, className, style, children, ...otherProps }, forwardedRef) => {
33
+ const cardRef = useRef(null);
34
+ const [isExpanded, setIsExpanded] = useState(false);
35
+ const [isPeeking, setIsPeeking] = useState(false);
36
+ const [lockedHeight, setLockedHeight] = useState();
37
+ const [isLocked, setIsLocked] = useState(false);
38
+ const isKpisExpanded = !!expandable && isExpanded && !isLoading;
39
+ const isShowingAllKpis = isKpisExpanded && !isPeeking;
40
+ const isKpisAvailable = !!expandable && !isLoading;
41
+ const unlock = useCallback(() => {
42
+ setIsLocked(false);
43
+ setLockedHeight(undefined);
44
+ }, []);
45
+ useEffect(() => {
46
+ if (!isKpisAvailable) {
47
+ setIsExpanded(false);
48
+ unlock();
49
+ }
50
+ }, [isKpisAvailable, unlock]);
51
+ useEffect(() => {
52
+ if (!isKpisExpanded)
53
+ setIsPeeking(false);
54
+ }, [isKpisExpanded]);
55
+ // The lock is kept while the collapse animation plays, then released on transition end (or after a fallback delay).
56
+ useEffect(() => {
57
+ if (!isLocked || isKpisExpanded)
58
+ return undefined;
59
+ const timeout = setTimeout(unlock, LOCK_RELEASE_FALLBACK_MS);
60
+ return () => clearTimeout(timeout);
61
+ }, [isLocked, isKpisExpanded, unlock]);
62
+ const handleExpandedChange = useCallback((expanded) => {
63
+ if (expanded && !isLocked) {
64
+ setLockedHeight(cardRef.current?.offsetHeight);
65
+ setIsLocked(true);
66
+ }
67
+ setIsExpanded(expanded);
68
+ }, [isLocked]);
69
+ const handleTransitionEnd = (event) => {
70
+ if (isLocked && !isKpisExpanded && event.propertyName === 'grid-template-rows')
71
+ unlock();
72
+ };
73
+ const handleCoverMouseEnter = isKpisExpanded ? () => setIsPeeking(true) : undefined;
74
+ const handleCoverMouseLeave = isKpisExpanded ? () => setIsPeeking(false) : undefined;
30
75
  const hasOverlay = !!(overlayActions && !onClick && !isLoading);
31
76
  const hasHighlight = !!(onClick ?? highlightOnHover);
32
77
  const isClickable = !!(!isLoading && onClick);
33
78
  const hasTopBar = !!sourceLabel;
34
- const hasColoredCard = hasColoredCover && !source?.length;
35
- const hasMedia = !hasColoredCover && !!source?.length;
36
- const hasCover = hasColoredCard || hasMedia;
79
+ const hasMedia = !!source?.length;
37
80
  const hasKpis = kpiItems !== 0;
38
81
  const hasFooterSeparator = !!footer || isLoading;
39
- return (_jsx(Elevator, { resting: 1, hover: hasHighlight ? 2 : undefined, children: _jsxs(Stack, { ref: forwardedRef, className: clsx(styles.Card, className), style: { ...style }, fill: false, "data-card-bordered": bordered, "data-card-clickable": isClickable, onClick: isLoading ? undefined : onClick, ...otherProps, children: [hasOverlay && (_jsx(Stack, { direction: "row", hPadding: 24, vAlign: "center", hAlign: "center", className: styles.OverlayActions, "data-inner-element": "ProductCard-OverlayActions", children: _jsx(Stack, { children: overlayActions }) })), hasTopBar && (_jsxs(_Fragment, { children: [_jsx(ProductCard.TopBar, { "data-inner-element": "ProductCard-TopBar", sourceLabel: sourceLabel, sourceIcon: sourceIcon, isLoading: isLoading }), _jsx(Separator, {})] })), hasCover && (_jsxs("div", { className: styles.Cover, children: [hasColoredCard && (_jsx(Stack, { className: styles.ColoredCard, style: { backgroundColor: `${getColorFromString(title ?? '')}` }, hAlign: "center", vAlign: "center", fill: false, children: _jsx(Text, { variant: "heading-1", textAlign: "center", style: { color: '#fff' }, children: title?.slice(0, 2) }) })), hasMedia && (_jsx(ProductCard.Media, { source: source, isLoading: isLoading, ratio: ratio, "data-inner-element": "ProductCard-Media" })), badge && !isLoading && (_jsx(Chip, { color: "yellow", className: styles.Badge, "data-inner-element": "ProductCard-Badge", children: badge }))] })), _jsx(ProductCard.Header, { "data-inner-element": "ProductCard-Header", title: title, titleRows: titleRows, subtitle: subtitle, description: description, descriptionRows: descriptionRows, isLoading: isLoading }), hasKpis && (_jsx(ProductCardKpis, { "data-inner-element": "ProductCard-Kpis", rating: rating, feedbackCount: feedbackCount, votesCount: votesCount, votesRating: votesRating, sentiment: sentiment, nps: nps, groups: groups, tgw: tgw, priceMin: priceMin, priceMax: priceMax, currency: currency, currencyDecimals: currencyDecimals, users: users, usersCap: usersCap, skus: skus, skusCap: skusCap, isLoading: isLoading, kpiItems: kpiItems, kpisRowGap: kpisRowGap })), children && !isLoading && (_jsx(Stack, { hPadding: 16, "data-inner-element": "ProductCard-Children", children: children })), _jsxs("div", { className: styles.Bottom, children: [hasFooterSeparator && _jsx(Separator, {}), _jsx(ProductCard.Footer, { isLoading: isLoading, "data-inner-element": "ProductCard-Footer", children: footer })] })] }) }));
82
+ return (_jsx(Elevator, { resting: 1, hover: hasHighlight ? 2 : undefined, children: _jsxs(Stack, { ref: mergeRefs([cardRef, forwardedRef]), className: clsx(styles.Card, className), style: { ...style, height: isLocked ? lockedHeight : style?.height }, fill: false, "data-card-bordered": bordered, "data-card-clickable": isClickable, "data-card-expanded": isShowingAllKpis, "data-card-locked": isLocked, onClick: isLoading ? undefined : onClick, onTransitionEnd: handleTransitionEnd, ...otherProps, children: [hasTopBar && (_jsxs(_Fragment, { children: [_jsx(ProductCard.TopBar, { "data-inner-element": "ProductCard-TopBar", sourceLabel: sourceLabel, sourceIcon: sourceIcon, isLoading: isLoading }), _jsx(Separator, {})] })), _jsxs("div", { className: styles.Cover, "data-inner-element": "ProductCard-Cover", onMouseEnter: handleCoverMouseEnter, onMouseLeave: handleCoverMouseLeave, children: [hasMedia
83
+ ? (_jsx(ProductCard.Media, { source: source, isLoading: isLoading, ratio: ratio, "data-inner-element": "ProductCard-Media" }))
84
+ : (_jsx(AspectRatio, { ratio: ratio ?? '1', children: isLoading
85
+ ? (_jsx(Skeleton, { style: { borderRadius: '0', lineHeight: '2rem' }, width: "inherit", height: "inherit" }))
86
+ : (_jsx(Stack, { className: styles.ColoredCard, style: { backgroundColor: getColorFromString(title ?? '') }, hAlign: "center", vAlign: "center", fill: false, "data-inner-element": "ProductCard-ColoredCover", children: _jsx(Text, { variant: "heading-1", textAlign: "center", style: { color: '#fff' }, children: title?.slice(0, 2) }) })) })), badge && !isLoading && (_jsx(Chip, { color: "yellow", className: styles.Badge, "data-inner-element": "ProductCard-Badge", children: badge })), hasOverlay && (_jsx(Stack, { direction: "row", hPadding: 24, vAlign: "center", hAlign: "center", className: styles.OverlayActions, "data-inner-element": "ProductCard-OverlayActions", children: _jsx(Stack, { children: overlayActions }) }))] }), _jsx(ProductCard.Header, { "data-inner-element": "ProductCard-Header", title: title, titleRows: titleRows, subtitle: subtitle, description: description, descriptionRows: descriptionRows, isLoading: isLoading }), hasKpis && (_jsx(ProductCardKpis, { "data-inner-element": "ProductCard-Kpis", rating: rating, feedbackCount: feedbackCount, votesCount: votesCount, votesRating: votesRating, sentiment: sentiment, nps: nps, groups: groups, tgw: tgw, priceMin: priceMin, priceMax: priceMax, currency: currency, currencyDecimals: currencyDecimals, users: users, usersCap: usersCap, skus: skus, skusCap: skusCap, isLoading: isLoading, kpiItems: kpiItems, kpisRowGap: kpisRowGap, expandable: expandable, expandLabel: expandLabel, collapseLabel: collapseLabel, isExpanded: isShowingAllKpis, onExpandedChange: handleExpandedChange })), children && !isLoading && (_jsx(Stack, { hPadding: 16, "data-inner-element": "ProductCard-Children", children: children })), _jsxs("div", { className: styles.Bottom, children: [hasFooterSeparator && _jsx(Separator, {}), _jsx(ProductCard.Footer, { isLoading: isLoading, "data-inner-element": "ProductCard-Footer", children: footer })] })] }) }));
40
87
  });
41
88
  ProductCard.displayName = 'ProductCard';
42
89
  ProductCard.Media = ProductCardMedia;
@@ -1 +1 @@
1
- @layer components{.Card{background-color:var(--global-background);border-radius:12px;overflow:hidden;padding:0;position:relative}.Card[data-card-bordered=true]{border:1px solid var(--dimmed-1)}.Card[data-card-clickable=true]{cursor:pointer}.Cover{position:relative}.Badge{position:absolute;right:1rem;top:10px}.Bottom{margin-top:auto}.OverlayActions{background-color:var(--global-background);height:100%;opacity:0;position:absolute;transition:opacity .3s;width:100%;z-index:1}.OverlayActions:hover{opacity:1}.ColoredCard{height:125px;max-height:125px;width:100%}}
1
+ @layer components{.Card{background-color:var(--global-background);border-radius:12px;overflow:hidden;padding:0;position:relative}.Card[data-card-bordered=true]{border:1px solid var(--dimmed-1)}.Card[data-card-clickable=true]{cursor:pointer}.Cover{overflow:hidden;position:relative}.Card[data-card-locked=true]>:not(.Cover){flex-shrink:0}.Card[data-card-locked=true] .Cover{flex:1 1 auto;min-height:0}.Badge{position:absolute;right:1rem;top:10px}.Bottom{margin-top:auto}.OverlayActions{background-color:var(--global-background);inset:0;opacity:0;position:absolute;transition:opacity .3s;z-index:1}.OverlayActions:hover{opacity:1}.Card[data-card-locked=true] .OverlayActions:hover{transition-delay:.3s}.ColoredCard{width:100%}}
@@ -3,15 +3,17 @@ import { ProductCard } from './product-card';
3
3
  declare const story: ComponentMeta<typeof ProductCard>;
4
4
  export default story;
5
5
  export declare const Default: ComponentStory<import("./product-card").ProductCardComponent>;
6
- export declare const withoutTopBar: ComponentStory<import("./product-card").ProductCardComponent>;
7
- export declare const withSourceLabelOnly: ComponentStory<import("./product-card").ProductCardComponent>;
8
- export declare const loading: ComponentStory<import("./product-card").ProductCardComponent>;
9
- export declare const withFewKpis: ComponentStory<import("./product-card").ProductCardComponent>;
10
- export declare const withOverlayActions: ComponentStory<import("./product-card").ProductCardComponent>;
11
- export declare const clickable: ComponentStory<import("./product-card").ProductCardComponent>;
12
- export declare const withMultipleProducts: ComponentStory<import("./product-card").ProductCardComponent>;
13
- export declare const withBrokenImage: ComponentStory<import("./product-card").ProductCardComponent>;
14
- export declare const withNoMedia: ComponentStory<import("./product-card").ProductCardComponent>;
15
- export declare const withDescription: ComponentStory<import("./product-card").ProductCardComponent>;
16
- export declare const withColoredCover: ComponentStory<import("./product-card").ProductCardComponent>;
6
+ export declare const Loading: ComponentStory<import("./product-card").ProductCardComponent>;
7
+ export declare const WithMultipleImages: ComponentStory<import("./product-card").ProductCardComponent>;
8
+ export declare const WithBrokenImage: ComponentStory<import("./product-card").ProductCardComponent>;
9
+ export declare const WithColoredCover: ComponentStory<import("./product-card").ProductCardComponent>;
10
+ export declare const WithoutTopBar: ComponentStory<import("./product-card").ProductCardComponent>;
11
+ export declare const WithSourceLabelOnly: ComponentStory<import("./product-card").ProductCardComponent>;
12
+ export declare const WithFewKpis: ComponentStory<import("./product-card").ProductCardComponent>;
13
+ export declare const WithDescription: ComponentStory<import("./product-card").ProductCardComponent>;
14
+ export declare const Clickable: ComponentStory<import("./product-card").ProductCardComponent>;
15
+ export declare const WithOverlayActions: ComponentStory<import("./product-card").ProductCardComponent>;
16
+ export declare const WithOverlayActionsOnColoredCover: ComponentStory<import("./product-card").ProductCardComponent>;
17
+ export declare const ExpandableKpis: ComponentStory<import("./product-card").ProductCardComponent>;
18
+ export declare const ExpandableKpisInGrid: ComponentStory<typeof ProductCard>;
17
19
  //# sourceMappingURL=product-card.stories.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"product-card.stories.d.ts","sourceRoot":"","sources":["../../../src/components/product-card/product-card.stories.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAOjE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAmB7C,QAAA,MAAM,KAAK,EAAE,aAAa,CAAC,OAAO,WAAW,CAqD5C,CAAC;AAEF,eAAe,KAAK,CAAC;AAMrB,eAAO,MAAM,OAAO,+DAAoB,CAAC;AAGzC,eAAO,MAAM,aAAa,+DAAoB,CAAC;AAO/C,eAAO,MAAM,mBAAmB,+DAAoB,CAAC;AAMrD,eAAO,MAAM,OAAO,+DAAoB,CAAC;AAKzC,eAAO,MAAM,WAAW,+DAAoB,CAAC;AAkB7C,eAAO,MAAM,kBAAkB,+DAAoB,CAAC;AAKpD,eAAO,MAAM,SAAS,+DAAoB,CAAC;AAK3C,eAAO,MAAM,oBAAoB,+DAAoB,CAAC;AAKtD,eAAO,MAAM,eAAe,+DAAoB,CAAC;AAKjD,eAAO,MAAM,WAAW,+DAAoB,CAAC;AAwB7C,eAAO,MAAM,eAAe,+DAAoB,CAAC;AAcjD,eAAO,MAAM,gBAAgB,+DAAoB,CAAC"}
1
+ {"version":3,"file":"product-card.stories.d.ts","sourceRoot":"","sources":["../../../src/components/product-card/product-card.stories.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAOjE,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAoC7C,QAAA,MAAM,KAAK,EAAE,aAAa,CAAC,OAAO,WAAW,CAwD5C,CAAC;AAEF,eAAe,KAAK,CAAC;AAQrB,eAAO,MAAM,OAAO,+DAAoB,CAAC;AAGzC,eAAO,MAAM,OAAO,+DAAoB,CAAC;AAOzC,eAAO,MAAM,kBAAkB,+DAAoB,CAAC;AAKpD,eAAO,MAAM,eAAe,+DAAoB,CAAC;AAKjD,eAAO,MAAM,gBAAgB,+DAAoB,CAAC;AAiBlD,eAAO,MAAM,aAAa,+DAAoB,CAAC;AAO/C,eAAO,MAAM,mBAAmB,+DAAoB,CAAC;AAQrD,eAAO,MAAM,WAAW,+DAAoB,CAAC;AAQ7C,eAAO,MAAM,eAAe,+DAAoB,CAAC;AAgBjD,eAAO,MAAM,SAAS,+DAAoB,CAAC;AAK3C,eAAO,MAAM,kBAAkB,+DAAoB,CAAC;AAKpD,eAAO,MAAM,gCAAgC,+DAAoB,CAAC;AAMlE,eAAO,MAAM,cAAc,+DAAoB,CAAC;AAOhD,eAAO,MAAM,oBAAoB,EAAE,cAAc,CAAC,OAAO,WAAW,CAKnE,CAAC"}
@@ -9,6 +9,22 @@ const SourceImages = [
9
9
  // 'https://storage.googleapis.com/wonderflow-product-images/delonghi_coffee_MC%20NESPRESSO%20INISSIA.png',
10
10
  ];
11
11
  const SourceLogo = _jsx("img", { src: "/amazon-logo.png", alt: "Amazon" });
12
+ const NoKpis = {
13
+ rating: undefined,
14
+ nps: undefined,
15
+ feedbackCount: undefined,
16
+ votesRating: undefined,
17
+ votesCount: undefined,
18
+ sentiment: undefined,
19
+ tgw: undefined,
20
+ priceMin: undefined,
21
+ priceMax: undefined,
22
+ groups: undefined,
23
+ users: undefined,
24
+ usersCap: undefined,
25
+ skus: undefined,
26
+ skusCap: undefined,
27
+ };
12
28
  const story = {
13
29
  title: 'Domain Components/Product Card',
14
30
  component: ProductCard,
@@ -39,6 +55,9 @@ const story = {
39
55
  skusCap: 1000,
40
56
  kpiItems: 3,
41
57
  kpisRowGap: 8,
58
+ expandable: false,
59
+ expandLabel: 'Show more',
60
+ collapseLabel: 'Show less',
42
61
  isLoading: false,
43
62
  ratio: '1',
44
63
  overlayActions: undefined,
@@ -65,81 +84,58 @@ const story = {
65
84
  };
66
85
  export default story;
67
86
  const Template = args => (_jsx("div", { style: { width: '272px' }, children: _jsx(ProductCard, { ...args }) }));
87
+ // Base
68
88
  export const Default = Template.bind({});
69
89
  Default.args = {};
70
- export const withoutTopBar = Template.bind({});
71
- withoutTopBar.args = {
72
- sourceLabel: undefined,
73
- sourceIcon: undefined,
74
- badge: undefined,
75
- };
76
- export const withSourceLabelOnly = Template.bind({});
77
- withSourceLabelOnly.args = {
78
- sourceIcon: undefined,
79
- badge: undefined,
80
- };
81
- export const loading = Template.bind({});
82
- loading.args = {
90
+ export const Loading = Template.bind({});
91
+ Loading.args = {
83
92
  isLoading: true,
84
93
  };
85
- export const withFewKpis = Template.bind({});
86
- withFewKpis.args = {
87
- rating: 4.3,
88
- nps: undefined,
89
- feedbackCount: 123,
90
- votesRating: undefined,
91
- votesCount: undefined,
92
- sentiment: 0.54,
93
- tgw: undefined,
94
- priceMin: undefined,
95
- priceMax: undefined,
96
- groups: undefined,
97
- users: undefined,
98
- usersCap: undefined,
99
- skus: undefined,
100
- skusCap: undefined,
101
- };
102
- export const withOverlayActions = Template.bind({});
103
- withOverlayActions.args = {
104
- overlayActions: OverlayButtons,
105
- };
106
- export const clickable = Template.bind({});
107
- clickable.args = {
108
- onClick: () => console.log('pressed'),
109
- };
110
- export const withMultipleProducts = Template.bind({});
111
- withMultipleProducts.args = {
94
+ // Cover
95
+ export const WithMultipleImages = Template.bind({});
96
+ WithMultipleImages.args = {
112
97
  source: SourceImages,
113
98
  };
114
- export const withBrokenImage = Template.bind({});
115
- withBrokenImage.args = {
99
+ export const WithBrokenImage = Template.bind({});
100
+ WithBrokenImage.args = {
116
101
  source: [...SourceImages, 'broken'],
117
102
  };
118
- export const withNoMedia = Template.bind({});
119
- withNoMedia.args = {
103
+ export const WithColoredCover = Template.bind({});
104
+ WithColoredCover.args = {
105
+ ...NoKpis,
120
106
  source: [],
121
107
  subtitle: '',
122
- title: 'Wonderflow Space Catalog Label',
108
+ title: 'My first board',
123
109
  titleRows: 2,
124
110
  kpiItems: 2,
125
- rating: undefined,
126
- sentiment: undefined,
127
- feedbackCount: undefined,
128
- votesCount: undefined,
129
- votesRating: undefined,
130
- nps: undefined,
131
- groups: undefined,
132
- tgw: undefined,
133
- priceMin: undefined,
134
- priceMax: undefined,
135
111
  users: 1789,
136
112
  usersCap: 1000,
137
113
  skus: 234,
138
114
  skusCap: 1000,
139
115
  footer: undefined,
140
116
  };
141
- export const withDescription = Template.bind({});
142
- withDescription.args = {
117
+ // Top bar
118
+ export const WithoutTopBar = Template.bind({});
119
+ WithoutTopBar.args = {
120
+ sourceLabel: undefined,
121
+ sourceIcon: undefined,
122
+ badge: undefined,
123
+ };
124
+ export const WithSourceLabelOnly = Template.bind({});
125
+ WithSourceLabelOnly.args = {
126
+ sourceIcon: undefined,
127
+ badge: undefined,
128
+ };
129
+ // Content
130
+ export const WithFewKpis = Template.bind({});
131
+ WithFewKpis.args = {
132
+ ...NoKpis,
133
+ rating: 4.3,
134
+ feedbackCount: 123,
135
+ sentiment: 0.54,
136
+ };
137
+ export const WithDescription = Template.bind({});
138
+ WithDescription.args = {
143
139
  sourceLabel: undefined,
144
140
  sourceIcon: undefined,
145
141
  badge: undefined,
@@ -151,28 +147,27 @@ withDescription.args = {
151
147
  kpiItems: 0,
152
148
  footer: undefined,
153
149
  };
154
- export const withColoredCover = Template.bind({});
155
- withColoredCover.args = {
156
- badge: 'New',
157
- source: [],
158
- subtitle: '',
159
- title: 'My first board',
160
- titleRows: 2,
150
+ // Interactions
151
+ export const Clickable = Template.bind({});
152
+ Clickable.args = {
153
+ onClick: () => console.log('pressed'),
154
+ };
155
+ export const WithOverlayActions = Template.bind({});
156
+ WithOverlayActions.args = {
157
+ overlayActions: OverlayButtons,
158
+ };
159
+ export const WithOverlayActionsOnColoredCover = Template.bind({});
160
+ WithOverlayActionsOnColoredCover.args = {
161
+ ...WithColoredCover.args,
162
+ overlayActions: OverlayButtons,
163
+ };
164
+ export const ExpandableKpis = Template.bind({});
165
+ ExpandableKpis.args = {
161
166
  kpiItems: 2,
162
- rating: undefined,
163
- sentiment: undefined,
164
- feedbackCount: undefined,
165
- votesCount: undefined,
166
- votesRating: undefined,
167
- nps: undefined,
168
- groups: undefined,
169
- tgw: undefined,
170
- priceMin: undefined,
171
- priceMax: undefined,
172
- users: 1789,
173
- usersCap: 1000,
174
- skus: 234,
175
- skusCap: 1000,
176
- footer: undefined,
177
- hasColoredCover: true,
167
+ expandable: true,
168
+ overlayActions: OverlayButtons,
169
+ };
170
+ export const ExpandableKpisInGrid = args => (_jsxs("div", { style: { display: 'grid', gridTemplateColumns: 'repeat(2, 272px)', gap: '16px' }, children: [_jsx(ProductCard, { ...args }), _jsx(ProductCard, { ...args, expandable: false })] }));
171
+ ExpandableKpisInGrid.args = {
172
+ ...ExpandableKpis.args,
178
173
  };
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { render, screen } from '@testing-library/react';
2
+ import { createEvent, fireEvent, render, screen, } from '@testing-library/react';
3
3
  import { ProductCard } from './product-card';
4
4
  describe('<ProductCard>', () => {
5
5
  const SourceImages = [
@@ -31,7 +31,7 @@ describe('<ProductCard>', () => {
31
31
  expect(container).not.toBeNull();
32
32
  });
33
33
  test('it should render properly with colored cover', () => {
34
- const { container } = render(_jsx(ProductCard, { title: "title", subtitle: "subtitle", rating: 1.2, feedbackCount: 1.2, votesCount: 1.2, votesRating: 1.2, sentiment: 1.2, nps: 1.2, groups: 1.2, priceMin: 1.2, priceMax: 1.2, users: 1.2, skus: 1.2, kpiItems: 2, kpisRowGap: 16, bordered: true, highlightOnHover: true, hasColoredCover: true }));
34
+ const { container } = render(_jsx(ProductCard, { title: "title", subtitle: "subtitle", rating: 1.2, feedbackCount: 1.2, votesCount: 1.2, votesRating: 1.2, sentiment: 1.2, nps: 1.2, groups: 1.2, priceMin: 1.2, priceMax: 1.2, users: 1.2, skus: 1.2, kpiItems: 2, kpisRowGap: 16, bordered: true, highlightOnHover: true }));
35
35
  expect(container).not.toBeNull();
36
36
  const coverText = screen.getByText('ti');
37
37
  expect(coverText).toBeDefined();
@@ -44,18 +44,40 @@ describe('<ProductCard>', () => {
44
44
  expect(screen.getByText('Amazon')).toBeDefined();
45
45
  expect(screen.getByAltText('logo')).toBeDefined();
46
46
  });
47
- test('it should render the badge only over a cover and not while loading', () => {
47
+ test('it should render the badge over the cover and not while loading', () => {
48
48
  const { container, rerender } = render(_jsx(ProductCard, { title: "title", source: SourceImages, badge: "New" }));
49
49
  const badge = () => container.querySelector('[data-inner-element="ProductCard-Badge"]');
50
50
  expect(badge()).not.toBeNull();
51
51
  expect(screen.getByText('New')).toBeDefined();
52
- rerender(_jsx(ProductCard, { title: "title", hasColoredCover: true, badge: "New" }));
52
+ rerender(_jsx(ProductCard, { title: "title", badge: "New" }));
53
53
  expect(badge()).not.toBeNull();
54
54
  rerender(_jsx(ProductCard, { title: "title", source: SourceImages, badge: "New", isLoading: true }));
55
55
  expect(badge()).toBeNull();
56
- rerender(_jsx(ProductCard, { title: "title", badge: "New" }));
56
+ rerender(_jsx(ProductCard, { title: "title", badge: "New", isLoading: true }));
57
57
  expect(badge()).toBeNull();
58
58
  });
59
+ test('it should render the media or the colored cover, with overlay actions only inside the cover', () => {
60
+ const { container, rerender } = render(_jsx(ProductCard, { title: "title", source: SourceImages, overlayActions: _jsx("button", { type: "button", children: "Action" }) }));
61
+ const cover = () => container.querySelector('[data-inner-element="ProductCard-Cover"]');
62
+ const media = () => container.querySelector('[data-inner-element="ProductCard-Media"]');
63
+ const coloredCover = () => container.querySelector('[data-inner-element="ProductCard-ColoredCover"]');
64
+ const overlay = () => container.querySelector('[data-inner-element="ProductCard-OverlayActions"]');
65
+ expect(media()).not.toBeNull();
66
+ expect(coloredCover()).toBeNull();
67
+ expect(cover()?.contains(overlay())).toBe(true);
68
+ expect(screen.getByText('Action')).toBeDefined();
69
+ rerender(_jsx(ProductCard, { title: "title", overlayActions: _jsx("button", { type: "button", children: "Action" }) }));
70
+ expect(media()).toBeNull();
71
+ expect(coloredCover()).not.toBeNull();
72
+ expect(screen.getByText('ti')).toBeDefined();
73
+ expect(cover()?.contains(overlay())).toBe(true);
74
+ rerender(_jsx(ProductCard, { title: "title", overlayActions: _jsx("button", { type: "button", children: "Action" }), onClick: jest.fn() }));
75
+ expect(overlay()).toBeNull();
76
+ rerender(_jsx(ProductCard, { title: "title", overlayActions: _jsx("button", { type: "button", children: "Action" }), isLoading: true }));
77
+ expect(overlay()).toBeNull();
78
+ expect(cover()).not.toBeNull();
79
+ expect(coloredCover()).toBeNull();
80
+ });
59
81
  test('it should render the footer separator only with a footer or while loading', () => {
60
82
  const { container, rerender } = render(_jsx(ProductCard, { title: "title" }));
61
83
  expect(container.querySelectorAll('hr')).toHaveLength(0);
@@ -80,4 +102,69 @@ describe('<ProductCard>', () => {
80
102
  rerender(_jsx(ProductCard, { title: "title", bordered: false }));
81
103
  expect(container.querySelector('[data-card-bordered="false"]')).not.toBeNull();
82
104
  });
105
+ describe('expandable KPIs', () => {
106
+ const kpis = {
107
+ rating: 4.3, nps: 23, feedbackCount: 123, votesRating: 2.3, tgw: 0.8,
108
+ };
109
+ const isHidden = (text) => !!screen.getByText(text).closest('[aria-hidden="true"]');
110
+ const toggle = () => screen.queryByRole('button', { name: /show (more|less)/i });
111
+ test('it should render the toggle only when expandable, with more KPIs than kpiItems, and not loading', () => {
112
+ const { rerender } = render(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2 }));
113
+ expect(toggle()).toBeNull();
114
+ rerender(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 5, expandable: true }));
115
+ expect(toggle()).toBeNull();
116
+ rerender(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2, expandable: true }));
117
+ expect(toggle()).not.toBeNull();
118
+ rerender(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2, expandable: true, isLoading: true }));
119
+ expect(toggle()).toBeNull();
120
+ });
121
+ test('it should expand and collapse the KPIs without triggering the card click', () => {
122
+ const onClick = jest.fn();
123
+ const { container } = render(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2, expandable: true, onClick: onClick }));
124
+ const card = () => container.querySelector('[data-card-expanded]');
125
+ expect(screen.getByText('4.3')).toBeDefined();
126
+ expect(screen.getByText('23')).toBeDefined();
127
+ expect(isHidden('123')).toBe(true);
128
+ expect(card()?.getAttribute('data-card-expanded')).toBe('false');
129
+ fireEvent.click(screen.getByRole('button', { name: 'Show more' }));
130
+ expect(isHidden('123')).toBe(false);
131
+ expect(isHidden('0.8')).toBe(false);
132
+ expect(toggle()?.getAttribute('aria-expanded')).toBe('true');
133
+ expect(toggle()?.textContent).toBe('Show less');
134
+ expect(card()?.getAttribute('data-card-expanded')).toBe('true');
135
+ fireEvent.click(screen.getByRole('button', { name: 'Show less' }));
136
+ expect(isHidden('123')).toBe(true);
137
+ expect(toggle()?.getAttribute('aria-expanded')).toBe('false');
138
+ expect(card()?.getAttribute('data-card-expanded')).toBe('false');
139
+ expect(onClick).not.toHaveBeenCalled();
140
+ });
141
+ test('it should keep the card locked while collapsing and release it when the transition ends', () => {
142
+ const { container } = render(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2, expandable: true }));
143
+ const card = () => container.querySelector('[data-card-locked]');
144
+ const extras = () => container.querySelector('[data-inner-element="ProductCard-KpisExtras"]');
145
+ expect(card()?.getAttribute('data-card-locked')).toBe('false');
146
+ fireEvent.click(screen.getByRole('button', { name: 'Show more' }));
147
+ expect(card()?.getAttribute('data-card-locked')).toBe('true');
148
+ fireEvent.click(screen.getByRole('button', { name: 'Show less' }));
149
+ expect(card()?.getAttribute('data-card-locked')).toBe('true');
150
+ const transitionEnd = createEvent.transitionEnd(extras());
151
+ Object.defineProperty(transitionEnd, 'propertyName', { value: 'grid-template-rows' });
152
+ fireEvent(extras(), transitionEnd);
153
+ expect(card()?.getAttribute('data-card-locked')).toBe('false');
154
+ });
155
+ test('it should temporarily restore the default state while hovering the cover', () => {
156
+ const { container } = render(_jsx(ProductCard, { title: "title", ...kpis, kpiItems: 2, expandable: true, expandLabel: "More", collapseLabel: "Less" }));
157
+ const cover = container.querySelector('[data-inner-element="ProductCard-Cover"]');
158
+ fireEvent.click(screen.getByRole('button', { name: 'More' }));
159
+ expect(isHidden('123')).toBe(false);
160
+ fireEvent.mouseEnter(cover);
161
+ expect(isHidden('123')).toBe(true);
162
+ expect(screen.getByRole('button', { name: 'More' })).toBeDefined();
163
+ expect(container.querySelector('[data-card-expanded="false"]')).not.toBeNull();
164
+ fireEvent.mouseLeave(cover);
165
+ expect(isHidden('123')).toBe(false);
166
+ expect(screen.getByRole('button', { name: 'Less' })).toBeDefined();
167
+ expect(container.querySelector('[data-card-expanded="true"]')).not.toBeNull();
168
+ });
169
+ });
83
170
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wonderflow/react-components",
3
- "version": "17.0.0",
3
+ "version": "17.2.0",
4
4
  "description": "UI components from Wonderflow's Wanda design system",
5
5
  "author": "Wonderflow",
6
6
  "license": "Apache-2.0",
@@ -81,9 +81,9 @@
81
81
  "@types/react": "17.0.43",
82
82
  "@types/react-dom": "17.0.14",
83
83
  "@types/react-test-renderer": "18.0.7",
84
- "@wonderflow/config": "17.0.0",
85
- "@wonderflow/tokens": "17.0.0",
86
- "@wonderflow/charts": "17.0.0",
84
+ "@wonderflow/config": "17.2.0",
85
+ "@wonderflow/tokens": "17.2.0",
86
+ "@wonderflow/charts": "17.2.0",
87
87
  "@types/uuid": "9.0.8",
88
88
  "babel-loader": "8.2.5",
89
89
  "cpy-cli": "3.1.1",