Skip to content

Commit a79fab9

Browse files
TheZokersdirix
authored andcommitted
Fix rating style and refactor useEffect to useMemo
1 parent 59e9d2c commit a79fab9

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

src/App.tsx

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fragment, useState, useEffect } from 'react';
1+
import { Fragment, useState, useMemo } from 'react';
22
import { JsonForms } from '@jsonforms/react';
33
import Grid from '@mui/material/Grid';
44
import Button from '@mui/material/Button';
@@ -57,15 +57,11 @@ const renderers = [
5757

5858
const App = () => {
5959
const classes = useStyles();
60-
const [displayDataAsString, setDisplayDataAsString] = useState('');
61-
const [jsonformsData, setJsonformsData] = useState<any>(initialData);
62-
63-
useEffect(() => {
64-
setDisplayDataAsString(JSON.stringify(jsonformsData, null, 2));
65-
}, [jsonformsData]);
60+
const [data, setData] = useState<any>(initialData);
61+
const stringifiedData = useMemo(() => JSON.stringify(data, null, 2), [data]);
6662

6763
const clearData = () => {
68-
setJsonformsData({});
64+
setData({});
6965
};
7066

7167
return (
@@ -89,7 +85,7 @@ const App = () => {
8985
Bound data
9086
</Typography>
9187
<div className={classes.dataContent}>
92-
<pre id='boundData'>{displayDataAsString}</pre>
88+
<pre id='boundData'>{stringifiedData}</pre>
9389
</div>
9490
<Button
9591
className={classes.resetButton}
@@ -108,10 +104,10 @@ const App = () => {
108104
<JsonForms
109105
schema={schema}
110106
uischema={uischema}
111-
data={jsonformsData}
107+
data={data}
112108
renderers={renderers}
113109
cells={materialCells}
114-
onChange={({ errors, data }) => setJsonformsData(data)}
110+
onChange={({ errors, data }) => setData(data)}
115111
/>
116112
</div>
117113
</Grid>

src/Rating.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { useState } from 'react';
3+
import { InputLabel } from '@mui/material';
34

45
interface RatingProps {
56
id?: string;
@@ -12,8 +13,8 @@ export const Rating: React.FC<RatingProps> = ({ id, value, updateValue }) => {
1213

1314
return (
1415
<div id='#/properties/rating' className='rating'>
15-
<p>Rating:</p>
16-
<span style={{ cursor: 'pointer' }}>
16+
<InputLabel shrink style={{ marginTop: '0.8em' }}>Rating</InputLabel>
17+
<div style={{ cursor: 'pointer', fontSize: '18px' }}>
1718
{[0, 1, 2, 3, 4].map((i) => {
1819
const fullStars = hoverAt ?? value;
1920

@@ -28,7 +29,7 @@ export const Rating: React.FC<RatingProps> = ({ id, value, updateValue }) => {
2829
</span>
2930
);
3031
})}
31-
</span>
32+
</div>
3233
</div>
3334
);
3435
};

0 commit comments

Comments
 (0)