Skip to content

Commit b5e8841

Browse files
authored
feat: add new hoc withPlatform (#2431)
1 parent c60e58d commit b5e8841

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/components/mobile/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './MobileContext';
22
export * from './MobileProvider';
33
export * from './useMobile';
44
export * from './usePlatform';
5+
export * from './withPlatform';
56
export * from './withMobile';
67
export * from './constants';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
3+
import {getComponentName} from '../utils/getComponentName';
4+
5+
import {MobileContext} from './MobileContext';
6+
import type {MobileContextProps} from './MobileContext';
7+
8+
export type WithPlatformProps = {platform: MobileContextProps['platform']};
9+
10+
export function withPlatform<T extends WithPlatformProps>(
11+
WrappedComponent: React.ComponentType<T>,
12+
): React.ComponentType<Omit<T, keyof WithPlatformProps>> {
13+
const componentName = getComponentName(WrappedComponent);
14+
15+
return class WithPlatformComponent extends React.Component<Omit<T, keyof WithPlatformProps>> {
16+
static displayName = `withPlatform(${componentName})`;
17+
static contextType = MobileContext;
18+
declare context: React.ContextType<typeof MobileContext>;
19+
20+
render() {
21+
return <WrappedComponent {...(this.props as T)} platform={this.context.platform} />;
22+
}
23+
};
24+
}

0 commit comments

Comments
 (0)