Skip to content

Commit

Permalink
Revert "feat: Add keepAlign support (#36)"
Browse files Browse the repository at this point in the history
This reverts commit 4f6ac65.
  • Loading branch information
zombieJ authored Jan 6, 2020
1 parent 1a0e1d0 commit e2583ec
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 153 deletions.
79 changes: 0 additions & 79 deletions examples/position.js

This file was deleted.

53 changes: 5 additions & 48 deletions src/Align.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export interface AlignProps {
monitorWindowResize?: boolean;
disabled?: boolean;
children: React.ReactElement;
/** Always trigger align with each render */
keepAlign?: boolean;
}

interface MonitorRef {
Expand All @@ -45,27 +43,11 @@ function getPoint(point: TargetType) {
return point;
}

interface InternalTestProps {
INTERNAL_TRIGGER_ALIGN?: Function;
}

const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
{
children,
disabled,
target,
align,
onAlign,
monitorWindowResize,
monitorBufferTime = 0,
keepAlign,
...restProps
},
{ children, disabled, target, align, onAlign, monitorWindowResize, monitorBufferTime = 0 },
ref,
) => {
const cacheRef = React.useRef<{ element?: HTMLElement; point?: TargetPoint }>(
{},
);
const cacheRef = React.useRef<{ element?: HTMLElement; point?: TargetPoint }>({});
const nodeRef = React.useRef();
let childNode = React.Children.only(children);

Expand All @@ -81,17 +63,7 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
forceAlignPropsRef.current.onAlign = onAlign;

const [forceAlign, cancelForceAlign] = useBuffer(() => {
if (
process.env.NODE_ENV !== 'production' &&
(restProps as InternalTestProps).INTERNAL_TRIGGER_ALIGN
) {
(restProps as InternalTestProps).INTERNAL_TRIGGER_ALIGN();
}

const {
disabled: latestDisabled,
target: latestTarget,
} = forceAlignPropsRef.current;
const { disabled: latestDisabled, target: latestTarget } = forceAlignPropsRef.current;
if (!latestDisabled && latestTarget) {
const source = nodeRef.current;

Expand Down Expand Up @@ -140,16 +112,10 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
if (nodeRef.current !== sourceResizeMonitor.current.element) {
sourceResizeMonitor.current.cancel();
sourceResizeMonitor.current.element = nodeRef.current;
sourceResizeMonitor.current.cancel = monitorResize(
nodeRef.current,
forceAlign,
);
sourceResizeMonitor.current.cancel = monitorResize(nodeRef.current, forceAlign);
}

if (
cacheRef.current.element !== element ||
!isSamePoint(cacheRef.current.point, point)
) {
if (cacheRef.current.element !== element || !isSamePoint(cacheRef.current.point, point)) {
forceAlign();

// Add resize observer
Expand All @@ -170,15 +136,6 @@ const Align: React.RefForwardingComponent<RefAlign, AlignProps> = (
}
}, [disabled]);

/**
* [Legacy] Should keep re-algin since we don't know if target position changed.
*/
React.useEffect(() => {
if (keepAlign && !disabled) {
forceAlign(true);
}
});

// Listen for window resize
const winResizeRef = React.useRef<{ remove: Function }>(null);
React.useEffect(() => {
Expand Down
28 changes: 2 additions & 26 deletions tests/element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ describe('element align', () => {
render() {
return (
<div style={{ paddingTop: 100 }}>
<div
ref={this.targetRef}
style={{ display: 'inline-block', width: 50, height: 50 }}
>
<div ref={this.targetRef} style={{ display: 'inline-block', width: 50, height: 50 }}>
target
</div>
<Align target={this.getTarget} align={align} {...this.props}>
Expand Down Expand Up @@ -76,33 +73,12 @@ describe('element align', () => {
it('disabled should trigger align', () => {
const onAlign = jest.fn();

const wrapper = mount(
<Test monitorWindowResize onAlign={onAlign} disabled />,
);
const wrapper = mount(<Test monitorWindowResize onAlign={onAlign} disabled />);
expect(onAlign).not.toHaveBeenCalled();

wrapper.setProps({ disabled: false });
jest.runAllTimers();
expect(onAlign).toHaveBeenCalled();
});

it('keepAlign', () => {
const triggerAlign = jest.fn();

class TestAlign extends React.Component {
state = {};

render = () => <Test INTERNAL_TRIGGER_ALIGN={triggerAlign} keepAlign />;
}

const wrapper = mount(<TestAlign />);
const times = triggerAlign.mock.calls.length;

for (let i = 0; i < 10; i += 1) {
wrapper.instance().forceUpdate();
}

expect(triggerAlign.mock.calls.length > times).toBeTruthy();
});
});
/* eslint-enable */

0 comments on commit e2583ec

Please sign in to comment.