@wonderflow/react-components 17.1.0 → 17.3.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.
- package/dist/components/product-card/product-card-kpis/product-card-kpis.d.ts +26 -0
- package/dist/components/product-card/product-card-kpis/product-card-kpis.d.ts.map +1 -1
- package/dist/components/product-card/product-card-kpis/product-card-kpis.js +22 -9
- package/dist/components/product-card/product-card-kpis/product-card-kpis.module.css +1 -1
- package/dist/components/product-card/product-card.d.ts +1 -1
- package/dist/components/product-card/product-card.d.ts.map +1 -1
- package/dist/components/product-card/product-card.js +49 -4
- package/dist/components/product-card/product-card.module.css +1 -1
- package/dist/components/product-card/product-card.stories.d.ts +2 -0
- package/dist/components/product-card/product-card.stories.d.ts.map +1 -1
- package/dist/components/product-card/product-card.stories.js +13 -0
- package/dist/components/product-card/product-card.test.js +66 -1
- package/package.json +4 -4
|
@@ -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;
|
|
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,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;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,UAAU,CAAC,EAAE,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 {
|
|
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(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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}}
|
|
@@ -46,7 +46,7 @@ export declare type ProductCardProps = PropsWithClass<PropsWithChildren<{
|
|
|
46
46
|
* It is not displayed while loading.
|
|
47
47
|
*/
|
|
48
48
|
badge?: string;
|
|
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' | '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'>;
|
|
50
50
|
export declare type ProductCardComponent = React.ForwardRefExoticComponent<ProductCardProps> & {
|
|
51
51
|
Media: ProductCardMediaComponent;
|
|
52
52
|
Kpis: ProductCardKpisComponent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"product-card.d.ts","sourceRoot":"","sources":["../../../src/components/product-card/product-card.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"product-card.d.ts","sourceRoot":"","sources":["../../../src/components/product-card/product-card.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,EACA,iBAAiB,EAC9B,MAAM,OAAO,CAAC;AAcf,OAAO,EAAqB,0BAA0B,EAAE,MAAM,2CAA2C,CAAC;AAC1G,OAAO,EAAqB,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,2CAA2C,CAAC;AAClI,OAAO,EAAmB,wBAAwB,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AACxH,OAAO,EAAoB,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,yCAAyC,CAAC;AAC7H,OAAO,EAAqB,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,6CAA6C,CAAC;AAEpI,oBAAY,gBAAgB,GAAG,cAAc,CAAC,iBAAiB,CAAC;IAC9D;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;;OAGG;IACH,cAAc,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IACjC;;OAEG;IACH,MAAM,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,7 +1,8 @@
|
|
|
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';
|
|
4
|
+
import { forwardRef, useCallback, useEffect, useRef, useState, } from 'react';
|
|
5
|
+
import { mergeRefs } from 'react-merge-refs';
|
|
5
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';
|
|
@@ -9,6 +10,8 @@ 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,7 +29,49 @@ 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, 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);
|
|
@@ -34,11 +79,11 @@ export const ProductCard = forwardRef(({ bordered = true, highlightOnHover = fal
|
|
|
34
79
|
const hasMedia = !!source?.length;
|
|
35
80
|
const hasKpis = kpiItems !== 0;
|
|
36
81
|
const hasFooterSeparator = !!footer || isLoading;
|
|
37
|
-
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: [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", children: [hasMedia
|
|
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
|
|
38
83
|
? (_jsx(ProductCard.Media, { source: source, isLoading: isLoading, ratio: ratio, "data-inner-element": "ProductCard-Media" }))
|
|
39
84
|
: (_jsx(AspectRatio, { ratio: ratio ?? '1', children: isLoading
|
|
40
85
|
? (_jsx(Skeleton, { style: { borderRadius: '0', lineHeight: '2rem' }, width: "inherit", height: "inherit" }))
|
|
41
|
-
: (_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 })), 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 })] })] }) }));
|
|
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 })] })] }) }));
|
|
42
87
|
});
|
|
43
88
|
ProductCard.displayName = 'ProductCard';
|
|
44
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{overflow:hidden;position:relative}.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}.ColoredCard{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%}}
|
|
@@ -14,4 +14,6 @@ export declare const WithDescription: ComponentStory<import("./product-card").Pr
|
|
|
14
14
|
export declare const Clickable: ComponentStory<import("./product-card").ProductCardComponent>;
|
|
15
15
|
export declare const WithOverlayActions: ComponentStory<import("./product-card").ProductCardComponent>;
|
|
16
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;AAoC7C,QAAA,MAAM,KAAK,EAAE,aAAa,CAAC,OAAO,WAAW,
|
|
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"}
|
|
@@ -55,6 +55,9 @@ const story = {
|
|
|
55
55
|
skusCap: 1000,
|
|
56
56
|
kpiItems: 3,
|
|
57
57
|
kpisRowGap: 8,
|
|
58
|
+
expandable: false,
|
|
59
|
+
expandLabel: 'Show more',
|
|
60
|
+
collapseLabel: 'Show less',
|
|
58
61
|
isLoading: false,
|
|
59
62
|
ratio: '1',
|
|
60
63
|
overlayActions: undefined,
|
|
@@ -158,3 +161,13 @@ WithOverlayActionsOnColoredCover.args = {
|
|
|
158
161
|
...WithColoredCover.args,
|
|
159
162
|
overlayActions: OverlayButtons,
|
|
160
163
|
};
|
|
164
|
+
export const ExpandableKpis = Template.bind({});
|
|
165
|
+
ExpandableKpis.args = {
|
|
166
|
+
kpiItems: 2,
|
|
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,
|
|
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 = [
|
|
@@ -102,4 +102,69 @@ describe('<ProductCard>', () => {
|
|
|
102
102
|
rerender(_jsx(ProductCard, { title: "title", bordered: false }));
|
|
103
103
|
expect(container.querySelector('[data-card-bordered="false"]')).not.toBeNull();
|
|
104
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
|
+
});
|
|
105
170
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wonderflow/react-components",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.3.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.
|
|
85
|
-
"@wonderflow/tokens": "17.
|
|
86
|
-
"@wonderflow/charts": "17.
|
|
84
|
+
"@wonderflow/config": "17.3.0",
|
|
85
|
+
"@wonderflow/tokens": "17.3.0",
|
|
86
|
+
"@wonderflow/charts": "17.3.0",
|
|
87
87
|
"@types/uuid": "9.0.8",
|
|
88
88
|
"babel-loader": "8.2.5",
|
|
89
89
|
"cpy-cli": "3.1.1",
|