Skip to content

Commit 25009b4

Browse files
Merge pull request #1929 from carbon-design-system/value-card-precision-editor
fix(editor): value cards need to set precision at the attribute level
2 parents 2dbce28 + b6c9275 commit 25009b4

5 files changed

Lines changed: 69 additions & 231 deletions

File tree

src/components/CardEditor/CardEditForm/CardEditFormItems/DataSeriesFormItemModal.jsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ const defaultProps = {
115115
source: 'Source data item',
116116
primaryButtonLabelText: 'Save',
117117
secondaryButtonLabelText: 'Cancel',
118+
example: 'Example',
119+
decimalPlaces: 'Decimal places',
118120
},
119121
editDataSeries: [],
120122
showEditor: false,
@@ -273,11 +275,14 @@ const DataSeriesFormItemModal = ({
273275
value={editDataItem.label}
274276
/>
275277
</div>
276-
<div className={`${baseClassName}--input-group--item-end`}>
278+
</div>
279+
<div className={`${baseClassName}--input-group`}>
280+
<div className={`${baseClassName}--input-group--item`}>
277281
<TextInput
278282
id={`${id}_attribute-unit`}
279283
labelText={mergedI18n.dataItemEditorDataItemUnit}
280284
light
285+
placeholder={`${mergedI18n.example}: %`}
281286
onChange={(evt) =>
282287
setEditDataItem({
283288
...editDataItem,
@@ -287,6 +292,33 @@ const DataSeriesFormItemModal = ({
287292
value={editDataItem.unit}
288293
/>
289294
</div>
295+
{cardConfig.type === CARD_TYPES.VALUE && (
296+
<div className={`${baseClassName}--input-group--item-end`}>
297+
<Dropdown
298+
id={`${id}_value-card-decimal-place`}
299+
titleText={mergedI18n.decimalPlaces}
300+
direction="bottom"
301+
label=""
302+
items={[mergedI18n.notSet, '0', '1', '2', '3', '4']}
303+
light
304+
translateWithId={handleTranslation}
305+
selectedItem={
306+
editDataItem.precision?.toString() || mergedI18n.notSet
307+
}
308+
onChange={({ selectedItem }) => {
309+
const isSet = selectedItem !== mergedI18n.notSet;
310+
if (isSet) {
311+
setEditDataItem({
312+
...editDataItem,
313+
precision: Number(selectedItem),
314+
});
315+
} else {
316+
setEditDataItem(omit(editDataItem, 'precision'));
317+
}
318+
}}
319+
/>
320+
</div>
321+
)}
290322
</div>
291323
<div className={`${baseClassName}--input-group`}>
292324
<div

src/components/CardEditor/CardEditForm/CardEditFormItems/DataSeriesFormItemModal.test.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ describe('DataSeriesFormItemModal', () => {
4848
{
4949
dataSourceId: 'key1',
5050
unit: '%',
51+
precision: 3,
5152
label: 'Key 1',
5253
},
5354
{
5455
dataSourceId: 'key2',
5556
unit: 'lb',
5657
label: 'Key 2',
58+
precision: 3,
5759
},
5860
],
5961
},
@@ -76,6 +78,7 @@ describe('DataSeriesFormItemModal', () => {
7678
const editValueDataItem = {
7779
dataSourceId: 'key1',
7880
unit: '%',
81+
precision: 3,
7982
label: 'Key 1',
8083
};
8184

@@ -144,6 +147,7 @@ describe('DataSeriesFormItemModal', () => {
144147
expect(mockSetEditDataItem).toHaveBeenCalledWith({
145148
dataSourceId: 'key1',
146149
label: 'newLabel',
150+
precision: 3,
147151
unit: '%',
148152
});
149153
});
@@ -166,9 +170,36 @@ describe('DataSeriesFormItemModal', () => {
166170
expect(mockSetEditDataItem).toHaveBeenCalledWith({
167171
dataSourceId: 'key1',
168172
label: 'Key 1',
173+
precision: 3,
169174
unit: 'PSI',
170175
});
171176
});
177+
it('changes precision in a valueCard', () => {
178+
render(
179+
<DataSeriesFormItemModal
180+
{...commonProps}
181+
showEditor
182+
cardConfig={valueCardConfig}
183+
editDataItem={editValueDataItem}
184+
/>
185+
);
186+
187+
const precisionInput = screen.getByText('3');
188+
expect(precisionInput).toBeInTheDocument();
189+
fireEvent.click(precisionInput);
190+
191+
const precisionOption = screen.getByText('4');
192+
expect(precisionOption).toBeInTheDocument();
193+
194+
fireEvent.click(precisionOption);
195+
196+
expect(mockSetEditDataItem).toHaveBeenCalledWith({
197+
dataSourceId: 'key1',
198+
unit: '%',
199+
precision: 4,
200+
label: 'Key 1',
201+
});
202+
});
172203
it('Changes dataFilter in a ValueCard', () => {
173204
render(
174205
<DataSeriesFormItemModal
@@ -195,6 +226,7 @@ describe('DataSeriesFormItemModal', () => {
195226
},
196227
dataSourceId: 'key1',
197228
label: 'Key 1',
229+
precision: 3,
198230
unit: '%',
199231
});
200232
});
@@ -210,6 +242,7 @@ describe('DataSeriesFormItemModal', () => {
210242
},
211243
dataSourceId: 'key1',
212244
label: 'Key 1',
245+
precision: 3,
213246
unit: '%',
214247
}}
215248
/>
@@ -231,6 +264,7 @@ describe('DataSeriesFormItemModal', () => {
231264
},
232265
dataSourceId: 'key1',
233266
label: 'Key 1',
267+
precision: 3,
234268
unit: '%',
235269
});
236270
});

src/components/CardEditor/CardEditForm/CardEditFormItems/ValueCardFormItems/ValueCardFormSettings.jsx

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import omit from 'lodash/omit';
43

54
import { settings } from '../../../../../constants/Settings';
6-
import { NumberInput, Dropdown } from '../../../../../index';
5+
import { NumberInput } from '../../../../../index';
76

87
const { iotPrefix } = settings;
98

@@ -39,8 +38,6 @@ const propTypes = {
3938
precisionLabel: PropTypes.string,
4039
notSet: PropTypes.string,
4140
}),
42-
/** Callback function to translate common ids */
43-
translateWithId: PropTypes.func.isRequired,
4441
};
4542

4643
const defaultProps = {
@@ -52,12 +49,7 @@ const defaultProps = {
5249
},
5350
};
5451

55-
const ValueCardFormSettings = ({
56-
cardConfig,
57-
onChange,
58-
i18n,
59-
translateWithId,
60-
}) => {
52+
const ValueCardFormSettings = ({ cardConfig, onChange, i18n }) => {
6153
const mergedI18n = { ...defaultProps.i18n, ...i18n };
6254
const { content, id } = cardConfig;
6355

@@ -85,37 +77,6 @@ const ValueCardFormSettings = ({
8577
}
8678
/>
8779
</div>
88-
<div className={`${baseClassName}--input`}>
89-
<Dropdown
90-
id={`${id}_value-card-decimal-place`}
91-
titleText={mergedI18n.precisionLabel}
92-
direction="bottom"
93-
label=""
94-
items={[mergedI18n.notSet, '0', '1', '2', '3', '4']}
95-
light
96-
translateWithId={translateWithId}
97-
selectedItem={content?.precision?.toString() || mergedI18n.notSet}
98-
onChange={({ selectedItem }) => {
99-
const isSet = selectedItem !== mergedI18n.notSet;
100-
if (isSet) {
101-
onChange({
102-
...cardConfig,
103-
content: {
104-
...content,
105-
precision: Number(selectedItem),
106-
},
107-
});
108-
} else {
109-
onChange({
110-
...cardConfig,
111-
content: {
112-
...omit(content, 'precision'),
113-
},
114-
});
115-
}
116-
}}
117-
/>
118-
</div>
11980
</>
12081
);
12182
};

src/components/CardEditor/CardEditForm/CardEditFormItems/ValueCardFormItems/ValueCardFormSettings.test.jsx

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const valueCardConfig = {
2222
},
2323
],
2424
fontSize: 16,
25-
precision: 5,
2625
},
2726
};
2827

@@ -60,7 +59,6 @@ describe('Value form fields', () => {
6059
},
6160
],
6261
fontSize: 30,
63-
precision: 5,
6462
},
6563
id: 'Standard',
6664
size: 'MEDIUM',
@@ -85,45 +83,6 @@ describe('Value form fields', () => {
8583
const invalidText = screen.getByText('Provide invalidText');
8684
expect(invalidText).toBeInTheDocument();
8785
});
88-
it('should update JSON for the precision', () => {
89-
render(
90-
<ValueCardFormSettings
91-
cardConfig={valueCardConfig}
92-
onChange={mockOnChange}
93-
/>
94-
);
95-
const precisionInput = screen.getByText('5');
96-
expect(precisionInput).toBeInTheDocument();
97-
98-
fireEvent.click(precisionInput);
99-
const precisionOption = screen.getByText('3');
100-
expect(precisionOption).toBeInTheDocument();
101-
102-
fireEvent.click(precisionOption);
103-
104-
expect(mockOnChange).toHaveBeenCalledWith({
105-
content: {
106-
attributes: [
107-
{
108-
dataSourceId: 'key1',
109-
label: 'Key 1',
110-
unit: '%',
111-
},
112-
{
113-
dataSourceId: 'key2',
114-
label: 'Key 2',
115-
unit: 'lb',
116-
},
117-
],
118-
fontSize: 16,
119-
precision: 3,
120-
},
121-
id: 'Standard',
122-
size: 'MEDIUM',
123-
title: 'value card',
124-
type: 'VALUE',
125-
});
126-
});
12786
it('should handle undefined content', () => {
12887
render(
12988
<ValueCardFormSettings
@@ -138,7 +97,5 @@ describe('Value form fields', () => {
13897
);
13998
const fontSizeInput = screen.getByText('Font size');
14099
expect(fontSizeInput).toBeInTheDocument();
141-
const precisionInput = screen.getByText('Precision');
142-
expect(precisionInput).toBeInTheDocument();
143100
});
144101
});

0 commit comments

Comments
 (0)