Skip to content

Commit

Permalink
fix(ConfigProvider): exclude prop with undefined value (#6044)
Browse files Browse the repository at this point in the history
  • Loading branch information
inomdzhon authored Oct 24, 2023
1 parent 9abc817 commit b32712f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('ConfigProvider', () => {
});
it('convert WebviewType.VKAPPS to hasCustomPanelHeaderAfter={true}', () => {
const config = {
platform: undefined,
appearance: Appearance.LIGHT,
webviewType: WebviewType.VKAPPS,
transitionMotionEnabled: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { useObjectMemo } from '../../hooks/useObjectMemo';
import { useDOM } from '../../lib/dom';
import { TokensClassProvider } from '../../lib/tokensClassProvider';
import { useIsomorphicLayoutEffect } from '../../lib/useIsomorphicLayoutEffect';
import { addClassNameToElement, removeClassNameFromElement } from '../../lib/utils';
import {
addClassNameToElement,
excludeKeysWithUndefined,
removeClassNameFromElement,
} from '../../lib/utils';
import { warnOnce } from '../../lib/warnOnce';
import {
ConfigProviderContext,
Expand All @@ -32,7 +36,8 @@ export interface ConfigProviderProps extends Partial<ConfigProviderContextInterf
/**
* @see https://vkcom.github.io/VKUI/#/ConfigProvider
*/
export const ConfigProvider = (props: ConfigProviderProps) => {
export const ConfigProvider = (propsRaw: ConfigProviderProps) => {
const props = excludeKeysWithUndefined(propsRaw);
const parentConfig = useConfigProvider();

const {
Expand Down
15 changes: 14 additions & 1 deletion packages/vkui/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { addClassNameToElement, removeClassNameFromElement } from './utils';
import {
addClassNameToElement,
excludeKeysWithUndefined,
removeClassNameFromElement,
} from './utils';

describe('addClassNameToElement', () => {
test('adds className to element', () => {
Expand Down Expand Up @@ -47,3 +51,12 @@ describe('removeClassNameFromElement', () => {
expect(div.getAttribute('class')).toEqual('');
});
});

describe(excludeKeysWithUndefined, () => {
test('should exclude keys with undefined ', () => {
expect(excludeKeysWithUndefined({})).toEqual({});
expect(excludeKeysWithUndefined({ key1: 1 })).toEqual({ key1: 1 });
expect(excludeKeysWithUndefined({ key1: 1, key2: undefined })).toEqual({ key1: 1 });
expect(excludeKeysWithUndefined({ key1: 1, key2: null, key3: undefined })).toEqual({ key1: 1, key2: null }); // prettier-ignore
});
});
16 changes: 16 additions & 0 deletions packages/vkui/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ export function removeClassNameFromElement(element: HTMLElement, classNameToRemo

element.setAttribute('class', classNamesArray.join(' '));
}

type ExcludeKeysWithUndefined<T> = {
[P in keyof T]?: Exclude<T[P], undefined>;
};

export const excludeKeysWithUndefined = <T extends Record<string | number | symbol, any>>(
obj: T,
): ExcludeKeysWithUndefined<T> => {
const filteredObj: ExcludeKeysWithUndefined<T> = {};
for (const key in obj) {
if (obj.hasOwnProperty(key) && obj[key] !== undefined) {
filteredObj[key] = obj[key];
}
}
return filteredObj;
};

0 comments on commit b32712f

Please sign in to comment.