Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Remove findDOMNode #13

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/MutateObserver.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React from 'react';
import useEvent from 'rc-util/lib/hooks/useEvent';
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
import { supportRef, useComposeRef } from 'rc-util/lib/ref';
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import useEvent from 'rc-util/lib/hooks/useEvent';
import DomWrapper from './wrapper';
import React from 'react';
import type { MutationObserverProps } from './interface';
import useMutateObserver from './useMutateObserver';
import DomWrapper from './wrapper';

const MutateObserver: React.FC<MutationObserverProps> = props => {
const { children, options, onMutate = () => {} } = props;
Expand All @@ -23,17 +22,15 @@ const MutateObserver: React.FC<MutationObserverProps> = props => {
canRef ? (children as any).ref : null,
);

const [target, setTarget] = React.useState<HTMLElement>(null);
const [target, setTarget] = React.useState<HTMLElement | DomWrapper>(null);

useMutateObserver(target, callback, options);
useMutateObserver(target as HTMLElement, callback, options);

// =========================== Effect ===========================
// Bind target
useLayoutEffect(() => {
setTarget(
findDOMNode(elementRef.current) || findDOMNode(wrapperRef.current),
);
});
setTarget(elementRef?.current || wrapperRef?.current);
}, []);

// =========================== Render ===========================
if (!children) {
Expand Down
46 changes: 23 additions & 23 deletions src/useMutateObserver.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
import * as React from 'react';

const defaultOptions: MutationObserverInit = {
subtree: true,
childList: true,
attributeFilter: ['style', 'class'],
subtree: true,
childList: true,
attributeFilter: ['style', 'class'],
};

export default function useMutateObserver(
nodeOrList: HTMLElement | HTMLElement[],
callback: MutationCallback,
options: MutationObserverInit = defaultOptions,
nodeOrList: HTMLElement | HTMLElement[],
callback: MutationCallback,
options: MutationObserverInit = defaultOptions,
) {
React.useEffect(() => {
if (!canUseDom() || !nodeOrList) {
return;
}
React.useEffect(() => {
if (!canUseDom() || !nodeOrList) {
return;
}

let instance: MutationObserver;
let instance: MutationObserver;

const nodeList = Array.isArray(nodeOrList) ? nodeOrList : [nodeOrList];
const nodeList = Array.isArray(nodeOrList) ? nodeOrList : [nodeOrList];

if ('MutationObserver' in window) {
instance = new MutationObserver(callback);
if ('MutationObserver' in window) {
instance = new MutationObserver(callback);

Check warning on line 25 in src/useMutateObserver.tsx

View check run for this annotation

Codecov / codecov/patch

src/useMutateObserver.tsx#L25

Added line #L25 was not covered by tests

nodeList.forEach(element => {
instance.observe(element, options);
});
}
return () => {
instance?.takeRecords();
instance?.disconnect();
};
}, [options, nodeOrList]);
nodeList.forEach(element => {
instance.observe(element, options);

Check warning on line 28 in src/useMutateObserver.tsx

View check run for this annotation

Codecov / codecov/patch

src/useMutateObserver.tsx#L27-L28

Added lines #L27 - L28 were not covered by tests
});
}
return () => {
instance?.takeRecords();
instance?.disconnect();
};
}, [options, nodeOrList]);
}
32 changes: 1 addition & 31 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import MutateObserver from '../src';

describe('MutateObserver', () => {
Expand Down Expand Up @@ -27,34 +27,4 @@ describe('MutateObserver', () => {
}
unmount();
});

it('findDOMNode should not error in React.StrictMode', () => {
const fn = jest.fn();
const buttonRef = React.createRef<HTMLButtonElement>();
const warnSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
const Demo = React.forwardRef<
HTMLButtonElement,
React.HTMLAttributes<HTMLButtonElement>
>((props, ref) => {
const [flag, setFlag] = React.useState<boolean>(true);
return (
<React.StrictMode>
<MutateObserver onMutate={fn}>
<button
{...props}
ref={ref}
className={flag ? 'aaa' : 'bbb'}
onClick={() => setFlag(!flag)}
>
click
</button>
</MutateObserver>
</React.StrictMode>
);
});
const { container } = render(<Demo ref={buttonRef} />);
fireEvent.click(container.querySelector('button')!);
expect(warnSpy).not.toHaveBeenCalled();
warnSpy.mockRestore();
});
});
Loading