Skip to content

Commit

Permalink
refactor: Wrap affix with FC (#34254)
Browse files Browse the repository at this point in the history
* test: fix

* chore: ts fix

* chore: update ts def

* test: fix test
  • Loading branch information
zombieJ committed Mar 2, 2022
1 parent cfca106 commit a05a867
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
10 changes: 5 additions & 5 deletions components/affix/__tests__/Affix.test.tsx
Expand Up @@ -17,7 +17,7 @@ class AffixMounter extends React.Component<{
}> {
private container: HTMLDivElement;

public affix: Affix;
public affix: React.Component<AffixProps, AffixState>;

componentDidMount() {
this.container.addEventListener = jest
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Affix Render', () => {

const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
let affixMounterWrapper: ReactWrapper<unknown, unknown, AffixMounter>;
let affixWrapper: ReactWrapper<AffixProps, AffixState, Affix>;
let affixWrapper: ReactWrapper<AffixProps, AffixState, React.Component<AffixProps, AffixState>>;

const classRect: Record<string, DOMRect> = {
container: {
Expand Down Expand Up @@ -157,9 +157,9 @@ describe('Affix Render', () => {
const getTarget = () => container;
affixWrapper = mount(<Affix target={getTarget}>{null}</Affix>);
affixWrapper.setProps({ target: () => null });
expect(affixWrapper.instance().state.status).toBe(0);
expect(affixWrapper.instance().state.affixStyle).toBe(undefined);
expect(affixWrapper.instance().state.placeholderStyle).toBe(undefined);
expect(affixWrapper.find('Affix').last().state().status).toBe(0);
expect(affixWrapper.find('Affix').last().state().affixStyle).toBe(undefined);
expect(affixWrapper.find('Affix').last().state().placeholderStyle).toBe(undefined);
});

it('instance change', async () => {
Expand Down
10 changes: 8 additions & 2 deletions components/affix/index.tsx
Expand Up @@ -126,7 +126,7 @@ class Affix extends React.Component<AffixProps, AffixState> {

getOffsetTop = () => {
const { offsetBottom, offsetTop } = this.props;
return (offsetBottom === undefined && offsetTop === undefined) ? 0 : offsetTop;
return offsetBottom === undefined && offsetTop === undefined ? 0 : offsetTop;
};

getOffsetBottom = () => this.props.offsetBottom;
Expand Down Expand Up @@ -286,4 +286,10 @@ class Affix extends React.Component<AffixProps, AffixState> {
}
}

export default Affix;
const AffixFC = React.forwardRef<Affix, AffixProps>((props, ref) => <Affix {...props} ref={ref} />);

if (process.env.NODE_ENV !== 'production') {
AffixFC.displayName = 'Affix';
}

export default AffixFC;
7 changes: 3 additions & 4 deletions components/affix/utils.ts
@@ -1,5 +1,4 @@
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import Affix from '.';

export type BindElement = HTMLElement | Window | null | undefined;

Expand Down Expand Up @@ -41,7 +40,7 @@ const TRIGGER_EVENTS = [

interface ObserverEntity {
target: HTMLElement | Window;
affixList: Affix[];
affixList: any[];
eventHandlers: { [eventName: string]: any };
}

Expand All @@ -52,7 +51,7 @@ export function getObserverEntities() {
return observerEntities;
}

export function addObserveTarget(target: HTMLElement | Window | null, affix: Affix): void {
export function addObserveTarget<T>(target: HTMLElement | Window | null, affix: T): void {
if (!target) return;

let entity: ObserverEntity | undefined = observerEntities.find(item => item.target === target);
Expand All @@ -78,7 +77,7 @@ export function addObserveTarget(target: HTMLElement | Window | null, affix: Aff
}
}

export function removeObserveTarget(affix: Affix): void {
export function removeObserveTarget<T>(affix: T): void {
const observerEntity = observerEntities.find(oriObserverEntity => {
const hasAffix = oriObserverEntity.affixList.some(item => item === affix);
if (hasAffix) {
Expand Down

0 comments on commit a05a867

Please sign in to comment.