Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

fix: filter out duplicate plugins #626

Merged
merged 1 commit into from
Oct 27, 2019
Merged
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
2 changes: 1 addition & 1 deletion src/addons/createSingleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function createSingleton(
);
}

plugins = defaultProps.plugins.concat(optionalProps.plugins || plugins);
plugins = optionalProps.plugins || plugins;

tippyInstances.forEach(instance => {
instance.disable();
Expand Down
2 changes: 1 addition & 1 deletion src/addons/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function delegate(
);
}

plugins = defaultProps.plugins.concat(props.plugins || plugins);
plugins = props.plugins || plugins;

let listeners: ListenerObj[] = [];
let childTippyInstances: Instance[] = [];
Expand Down
3 changes: 2 additions & 1 deletion src/createTippy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
pushIfUnique,
arrayFrom,
appendPxIfNumber,
unique,
} from './utils';
import {warnWhen, validateProps, createMemoryLeakWarning} from './validation';

Expand All @@ -72,7 +73,6 @@ export default function createTippy(
collectionProps: Props,
): Instance | null {
const props = getExtendedProps(evaluateProps(reference, collectionProps));
const {plugins} = props;

// If the reference shouldn't have multiple tippys, return null early
if (!props.multiple && reference._tippy) {
Expand Down Expand Up @@ -103,6 +103,7 @@ export default function createTippy(
const popper = createPopperElement(id, props);
const popperChildren = getChildren(popper);
const popperInstance: PopperInstance | null = null;
const plugins = unique(props.plugins);

// These two elements are static
const {tooltip, content} = popperChildren;
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,10 @@ export function pushIfUnique<T>(arr: T[], value: T): void {
export function appendPxIfNumber(value: string | number): string {
return typeof value === 'number' ? `${value}px` : value;
}

/**
* Filters out duplicate elements in an array
*/
export function unique<T>(arr: T[]): T[] {
return arr.filter((item, index) => arr.indexOf(item) === index);
}
9 changes: 9 additions & 0 deletions test/integration/createTippy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ describe('createTippy', () => {

expect(instance.props.animateFill).toBe(animateFill.defaultValue);
});

it('`instance.plugins` does not contain duplicate plugins', () => {
instance = createTippy(h(), {
...defaultProps,
plugins: [animateFill, animateFill],
});

expect(instance.plugins).toEqual([animateFill]);
});
});

describe('instance.destroy()', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/unit/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,12 @@ describe('appendPxIfNumber', () => {
expect(Utils.appendPxIfNumber('10px')).toBe('10px');
});
});

describe('unique', () => {
it('filters out duplicate elements', () => {
const ref1 = {};
const ref2 = {};
expect(Utils.unique([0, 1, 0, 2, 3, 2, 3, 3, 4])).toEqual([0, 1, 2, 3, 4]);
expect(Utils.unique([ref1, ref1, ref2])).toEqual([ref1, ref2]);
});
});