Skip to content

Commit

Permalink
Decouple custom margin in centered layout from legacy wide position
Browse files Browse the repository at this point in the history
REDMINE-20384
  • Loading branch information
tf committed Aug 28, 2023
1 parent 293296b commit 1ff555f
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
93 changes: 92 additions & 1 deletion entry_types/scrolled/package/spec/frontend/Layout-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,20 @@ describe('Layout', () => {
expect(container.textContent).toEqual('custom 1 ');
});

it('is false if rendered custom margin is not supported by position', () => {
it('is true if rendered with xl width with custom margin', () => {
const items = [
{id: 1, type: 'probeWithCustomMarginProp', props: {width: 2}}
];
const {container} = renderInEntry(
<Layout sectionProps={{layout: 'center'}} items={items}>
{children => children}
</Layout>
);

expect(container.textContent).toEqual('custom 1 ');
});

it('is false if rendered with floated position', () => {
const items = [
{id: 1, type: 'probeWithCustomMarginProp', position: 'left'}
];
Expand All @@ -693,6 +706,32 @@ describe('Layout', () => {

expect(container.textContent).toEqual('normal 1 ');
});

it('is false if rendered with legacy full position with custom margin', () => {
const items = [
{id: 1, type: 'probeWithCustomMarginProp', position: 'full'}
];
const {container} = renderInEntry(
<Layout sectionProps={{layout: 'center'}} items={items}>
{children => children}
</Layout>
);

expect(container.textContent).toEqual('normal 1 ');
});

it('is false if rendered with full width with custom margin', () => {
const items = [
{id: 1, type: 'probeWithCustomMarginProp', props: {width: 3}}
];
const {container} = renderInEntry(
<Layout sectionProps={{layout: 'center'}} items={items}>
{children => children}
</Layout>
);

expect(container.textContent).toEqual('normal 1 ');
});
});
});

Expand Down Expand Up @@ -1076,6 +1115,32 @@ describe('Layout', () => {
expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['group-full'])).not.toBeNull();
});

it('lets width take precedence for legacy wide item', () => {
const items = [
{id: 2, type: 'probe', position: 'wide', props: {width: 1}}
];
const {getByTestId} = renderInEntry(
<Layout sectionProps={{layout: 'left'}} items={items}>
{children => children}
</Layout>
);

expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['width-lg'])).not.toBeNull();
});

it('applies width class to legacy full items', () => {
const items = [
{id: 2, type: 'probe', position: 'full'}
];
const {getByTestId} = renderInEntry(
<Layout sectionProps={{layout: 'left'}} items={items}>
{children => children}
</Layout>
);

expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['width-full'])).not.toBeNull();
});

it('applies restrict classes to inline box with negative width', () => {
const items = [
{id: 2, type: 'probe', props: {width: -2}}
Expand All @@ -1088,6 +1153,32 @@ describe('Layout', () => {

expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['restrict-xs'])).not.toBeNull();
});

it('changes xxs to xs for sticky item', () => {
const items = [
{id: 2, type: 'probe', position: 'sticky', props: {width: -3}}
];
const {getByTestId} = renderInEntry(
<Layout sectionProps={{layout: 'left'}} items={items}>
{children => children}
</Layout>
);

expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['restrict-xs'])).not.toBeNull();
});

it('changes full to xl for sticky item', () => {
const items = [
{id: 2, type: 'probe', position: 'sticky', props: {width: 3}}
];
const {getByTestId} = renderInEntry(
<Layout sectionProps={{layout: 'left'}} items={items}>
{children => children}
</Layout>
);

expect(findParentWithClass(getByTestId('probe'), twoColumnStyles['width-xl'])).not.toBeNull();
});
});

describe('width classes in centered variant', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function supportsWrappingAroundFloats(item) {
function hasCustomMargin(item) {
const position = getPosition(item);
const {customMargin: elementSupportsCustomMargin} = api.contentElementTypes.getOptions(item.type) || {};
return !!elementSupportsCustomMargin && (position === 'inline' || position === 'wide');
return !!(elementSupportsCustomMargin && position === 'inline' && getWidth(item) < 3);
}

function getPosition(item) {
Expand Down
11 changes: 10 additions & 1 deletion entry_types/scrolled/package/src/frontend/layouts/TwoColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ const legacyPositionWidths = {

function getWidth(item) {
return (typeof item.props?.width === 'number') ?
item.props.width :
clampWidthByPosition(item) :
legacyPositionWidths[item.position] || 0;
}

function clampWidthByPosition(item) {
if (item.position === 'sticky') {
return Math.min(Math.max(item.props?.width || 0, -2), 2);
}
else {
return item.props?.width;
}
}

0 comments on commit 1ff555f

Please sign in to comment.