Skip to content

Commit 6ae850f

Browse files
refactor(MicroBarChart): replace react-jss with css module (#5719)
Co-authored-by: Marcus Notheis <marcus.notheis@sap.com>
1 parent ede029e commit 6ae850f

File tree

3 files changed

+71
-64
lines changed

3 files changed

+71
-64
lines changed

packages/charts/src/components/MicroBarChart/MicroBarChart.cy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('MicroBarChart', () => {
2323
it('Basic', () => {
2424
cy.mount(<MicroBarChart dataset={dataset} dimension={dimension} measure={measure} />);
2525
cy.get('.recharts-responsive-container').should('be.visible');
26-
cy.get('div[class^=MicroBarChart-valueBar]').should('have.length', 3);
26+
cy.get('[data-component-name="MicroBarChartValueBar"]').should('have.length', 3);
2727

2828
cy.findByText(text1).should('be.visible');
2929
cy.findByText(text2).should('be.visible');
@@ -39,7 +39,7 @@ describe('MicroBarChart', () => {
3939
/>
4040
);
4141
cy.get('.recharts-responsive-container').should('be.visible');
42-
cy.get('div[class^=MicroBarChart-valueBar]').should('have.length', 3);
42+
cy.get('[data-component-name="MicroBarChartValueBar"]').should('have.length', 3);
4343

4444
cy.findByText(`${text1} - formatted`).should('be.visible');
4545
cy.findByText(`${text2} - formatted`).should('be.visible');
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
.container {
2+
display: flex;
3+
flex-direction: column;
4+
overflow: hidden;
5+
font-family: var(--sapFontFamily);
6+
font-weight: normal;
7+
width: 100%;
8+
height: 100%;
9+
justify-content: space-around;
10+
}
11+
12+
.barContainer {
13+
cursor: auto;
14+
}
15+
16+
.barContainerActive {
17+
cursor: pointer;
18+
19+
&:active {
20+
opacity: 0.3 !important;
21+
}
22+
}
23+
24+
.labelContainer {
25+
display: flex;
26+
justify-content: space-between;
27+
}
28+
29+
.valueContainer {
30+
display: flex;
31+
background-color: var(--sapContent_Placeholderloading_Background);
32+
}
33+
34+
.valueBar {
35+
height: 100%;
36+
}
37+
38+
.label {
39+
color: var(--sapContent_LabelColor);
40+
display: block;
41+
overflow: hidden;
42+
white-space: nowrap;
43+
text-overflow: ellipsis;
44+
font-size: var(--sapFontSmallSize);
45+
max-width: 70%;
46+
}
47+
48+
.text {
49+
padding-inline-start: 6px;
50+
display: inline-block;
51+
overflow: hidden;
52+
font-size: var(--sapFontSmallSize);
53+
box-sizing: border-box;
54+
white-space: nowrap;
55+
text-overflow: ellipsis;
56+
color: var(--sapTextColor);
57+
text-align: right;
58+
}

packages/charts/src/components/MicroBarChart/MicroBarChart.tsx

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use client';
22

3-
import { enrichEventWithDetails, ThemingParameters } from '@ui5/webcomponents-react-base';
3+
import { enrichEventWithDetails, ThemingParameters, useStylesheet } from '@ui5/webcomponents-react-base';
44
import { clsx } from 'clsx';
55
import type { CSSProperties } from 'react';
66
import React, { createElement, forwardRef, useCallback, useMemo } from 'react';
7-
import { createUseStyles } from 'react-jss';
87
import { getValueByDataKey } from 'recharts/lib/util/ChartUtils.js';
98
import type { IChartBaseProps } from '../../interfaces/IChartBaseProps.js';
109
import type { IChartDimension } from '../../interfaces/IChartDimension.js';
1110
import type { IChartMeasure } from '../../interfaces/IChartMeasure.js';
1211
import { ChartContainer } from '../../internal/ChartContainer.js';
1312
import { defaultFormatter } from '../../internal/defaults.js';
1413
import { BarChartPlaceholder } from '../BarChart/Placeholder.js';
14+
import { classNames, styleData } from './MicroBarChart.module.css.js';
1515

1616
interface MeasureConfig extends Omit<IChartMeasure, 'color'> {
1717
/**
@@ -86,65 +86,13 @@ const resolveColor = (index: number, color = null) => {
8686
return ThemingParameters[`sapChart_Sequence_${(index % 11) + 1}`];
8787
};
8888

89-
const MicroBarChartStyles = {
90-
container: {
91-
display: 'flex',
92-
flexDirection: 'column',
93-
overflow: 'hidden',
94-
fontFamily: ThemingParameters.sapFontFamily,
95-
fontWeight: 'normal',
96-
width: '100%',
97-
height: '100%',
98-
justifyContent: 'space-around'
99-
},
100-
barContainer: {
101-
cursor: 'auto'
102-
},
103-
barContainerActive: {
104-
'&:active': { opacity: '0.3 !important' },
105-
cursor: 'pointer'
106-
},
107-
labelContainer: {
108-
display: 'flex',
109-
justifyContent: 'space-between'
110-
},
111-
valueContainer: {
112-
display: 'flex',
113-
backgroundColor: ThemingParameters.sapContent_Placeholderloading_Background
114-
},
115-
valueBar: { height: '100%' },
116-
label: {
117-
color: ThemingParameters.sapContent_LabelColor,
118-
display: 'block',
119-
overflow: 'hidden',
120-
whiteSpace: 'nowrap',
121-
textOverflow: 'ellipsis',
122-
fontSize: ThemingParameters.sapFontSmallSize,
123-
maxWidth: '70%'
124-
},
125-
text: {
126-
paddingLeft: '6px',
127-
display: 'inline-block',
128-
overflow: 'hidden',
129-
fontSize: ThemingParameters.sapFontSmallSize,
130-
boxSizing: 'border-box',
131-
132-
whiteSpace: 'nowrap',
133-
textOverflow: 'ellipsis',
134-
color: ThemingParameters.sapTextColor,
135-
textAlign: 'right'
136-
}
137-
};
138-
139-
const useStyles = createUseStyles(MicroBarChartStyles, { name: 'MicroBarChart' });
140-
14189
/**
14290
* The `MicroBarChart` compares different values of the same category to each other by displaying them in a compact way.
14391
*/
14492
const MicroBarChart = forwardRef<HTMLDivElement, MicroBarChartProps>((props, ref) => {
14593
const { loading, dataset, onDataPointClick, style, className, slot, ChartPlaceholder, ...rest } = props;
14694

147-
const classes = useStyles();
95+
useStylesheet(styleData, MicroBarChart.displayName);
14896

14997
const dimension = useMemo<IChartDimension>(
15098
() => ({
@@ -186,7 +134,7 @@ const MicroBarChart = forwardRef<HTMLDivElement, MicroBarChartProps>((props, ref
186134
},
187135
[measure.accessor, onDataPointClick]
188136
);
189-
const barContainerClasses = clsx(classes.barContainer, onDataPointClick && classes.barContainerActive);
137+
const barContainerClasses = clsx(classNames.barContainer, onDataPointClick && classNames.barContainerActive);
190138
const { maxValue: _0, dimension: _1, measure: _2, ...propsWithoutOmitted } = rest;
191139
return (
192140
<ChartContainer
@@ -200,7 +148,7 @@ const MicroBarChart = forwardRef<HTMLDivElement, MicroBarChartProps>((props, ref
200148
resizeDebounce={250}
201149
{...propsWithoutOmitted}
202150
>
203-
<div className={classes.container}>
151+
<div className={classNames.container}>
204152
{dataset?.map((item, index) => {
205153
const dimensionValue = getValueByDataKey(item, dimension.accessor);
206154
const measureValue = getValueByDataKey(item, measure.accessor);
@@ -219,23 +167,24 @@ const MicroBarChart = forwardRef<HTMLDivElement, MicroBarChartProps>((props, ref
219167
}
220168
return (
221169
<div key={dimensionValue} className={barContainerClasses} onClick={onBarClick(item, index)}>
222-
<div className={classes.labelContainer}>
223-
<span className={classes.label} title={formattedDimension}>
170+
<div className={classNames.labelContainer}>
171+
<span className={classNames.label} title={formattedDimension}>
224172
{formattedDimension}
225173
</span>
226-
<span className={classes.text} title={formattedMeasure}>
174+
<span className={classNames.text} title={formattedMeasure}>
227175
{measure.hideDataLabel ? '' : formattedMeasure}
228176
</span>
229177
</div>
230178
<div
231-
className={classes.valueContainer}
179+
className={classNames.valueContainer}
232180
style={{
233181
opacity: measure?.opacity ?? 1,
234182
height: barHeight
235183
}}
236184
>
237185
<div
238-
className={classes.valueBar}
186+
className={classNames.valueBar}
187+
data-component-name="MicroBarChartValueBar"
239188
style={{
240189
width: `${(measureValue / maxValue) * 100}%`,
241190
backgroundColor: resolveColor(index, measure?.colors?.[index])

0 commit comments

Comments
 (0)