mirror of
https://gitee.com/antv/g6.git
synced 2024-12-03 12:18:40 +08:00
feat(utils): add mergeOptions util
This commit is contained in:
parent
4b997dccfe
commit
6aaca9f9c8
@ -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' });
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user