cozy-ui 62.8.0 → 62.10.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/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ # [62.10.0](https://github.com/cozy/cozy-ui/compare/v62.9.1...v62.10.0) (2022-04-21)
2
+
3
+
4
+ ### Features
5
+
6
+ * Add chart.js and react-chartjs-2 packages ([16df033](https://github.com/cozy/cozy-ui/commit/16df033))
7
+ * Add PieChart component ([b7ce49f](https://github.com/cozy/cozy-ui/commit/b7ce49f))
8
+
9
+ ## [62.9.1](https://github.com/cozy/cozy-ui/compare/v62.9.0...v62.9.1) (2022-04-21)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * bump convict from 6.0.0 to 6.2.2 ([403c38e](https://github.com/cozy/cozy-ui/commit/403c38e))
15
+
16
+ # [62.9.0](https://github.com/cozy/cozy-ui/compare/v62.8.0...v62.9.0) (2022-04-20)
17
+
18
+
19
+ ### Features
20
+
21
+ * Add makeStyles helper from Mui ([7962227](https://github.com/cozy/cozy-ui/commit/7962227))
22
+
1
23
  # [62.8.0](https://github.com/cozy/cozy-ui/compare/v62.7.0...v62.8.0) (2022-04-19)
2
24
 
3
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cozy-ui",
3
- "version": "62.8.0",
3
+ "version": "62.10.0",
4
4
  "description": "Cozy apps UI SDK",
5
5
  "main": "./index.js",
6
6
  "bin": {
@@ -113,6 +113,7 @@
113
113
  "http-server": "0.13.0",
114
114
  "husky": "0.14.3",
115
115
  "identity-obj-proxy": "3.0.0",
116
+ "jest-canvas-mock": "2.3.1",
116
117
  "jest-cli": "^26.6.3",
117
118
  "kss": "3.0.1",
118
119
  "kss-webpack-plugin": "1.5.0",
@@ -153,6 +154,7 @@
153
154
  "dependencies": {
154
155
  "@babel/runtime": "^7.3.4",
155
156
  "@popperjs/core": "^2.4.4",
157
+ "chart.js": "3.7.1",
156
158
  "classnames": "^2.2.5",
157
159
  "cozy-interapp": "^0.5.4",
158
160
  "date-fns": "^1.28.5",
@@ -164,6 +166,7 @@
164
166
  "node-polyglot": "^2.2.2",
165
167
  "normalize.css": "^8.0.0",
166
168
  "piwik-react-router": "0.12.1",
169
+ "react-chartjs-2": "4.1.0",
167
170
  "react-markdown": "^4.0.8",
168
171
  "react-pdf": "^4.0.5",
169
172
  "react-popper": "^2.2.3",
@@ -200,7 +203,8 @@
200
203
  },
201
204
  "jest": {
202
205
  "setupFilesAfterEnv": [
203
- "./test/jestsetup.js"
206
+ "./test/jestsetup.js",
207
+ "jest-canvas-mock"
204
208
  ],
205
209
  "moduleFileExtensions": [
206
210
  "js",
@@ -0,0 +1,53 @@
1
+ #### PieChart
2
+
3
+ ```jsx
4
+ import Grid from 'cozy-ui/transpiled/react/MuiCozyTheme/Grid'
5
+ import PieChart from 'cozy-ui/transpiled/react/PieChart'
6
+
7
+ ;
8
+
9
+ <Grid container>
10
+ <Grid item xs={12} sm={6}>
11
+ <PieChart
12
+ single={true}
13
+ primaryText="60 %"
14
+ secondaryText="of the total"
15
+ data={{
16
+ labels: ['Health'],
17
+ datasets: [{ data: [60, 40] }]
18
+ }}
19
+ options={{
20
+ plugins: {
21
+ tooltip: {
22
+ callbacks: {
23
+ label: tooltipItems => `${tooltipItems.label}: ${tooltipItems.raw}%`
24
+ }
25
+ }
26
+ }
27
+ }}
28
+ />
29
+ </Grid>
30
+ <Grid item xs={12} sm={6}>
31
+ <PieChart
32
+ primaryText="105 €"
33
+ secondaryText="on the period"
34
+ data={{
35
+ labels: ['Earnings', 'Children', 'Sports', 'Housing', 'Health'],
36
+ datasets: [{
37
+ data: [40, 30, 20, 10, 5],
38
+ backgroundColor: ['#8978FF', '#FF7B5E', '#F85AA8', '#F1B61E', '#15CACD']
39
+ }]
40
+ }}
41
+ options={{
42
+ plugins: {
43
+ tooltip: {
44
+ callbacks: {
45
+ label: tooltipItems => `${tooltipItems.label}: ${tooltipItems.raw}€`
46
+ }
47
+ }
48
+ }
49
+ }}
50
+ />
51
+ </Grid>
52
+ </Grid>
53
+ ```
@@ -0,0 +1,141 @@
1
+ import React from 'react'
2
+ import PropTypes from 'prop-types'
3
+ import cx from 'classnames'
4
+ import { Chart as ChartJS, ArcElement, Tooltip } from 'chart.js'
5
+ import { Doughnut } from 'react-chartjs-2'
6
+ import { useTheme } from '@material-ui/core'
7
+ import { makeStyles } from '@material-ui/core/styles'
8
+ import set from 'lodash/set'
9
+ import cloneDeep from 'lodash/cloneDeep'
10
+
11
+ import Typography from '../Typography'
12
+ import isTesting from '../helpers/isTesting'
13
+
14
+ ChartJS.register(ArcElement, Tooltip)
15
+
16
+ const useStyles = makeStyles(theme => ({
17
+ container: {
18
+ position: 'relative',
19
+ zIndex: '1',
20
+ width: '192px',
21
+ height: '192px'
22
+ },
23
+ background: {
24
+ position: 'absolute',
25
+ boxSizing: 'border-box',
26
+ top: 0,
27
+ bottom: 0,
28
+ left: 0,
29
+ right: 0,
30
+ border: '24px solid var(--actionColorGhost)',
31
+ borderRadius: '100%',
32
+ zIndex: -1,
33
+ backgroundColor: ({ single }) =>
34
+ !single && theme.palette.type === 'dark'
35
+ ? theme.palette.primary.main
36
+ : 'none',
37
+ boxShadow: ({ single }) =>
38
+ !single && theme.palette.type === 'dark'
39
+ ? `0 0 0 2px ${theme.palette.primary.main}`
40
+ : 'none'
41
+ },
42
+ centerText: {
43
+ display: 'flex',
44
+ flexDirection: 'column',
45
+ position: 'absolute',
46
+ alignItems: 'center',
47
+ justifyContent: 'center',
48
+ textAlign: 'center',
49
+ top: '25px',
50
+ left: '25px',
51
+ right: '25px',
52
+ bottom: '25px',
53
+ borderRadius: '50%',
54
+ zIndex: '-1'
55
+ },
56
+ typography: {
57
+ color: ({ single }) =>
58
+ !single && theme.palette.type === 'dark'
59
+ ? theme.palette.primary.contrastText
60
+ : theme.palette.text.primary
61
+ }
62
+ }))
63
+
64
+ export const makeOptions = ({ options, single }) => {
65
+ const madeOptions = {
66
+ cutout: '75%',
67
+ elements: {
68
+ arc: {
69
+ borderWidth: 0
70
+ }
71
+ },
72
+ animation: isTesting() ? false : { duration: 1000 },
73
+ ...options
74
+ }
75
+ if (single) set(madeOptions, 'plugins.tooltip.filter', item => item.label)
76
+
77
+ return madeOptions
78
+ }
79
+
80
+ export const makeData = ({ data, single, theme }) => {
81
+ const madeData = cloneDeep(data)
82
+ if (single)
83
+ set(madeData, 'datasets[0].backgroundColor', [
84
+ theme.palette.primary.main,
85
+ 'transparent'
86
+ ])
87
+
88
+ return madeData
89
+ }
90
+
91
+ const PieChart = ({
92
+ data,
93
+ options = {},
94
+ primaryText,
95
+ secondaryText,
96
+ single = false,
97
+ className,
98
+ ...props
99
+ }) => {
100
+ const theme = useTheme()
101
+ const classes = useStyles({ single })
102
+ const madeOptions = makeOptions({ options, single })
103
+ const madeData = makeData({ data, single, theme })
104
+
105
+ return (
106
+ <div className={cx(classes.container, className)} {...props}>
107
+ <div className={classes.background} />
108
+ <Doughnut data={madeData} options={madeOptions} />
109
+ {(primaryText || secondaryText) && (
110
+ <div className={classes.centerText}>
111
+ {primaryText && (
112
+ <Typography classes={{ root: classes.typography }} variant="h3">
113
+ {primaryText}
114
+ </Typography>
115
+ )}
116
+ {secondaryText && (
117
+ <Typography classes={{ root: classes.typography }} variant="body2">
118
+ {secondaryText}
119
+ </Typography>
120
+ )}
121
+ </div>
122
+ )}
123
+ </div>
124
+ )
125
+ }
126
+
127
+ PieChart.propTypes = {
128
+ /** Data to be passed to chart.js graph */
129
+ data: PropTypes.object.isRequired,
130
+ /** Options to be passed to chart.js graph */
131
+ options: PropTypes.object,
132
+ /** Text to show as primary */
133
+ primaryText: PropTypes.string,
134
+ /** Text to show as secondary */
135
+ secondaryText: PropTypes.string,
136
+ /** Whether to use a single data */
137
+ single: PropTypes.bool,
138
+ className: PropTypes.string
139
+ }
140
+
141
+ export default PieChart
@@ -0,0 +1,123 @@
1
+ import { makeOptions, makeData } from './index'
2
+
3
+ describe('makeOptions', () => {
4
+ it('should return default options', () => {
5
+ const options = makeOptions({ options: {}, single: false })
6
+
7
+ expect(options).toStrictEqual({
8
+ cutout: '75%',
9
+ elements: {
10
+ arc: {
11
+ borderWidth: 0
12
+ }
13
+ },
14
+ animation: { duration: 1000 }
15
+ })
16
+ })
17
+
18
+ it('should return default options for a single pie chart', () => {
19
+ const options = makeOptions({ options: {}, single: true })
20
+
21
+ expect(options).toStrictEqual({
22
+ cutout: '75%',
23
+ elements: {
24
+ arc: {
25
+ borderWidth: 0
26
+ }
27
+ },
28
+ animation: { duration: 1000 },
29
+ plugins: { tooltip: { filter: expect.any(Function) } }
30
+ })
31
+ })
32
+
33
+ it('should return default and customized options', () => {
34
+ const options = makeOptions({ options: { foo: 'bar' }, single: false })
35
+
36
+ expect(options).toStrictEqual({
37
+ cutout: '75%',
38
+ elements: {
39
+ arc: {
40
+ borderWidth: 0
41
+ }
42
+ },
43
+ animation: { duration: 1000 },
44
+ foo: 'bar'
45
+ })
46
+ })
47
+
48
+ it('should return default and customized options for a single pie chart', () => {
49
+ const options = makeOptions({ options: { foo: 'bar' }, single: true })
50
+
51
+ expect(options).toStrictEqual({
52
+ cutout: '75%',
53
+ elements: {
54
+ arc: {
55
+ borderWidth: 0
56
+ }
57
+ },
58
+ animation: { duration: 1000 },
59
+ plugins: { tooltip: { filter: expect.any(Function) } },
60
+ foo: 'bar'
61
+ })
62
+ })
63
+
64
+ it('should return default and customized options for a single pie chart even for customized default option', () => {
65
+ const options = makeOptions({
66
+ options: { plugins: { tooltip: { foo: 'bar' } } },
67
+ single: true
68
+ })
69
+
70
+ expect(options).toStrictEqual({
71
+ cutout: '75%',
72
+ elements: {
73
+ arc: {
74
+ borderWidth: 0
75
+ }
76
+ },
77
+ animation: { duration: 1000 },
78
+ plugins: {
79
+ tooltip: {
80
+ foo: 'bar',
81
+ filter: expect.any(Function)
82
+ }
83
+ }
84
+ })
85
+ })
86
+ })
87
+
88
+ describe('makeData', () => {
89
+ it('should return previous data is not a single pie chart', () => {
90
+ const prevData = { labels: ['Health'] }
91
+ const data = makeData({
92
+ data: prevData,
93
+ single: false,
94
+ theme: { palette: { primary: { main: '#fff' } } }
95
+ })
96
+
97
+ expect(data).toStrictEqual(prevData)
98
+ })
99
+
100
+ it('should add backgroud color for a single pie chart', () => {
101
+ const data = makeData({
102
+ data: { labels: ['Health'] },
103
+ single: true,
104
+ theme: { palette: { primary: { main: '#fff' } } }
105
+ })
106
+
107
+ expect(data).toStrictEqual({
108
+ labels: ['Health'],
109
+ datasets: [{ backgroundColor: ['#fff', 'transparent'] }]
110
+ })
111
+ })
112
+
113
+ it('should not mutate previous data for a single pie chart', () => {
114
+ const prevData = { labels: ['Health'] }
115
+ makeData({
116
+ data: prevData,
117
+ single: true,
118
+ theme: { palette: { primary: { main: '#fff' } } }
119
+ })
120
+
121
+ expect(prevData).toStrictEqual({ labels: ['Health'] })
122
+ })
123
+ })
@@ -7577,13 +7577,13 @@ exports[`Media should render examples: Media 3`] = `
7577
7577
  exports[`MuiCozyTheme/Switch should render examples: MuiCozyTheme/Switch 1`] = `
7578
7578
  "<div>
7579
7579
  <div class=\\"styles__Stack--xs___2R5lW\\">
7580
- <div class=\\"MuiBox-root MuiBox-root-16\\">
7580
+ <div class=\\"MuiBox-root MuiBox-root-24\\">
7581
7581
  <p class=\\"MuiTypography-root MuiTypography-body1 MuiTypography-displayInline\\">primary</p><span class=\\"MuiSwitch-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiSwitch-switchBase MuiSwitch-colorPrimary PrivateSwitchBase-checked-2 Mui-checked\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4 MuiSwitch-input\\" type=\\"checkbox\\" value=\\"\\" checked=\\"\\"><span class=\\"MuiSwitch-thumb\\"></span></span></span><span class=\\"MuiSwitch-track\\"></span></span>
7582
7582
  </div>
7583
- <div class=\\"MuiBox-root MuiBox-root-17\\">
7583
+ <div class=\\"MuiBox-root MuiBox-root-25\\">
7584
7584
  <p class=\\"MuiTypography-root MuiTypography-body1 MuiTypography-displayInline\\">secondary</p><span class=\\"MuiSwitch-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiSwitch-switchBase MuiSwitch-colorSecondary PrivateSwitchBase-checked-2 Mui-checked\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4 MuiSwitch-input\\" type=\\"checkbox\\" value=\\"\\" checked=\\"\\"><span class=\\"MuiSwitch-thumb\\"></span></span></span><span class=\\"MuiSwitch-track\\"></span></span>
7585
7585
  </div>
7586
- <div class=\\"MuiBox-root MuiBox-root-18\\">
7586
+ <div class=\\"MuiBox-root MuiBox-root-26\\">
7587
7587
  <p class=\\"MuiTypography-root MuiTypography-body1 MuiTypography-displayInline\\">default</p><span class=\\"MuiSwitch-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiSwitch-switchBase\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4 MuiSwitch-input\\" type=\\"checkbox\\" value=\\"\\"><span class=\\"MuiSwitch-thumb\\"></span></span></span><span class=\\"MuiSwitch-track\\"></span></span>
7588
7588
  </div>
7589
7589
  </div>
@@ -7683,6 +7683,31 @@ exports[`PercentageLine should render examples: PercentageLine 1`] = `
7683
7683
  </div>"
7684
7684
  `;
7685
7685
 
7686
+ exports[`PieChart should render examples: PieChart 1`] = `
7687
+ "<div>
7688
+ <div class=\\"MuiGrid-root MuiGrid-container\\">
7689
+ <div class=\\"MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-6\\">
7690
+ <div class=\\"makeStyles-container-14\\">
7691
+ <div class=\\"makeStyles-background-15 makeStyles-background-18\\"></div><canvas role=\\"img\\" height=\\"150\\" width=\\"300\\"></canvas>
7692
+ <div class=\\"makeStyles-centerText-16\\">
7693
+ <h3 class=\\"MuiTypography-root makeStyles-typography-17 makeStyles-typography-19 MuiTypography-h3\\">60 %</h3>
7694
+ <p class=\\"MuiTypography-root makeStyles-typography-17 makeStyles-typography-19 MuiTypography-body2\\">of the total</p>
7695
+ </div>
7696
+ </div>
7697
+ </div>
7698
+ <div class=\\"MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-6\\">
7699
+ <div class=\\"makeStyles-container-14\\">
7700
+ <div class=\\"makeStyles-background-15 makeStyles-background-20\\"></div><canvas role=\\"img\\" height=\\"150\\" width=\\"300\\"></canvas>
7701
+ <div class=\\"makeStyles-centerText-16\\">
7702
+ <h3 class=\\"MuiTypography-root makeStyles-typography-17 makeStyles-typography-21 MuiTypography-h3\\">105 €</h3>
7703
+ <p class=\\"MuiTypography-root makeStyles-typography-17 makeStyles-typography-21 MuiTypography-body2\\">on the period</p>
7704
+ </div>
7705
+ </div>
7706
+ </div>
7707
+ </div>
7708
+ </div>"
7709
+ `;
7710
+
7686
7711
  exports[`Progress should render examples: Progress 1`] = `
7687
7712
  "<div>
7688
7713
  <div class=\\"MuiPaper-root u-p-1 u-mb-1 MuiPaper-elevation1\\">
@@ -7703,7 +7728,7 @@ exports[`ProgressionBanner should render examples: ProgressionBanner 1`] = `
7703
7728
  <h5 class=\\"MuiTypography-root u-mb-1 MuiTypography-h5\\">Variant selector</h5><label class=\\"MuiFormControlLabel-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiCheckbox-root MuiCheckbox-colorPrimary PrivateSwitchBase-checked-2 Mui-checked MuiIconButton-colorPrimary\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4\\" type=\\"checkbox\\" data-indeterminate=\\"false\\" aria-checked=\\"\\" value=\\"\\" checked=\\"\\"><svg class=\\"MuiSvgIcon-root\\" focusable=\\"false\\" viewBox=\\"0 0 24 24\\" aria-hidden=\\"true\\"><path d=\\"M19 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.11 0 2-.9 2-2V5c0-1.1-.89-2-2-2zm-9 14l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z\\"></path></svg></span></span><span class=\\"MuiTypography-root MuiFormControlLabel-label MuiTypography-body1\\">WITHVALUE</span></label><label class=\\"MuiFormControlLabel-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiCheckbox-root MuiCheckbox-colorPrimary PrivateSwitchBase-checked-2 Mui-checked MuiIconButton-colorPrimary\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4\\" type=\\"checkbox\\" data-indeterminate=\\"false\\" aria-checked=\\"\\" value=\\"\\" checked=\\"\\"><svg class=\\"MuiSvgIcon-root\\" focusable=\\"false\\" viewBox=\\"0 0 24 24\\" aria-hidden=\\"true\\"><path d=\\"M19 3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.11 0 2-.9 2-2V5c0-1.1-.89-2-2-2zm-9 14l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z\\"></path></svg></span></span><span class=\\"MuiTypography-root MuiFormControlLabel-label MuiTypography-body1\\">PROGRESSBAR</span></label><label class=\\"MuiFormControlLabel-root\\"><span class=\\"MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiCheckbox-root MuiCheckbox-colorPrimary MuiIconButton-colorPrimary\\" aria-disabled=\\"false\\"><span class=\\"MuiIconButton-label\\"><input class=\\"PrivateSwitchBase-input-4\\" type=\\"checkbox\\" data-indeterminate=\\"false\\" aria-checked=\\"\\" value=\\"\\"><svg class=\\"MuiSvgIcon-root\\" focusable=\\"false\\" viewBox=\\"0 0 24 24\\" aria-hidden=\\"true\\"><path d=\\"M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z\\"></path></svg></span></span><span class=\\"MuiTypography-root MuiFormControlLabel-label MuiTypography-body1\\">WITHBUTTON</span></label>
7704
7729
  </div>
7705
7730
  <div class=\\"MuiPaper-root MuiPaper-elevation0\\">
7706
- <div class=\\"styles__c-banner-wrapper___3KlaG Component-banner-14\\">
7731
+ <div class=\\"styles__c-banner-wrapper___3KlaG Component-banner-22\\">
7707
7732
  <div class=\\"MuiGrid-root MuiGrid-container MuiGrid-justify-content-xs-space-between\\">
7708
7733
  <div class=\\"MuiGrid-root MuiGrid-container MuiGrid-item MuiGrid-wrap-xs-nowrap MuiGrid-align-items-xs-center MuiGrid-grid-xs-12 MuiGrid-grid-lg-8\\">
7709
7734
  <div class=\\"MuiGrid-root MuiGrid-item\\">
@@ -7722,7 +7747,7 @@ exports[`ProgressionBanner should render examples: ProgressionBanner 1`] = `
7722
7747
  </div>
7723
7748
  </div>
7724
7749
  <hr class=\\"MuiDivider-root\\">
7725
- <div class=\\"MuiLinearProgress-root MuiLinearProgress-colorPrimary Component-progress-15 MuiLinearProgress-indeterminate\\" role=\\"progressbar\\">
7750
+ <div class=\\"MuiLinearProgress-root MuiLinearProgress-colorPrimary Component-progress-23 MuiLinearProgress-indeterminate\\" role=\\"progressbar\\">
7726
7751
  <div class=\\"MuiLinearProgress-bar MuiLinearProgress-barColorPrimary MuiLinearProgress-bar1Indeterminate\\"></div>
7727
7752
  <div class=\\"MuiLinearProgress-bar MuiLinearProgress-bar2Indeterminate MuiLinearProgress-barColorPrimary\\"></div>
7728
7753
  </div>
@@ -60,6 +60,7 @@ testComponent('Paper')
60
60
  testComponent('PasswordExample')
61
61
  testComponent('PercentageBar')
62
62
  testComponent('PercentageLine')
63
+ testComponent('PieChart')
63
64
  testComponent('Progress')
64
65
  testComponent('ProgressionBanner')
65
66
  testComponent('QuotaAlert')
@@ -0,0 +1,2 @@
1
+ import { makeStyles } from '@material-ui/core/styles'
2
+ export default makeStyles
package/react/index.js CHANGED
@@ -28,6 +28,7 @@ export { default as AccordionSummary } from './MuiCozyTheme/AccordionSummary'
28
28
  export { default as AccordionDetails } from './MuiCozyTheme/AccordionDetails'
29
29
  export { default as Toggle } from './Toggle'
30
30
  export { default as withBreakpoints } from './helpers/withBreakpoints'
31
+ export { default as makeStyles } from './helpers/makeStyles'
31
32
  export { default as useBreakpoints } from './hooks/useBreakpoints'
32
33
  export { default as useBrowserOffline } from './hooks/useBrowserOffline'
33
34
  export { Media, Img, Bd } from './Media'
@@ -102,3 +103,4 @@ export { default as Radios } from './Radios'
102
103
  export { default as BottomSheet, BottomSheetItem } from './BottomSheet'
103
104
  export { default as FilePicker } from './FilePicker'
104
105
  export { default as Chips } from './Chips'
106
+ export { default as PieChart } from './PieChart'
@@ -0,0 +1,164 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ var _excluded = ["data", "options", "primaryText", "secondaryText", "single", "className"];
5
+
6
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
7
+
8
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
9
+
10
+ import React from 'react';
11
+ import PropTypes from 'prop-types';
12
+ import cx from 'classnames';
13
+ import { Chart as ChartJS, ArcElement, Tooltip } from 'chart.js';
14
+ import { Doughnut } from 'react-chartjs-2';
15
+ import { useTheme } from '@material-ui/core';
16
+ import { makeStyles } from '@material-ui/core/styles';
17
+ import set from 'lodash/set';
18
+ import cloneDeep from 'lodash/cloneDeep';
19
+ import Typography from "cozy-ui/transpiled/react/Typography";
20
+ import isTesting from "cozy-ui/transpiled/react/helpers/isTesting";
21
+ ChartJS.register(ArcElement, Tooltip);
22
+ var useStyles = makeStyles(function (theme) {
23
+ return {
24
+ container: {
25
+ position: 'relative',
26
+ zIndex: '1',
27
+ width: '192px',
28
+ height: '192px'
29
+ },
30
+ background: {
31
+ position: 'absolute',
32
+ boxSizing: 'border-box',
33
+ top: 0,
34
+ bottom: 0,
35
+ left: 0,
36
+ right: 0,
37
+ border: '24px solid var(--actionColorGhost)',
38
+ borderRadius: '100%',
39
+ zIndex: -1,
40
+ backgroundColor: function backgroundColor(_ref) {
41
+ var single = _ref.single;
42
+ return !single && theme.palette.type === 'dark' ? theme.palette.primary.main : 'none';
43
+ },
44
+ boxShadow: function boxShadow(_ref2) {
45
+ var single = _ref2.single;
46
+ return !single && theme.palette.type === 'dark' ? "0 0 0 2px ".concat(theme.palette.primary.main) : 'none';
47
+ }
48
+ },
49
+ centerText: {
50
+ display: 'flex',
51
+ flexDirection: 'column',
52
+ position: 'absolute',
53
+ alignItems: 'center',
54
+ justifyContent: 'center',
55
+ textAlign: 'center',
56
+ top: '25px',
57
+ left: '25px',
58
+ right: '25px',
59
+ bottom: '25px',
60
+ borderRadius: '50%',
61
+ zIndex: '-1'
62
+ },
63
+ typography: {
64
+ color: function color(_ref3) {
65
+ var single = _ref3.single;
66
+ return !single && theme.palette.type === 'dark' ? theme.palette.primary.contrastText : theme.palette.text.primary;
67
+ }
68
+ }
69
+ };
70
+ });
71
+ export var makeOptions = function makeOptions(_ref4) {
72
+ var options = _ref4.options,
73
+ single = _ref4.single;
74
+
75
+ var madeOptions = _objectSpread({
76
+ cutout: '75%',
77
+ elements: {
78
+ arc: {
79
+ borderWidth: 0
80
+ }
81
+ },
82
+ animation: isTesting() ? false : {
83
+ duration: 1000
84
+ }
85
+ }, options);
86
+
87
+ if (single) set(madeOptions, 'plugins.tooltip.filter', function (item) {
88
+ return item.label;
89
+ });
90
+ return madeOptions;
91
+ };
92
+ export var makeData = function makeData(_ref5) {
93
+ var data = _ref5.data,
94
+ single = _ref5.single,
95
+ theme = _ref5.theme;
96
+ var madeData = cloneDeep(data);
97
+ if (single) set(madeData, 'datasets[0].backgroundColor', [theme.palette.primary.main, 'transparent']);
98
+ return madeData;
99
+ };
100
+
101
+ var PieChart = function PieChart(_ref6) {
102
+ var data = _ref6.data,
103
+ _ref6$options = _ref6.options,
104
+ options = _ref6$options === void 0 ? {} : _ref6$options,
105
+ primaryText = _ref6.primaryText,
106
+ secondaryText = _ref6.secondaryText,
107
+ _ref6$single = _ref6.single,
108
+ single = _ref6$single === void 0 ? false : _ref6$single,
109
+ className = _ref6.className,
110
+ props = _objectWithoutProperties(_ref6, _excluded);
111
+
112
+ var theme = useTheme();
113
+ var classes = useStyles({
114
+ single: single
115
+ });
116
+ var madeOptions = makeOptions({
117
+ options: options,
118
+ single: single
119
+ });
120
+ var madeData = makeData({
121
+ data: data,
122
+ single: single,
123
+ theme: theme
124
+ });
125
+ return /*#__PURE__*/React.createElement("div", _extends({
126
+ className: cx(classes.container, className)
127
+ }, props), /*#__PURE__*/React.createElement("div", {
128
+ className: classes.background
129
+ }), /*#__PURE__*/React.createElement(Doughnut, {
130
+ data: madeData,
131
+ options: madeOptions
132
+ }), (primaryText || secondaryText) && /*#__PURE__*/React.createElement("div", {
133
+ className: classes.centerText
134
+ }, primaryText && /*#__PURE__*/React.createElement(Typography, {
135
+ classes: {
136
+ root: classes.typography
137
+ },
138
+ variant: "h3"
139
+ }, primaryText), secondaryText && /*#__PURE__*/React.createElement(Typography, {
140
+ classes: {
141
+ root: classes.typography
142
+ },
143
+ variant: "body2"
144
+ }, secondaryText)));
145
+ };
146
+
147
+ PieChart.propTypes = {
148
+ /** Data to be passed to chart.js graph */
149
+ data: PropTypes.object.isRequired,
150
+
151
+ /** Options to be passed to chart.js graph */
152
+ options: PropTypes.object,
153
+
154
+ /** Text to show as primary */
155
+ primaryText: PropTypes.string,
156
+
157
+ /** Text to show as secondary */
158
+ secondaryText: PropTypes.string,
159
+
160
+ /** Whether to use a single data */
161
+ single: PropTypes.bool,
162
+ className: PropTypes.string
163
+ };
164
+ export default PieChart;
@@ -0,0 +1,2 @@
1
+ import { makeStyles } from '@material-ui/core/styles';
2
+ export default makeStyles;
@@ -21,6 +21,7 @@ export { default as AccordionSummary } from './MuiCozyTheme/AccordionSummary';
21
21
  export { default as AccordionDetails } from './MuiCozyTheme/AccordionDetails';
22
22
  export { default as Toggle } from './Toggle';
23
23
  export { default as withBreakpoints } from './helpers/withBreakpoints';
24
+ export { default as makeStyles } from './helpers/makeStyles';
24
25
  export { default as useBreakpoints } from './hooks/useBreakpoints';
25
26
  export { default as useBrowserOffline } from './hooks/useBrowserOffline';
26
27
  export { Media, Img, Bd } from './Media';
@@ -79,4 +80,5 @@ export { default as FileImageLoader } from './FileImageLoader';
79
80
  export { default as Radios } from './Radios';
80
81
  export { default as BottomSheet, BottomSheetItem } from './BottomSheet';
81
82
  export { default as FilePicker } from './FilePicker';
82
- export { default as Chips } from './Chips';
83
+ export { default as Chips } from './Chips';
84
+ export { default as PieChart } from './PieChart';