Browse Source

fix: anchorLink href update

pull/10309/head
tangjinzhou 6 years ago
committed by 偏右
parent
commit
14e0cc0161
  1. 8
      components/anchor/AnchorLink.tsx
  2. 16
      components/anchor/__tests__/Anchor.test.js

8
components/anchor/AnchorLink.tsx

@ -27,6 +27,14 @@ export default class AnchorLink extends React.Component<AnchorLinkProps, any> {
this.context.antAnchor.registerLink(this.props.href); this.context.antAnchor.registerLink(this.props.href);
} }
componentWillReceiveProps(nextProps: AnchorLinkProps) {
const { href } = nextProps;
if (this.props.href !== href) {
this.context.antAnchor.unregisterLink(this.props.href);
this.context.antAnchor.registerLink(href);
}
}
componentWillUnmount() { componentWillUnmount() {
this.context.antAnchor.unregisterLink(this.props.href); this.context.antAnchor.unregisterLink(this.props.href);
} }

16
components/anchor/__tests__/Anchor.test.js

@ -90,4 +90,20 @@ describe('Anchor Render', () => {
wrapper.setProps({ children: null }); wrapper.setProps({ children: null });
expect(wrapper.instance().links).toEqual([]); expect(wrapper.instance().links).toEqual([]);
}); });
it('should update links when link href update', async () => {
let anchorInstance = null;
function AnchorUpdate({ href }) {
return (
<Anchor ref={c => anchorInstance = c}>
<Link href={href} title="API" />
</Anchor>
);
}
const wrapper = mount(<AnchorUpdate href="#API" />);
expect(anchorInstance.links).toEqual(['#API']);
wrapper.setProps({ href: '#API_1' });
expect(anchorInstance.links).toEqual(['#API_1']);
});
}); });

Loading…
Cancel
Save