forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutProps.tsx
More file actions
35 lines (30 loc) · 1.09 KB
/
withoutProps.tsx
File metadata and controls
35 lines (30 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Based on https://github.com/styled-components/styled-components/pull/2093#issuecomment-461510706
import React from 'react';
export const withoutProps = (...omitProps: string[]) => {
const omitSingle = (object = {}, key: string) => {
if (key === null || key === undefined || !(key in object)) return object;
const { [key]: deleted, ...otherKeys }: { [key: string]: any } = object;
return otherKeys;
};
const omit = (object = {}, keys: string[]) => {
if (!keys) return object;
if (Array.isArray(keys)) {
return keys.reduce((result, key) => {
if (key in result) {
return omitSingle(result, key);
}
return result;
}, object);
}
return omitSingle(object, keys);
};
return (Component: React.RefForwardingComponent<any, any>) => {
const componentWithoutProps = React.forwardRef((props, ref) => (
<Component ref={ref} {...omit(props, omitProps)} />
));
if (Component.displayName) {
componentWithoutProps.displayName = `${Component.displayName}WithoutProps`;
}
return componentWithoutProps;
};
};