feat(utils): add mergeOptions util

This commit is contained in:
Aaron 2024-05-30 21:32:04 +08:00
parent 4b997dccfe
commit 6aaca9f9c8
2 changed files with 28 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { computeElementCallbackStyle, zIndexOf } from '@/src/utils/style';
import { computeElementCallbackStyle, mergeOptions, zIndexOf } from '@/src/utils/style';
describe('style', () => {
it('computeElementCallbackStyle', () => {
@ -45,4 +45,13 @@ describe('style', () => {
expect(zIndexOf({ id: 'node-1', style: { zIndex: 1 } })).toBe(1);
expect(zIndexOf({ id: 'node-1', style: { zIndex: -1 } })).toBe(-1);
});
it('mergeOptions', () => {
expect(
mergeOptions(
{ style: { a: 1, b: [1, 2], c: { d: 1 } }, id: '1' },
{ style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' },
),
).toEqual({ style: { a: 2, b: [2, 3], c: { f: 1 } }, id: '2' });
});
});

View File

@ -1,3 +1,4 @@
import type { DisplayObjectConfig } from '@antv/g';
import { isFunction } from '@antv/util';
import type { CallableObject, ElementDatum, StyleIterationContext } from '../types';
import { inferDefaultValue } from './animation';
@ -36,3 +37,20 @@ export function computeElementCallbackStyle(
export function zIndexOf(datum: ElementDatum) {
return datum.style?.zIndex ?? (inferDefaultValue('zIndex') as number) ?? 0;
}
/**
* <zh/>
*
* <en/> Merge graphic configuration
* @param options - <zh/> | <en/> graphic configuration array
* @returns <zh/> | <en/> merged configuration
*/
export function mergeOptions(...options: DisplayObjectConfig<any>[]): DisplayObjectConfig<any> {
const option = { style: {} };
for (const opt of options) {
const { style, ..._ } = opt;
Object.assign(option.style, style);
Object.assign(option, _);
}
return option;
}