Skip to content

Commit ed961ac

Browse files
authored
chore/MIG-6900 Fix preview border when there exists duplicate field names (#46)
* chore/MIG-6900 Fix preview when there exists a field with the same name * chore/MIG-6900 Swap parameters
1 parent 14d4055 commit ed961ac

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

src/components/field/field-list.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { spacing } from '@leafygreen-ui/tokens';
33

44
import { Field } from '@/components/field/field';
55
import { NodeField, NodeType } from '@/types';
6-
import { DEFAULT_PREVIEW_GROUP_AREA, getPreviewGroupArea } from '@/utilities/get-preview-group-area';
6+
import { DEFAULT_PREVIEW_GROUP_AREA, getPreviewGroupArea, getPreviewId } from '@/utilities/get-preview-group-area';
77
import { DEFAULT_FIELD_PADDING } from '@/utilities/constants';
88

99
const NodeFieldWrapper = styled.div`
@@ -28,7 +28,7 @@ export const FieldList = ({ fields, nodeType, isHovering }: Props) => {
2828
name={name}
2929
nodeType={nodeType}
3030
isHovering={isHovering}
31-
previewGroupArea={previewGroupArea[name] || DEFAULT_PREVIEW_GROUP_AREA}
31+
previewGroupArea={previewGroupArea[getPreviewId(i, name)] || DEFAULT_PREVIEW_GROUP_AREA}
3232
type={fieldType}
3333
spacing={spacing}
3434
{...rest}

src/components/node/node.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ export const NodeWithNestedPreviewFields: Story = {
355355
depth: 1,
356356
variant: 'preview',
357357
},
358+
{
359+
name: 'addresses',
360+
type: 'string',
361+
depth: 2,
362+
variant: 'preview',
363+
},
358364
{
359365
name: 'streetName',
360366
type: 'string',
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,48 @@
1-
import { getPreviewGroupArea } from '@/utilities/get-preview-group-area';
1+
import { getPreviewGroupArea, getPreviewId } from '@/utilities/get-preview-group-area';
22

33
describe('get-preview-group-lengths', () => {
4+
it('Should get preview id', () => {
5+
const previewId = getPreviewId(40, 'orders');
6+
expect(previewId).toEqual('40.orders');
7+
});
48
it('With empty list', () => {
59
const result = getPreviewGroupArea([]);
610
expect(result).toEqual({});
711
});
812
it('With single field with preview variant', () => {
913
const result = getPreviewGroupArea([{ variant: 'preview', name: 'orderId' }]);
10-
expect(result).toEqual({ orderId: { height: 1, width: 0 } });
14+
expect(result).toEqual({ ['0.orderId']: { height: 1, width: 0 } });
1115
});
1216
it('With all fields with preview variant', () => {
1317
const result = getPreviewGroupArea([
1418
{ variant: 'preview', name: 'orderId' },
1519
{ variant: 'preview', name: 'shippingAddress' },
1620
{ variant: 'preview', name: 'transactionId' },
1721
]);
18-
expect(result).toEqual({ orderId: { height: 3, width: 0 } });
22+
expect(result).toEqual({ ['0.orderId']: { height: 3, width: 0 } });
23+
});
24+
it('With all fields with preview variant, with duplicate names', () => {
25+
const result = getPreviewGroupArea([
26+
{ variant: 'preview', name: 'orderId' },
27+
{ variant: 'preview', name: 'orderId' },
28+
{ variant: 'preview', name: 'orderId' },
29+
]);
30+
expect(result).toEqual({ ['0.orderId']: { height: 3, width: 0 } });
1931
});
2032
it('With variable number of glyphs', () => {
2133
const result = getPreviewGroupArea([
2234
{ variant: 'preview', name: 'orderId', glyphs: ['key', 'key'] },
2335
{ variant: 'preview', name: 'shippingAddress', glyphs: ['key', 'key', 'key', 'key'] },
2436
{ variant: 'preview', name: 'transactionId' },
2537
]);
26-
expect(result).toEqual({ orderId: { height: 3, width: 4 } });
38+
expect(result).toEqual({ ['0.orderId']: { height: 3, width: 4 } });
2739
});
2840
it('With some fields with preview variant', () => {
2941
const result = getPreviewGroupArea([
3042
{ variant: 'preview', name: 'orderId', glyphs: ['key'] },
3143
{ name: 'shippingAddress' },
3244
{ variant: 'preview', name: 'transactionId' },
3345
]);
34-
expect(result).toEqual({ orderId: { height: 1, width: 1 }, transactionId: { height: 1, width: 0 } });
46+
expect(result).toEqual({ ['0.orderId']: { height: 1, width: 1 }, ['2.transactionId']: { height: 1, width: 0 } });
3547
});
3648
});

src/utilities/get-preview-group-area.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export const DEFAULT_PREVIEW_GROUP_AREA = {
1010
width: 0,
1111
};
1212

13+
export const getPreviewId = (id: number, name: string) => {
14+
return `${id}.${name}`;
15+
};
16+
1317
/**
1418
* Computes the area of consecutive groups of fields with the variant "preview".
1519
* Height is a unit that is denoted by the number of consecutive fields.
@@ -26,7 +30,7 @@ export const getPreviewGroupArea = (fields: Array<NodeField>) => {
2630

2731
if (field.variant === 'preview') {
2832
if (!name) {
29-
name = field.name;
33+
name = getPreviewId(i, field.name);
3034
}
3135
currentArea = {
3236
height: currentArea.height + 1,

0 commit comments

Comments
 (0)