Skip to content

Commit

Permalink
Merge pull request #393 from iCHEF/feature/add_right_placement_strate…
Browse files Browse the repository at this point in the history
…gy_pt2

[Anchor] Support left / right direction (pt. 4 - add horizontal placement prop on Popover)
  • Loading branch information
chenesan authored Oct 27, 2022
2 parents 2e03919 + 1bd91ac commit c22e880
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/core/src/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import closable from './mixins/closable';
import renderToLayer from './mixins/renderToLayer';

import './styles/Popover.scss';
import { verticalPlacements } from './mixins/anchored/getPositionState';

export const COMPONENT_NAME = prefixClass('popover');
const ROOT_BEM = icBEM(COMPONENT_NAME);
Expand Down Expand Up @@ -48,7 +49,11 @@ function Popover({
* The `maxHeight` is for `BEM.container`, which doesn't include root class padding.
* So we need to minus POPOVER_PADDING here.
*/
const maxHeight = remainingSpace ? remainingSpace - POPOVER_PADDING : undefined;
const maxHeight = (
(remainingSpace && verticalPlacements.includes(placement))
? remainingSpace - POPOVER_PADDING
: undefined
);

const handleWrapperClick = (event) => {
onInsideClick(event);
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/__tests__/Popover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,35 @@ describe('Pure <Popover>', () => {
left: 100,
});
});

describe('container max height', () => {
it('compute maxHeight on vertical placement', () => {
const wrapper = shallow(
<PurePopover
placement="top"
remainingSpace={50}
/>
);
const containerClassName = BEM.container.toString();

expect(wrapper.find(`.${containerClassName}`).prop('style'))
.toMatchObject({
maxHeight: 26,
});
});
it('wont compute maxHeight on horizontal placement', () => {
const wrapper = shallow(
<PurePopover
placement="left"
remainingSpace={50}
/>
);
const containerClassName = BEM.container.toString();

expect(wrapper.find(`.${containerClassName}`).prop('style'))
.toMatchObject({
maxHeight: undefined,
});
});
});
});
18 changes: 18 additions & 0 deletions packages/core/src/mixins/anchored/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,21 @@ it('passed anchor and self nodes to getter function for position config', () =>
0
);
});

it('can pass defaultPlacement through anchored component to getter function for position config', () => {
const anchorRef = React.createRef();
mount(<><div ref={anchorRef} /></>);

const wrapper = mount(
<AnchoredBox
defaultPlacement={ANCHORED_PLACEMENT.BOTTOM}
anchor={anchorRef.current}
/>
);
expect(mockedGetterFunc).toHaveBeenCalledWith(
ANCHORED_PLACEMENT.BOTTOM,
anchorRef.current,
wrapper.state().selfNode,
0
);
});
4 changes: 2 additions & 2 deletions packages/core/src/mixins/anchored/getPositionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import PLACEMENT from './constants/placement';

export { PLACEMENT };
const { TOP, BOTTOM, LEFT, RIGHT } = PLACEMENT;
const verticalPlacements = [TOP, BOTTOM];
const horizontalPlacements = [LEFT, RIGHT];
export const verticalPlacements = [TOP, BOTTOM];
export const horizontalPlacements = [LEFT, RIGHT];

/**
* @typedef {typeof TOP| typeof BOTTOM} Placement
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/mixins/anchored/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ const anchored = ({
style,
distanceFromAnchor,
refreshOnWindowResize,
defaultPlacement: omittedDefaultPlacement,
...otherProps
} = this.props;

Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/styles/Popover.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,19 @@ $component-name: #{$prefix}-popover;
border-top-color: $c-popover-background;
}
}

&--right {
.#{$component-name}__arrow {
left: -$popover-arrow-size;
border-right-color: $c-popover-background;
}
}

&--left {
.#{$component-name}__arrow {
left: unset;
right: -$popover-arrow-size * 2;
border-left-color: $c-popover-background;
}
}
}
7 changes: 6 additions & 1 deletion packages/storybook/examples/core/Popover.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function BasicExample() {
);
}

export function AnchoredPopover() {
export function AnchoredPopover(popoverProps) {
function ButtonRow(props) {
return (
<ListRow>
Expand Down Expand Up @@ -110,6 +110,7 @@ export function AnchoredPopover() {
anchor={btn}
placement="top"
onClose={handlePopoverClose}
{...popoverProps}
>
<DemoList />
</Popover>
Expand All @@ -125,3 +126,7 @@ AnchoredPopover.story = {
},
},
};

export function RightPlacementAnchoredPopover() {
return <AnchoredPopover defaultPlacement="right" />;
}

0 comments on commit c22e880

Please sign in to comment.