ghost-portal/packages/react-api/src/hoc/api.tsx
Ewa Kowalska 87505b1c59
Eslint imports (#4037)
* Add eslint configuration

* Patch eslint plugin for sorting imports

* Apply eslint rules for imports
2020-12-01 14:40:42 +01:00

37 lines
1.2 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-api authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import { assert } from '@polkadot/util';
import type { ApiProps, SubtractProps } from '../types';
import type { DefaultProps } from './types';
import { ApiConsumer } from '../ApiContext';
export default function withApi <P extends ApiProps> (Inner: React.ComponentType<P>, defaultProps: DefaultProps = {}): React.ComponentType<any> {
return class WithApi extends React.PureComponent<SubtractProps<P, ApiProps>> {
private component: any = React.createRef();
public render (): React.ReactNode {
return (
<ApiConsumer>
{(apiProps?: ApiProps): React.ReactNode => {
assert(apiProps && apiProps.api, 'Application root must be wrapped inside \'react-api/Api\' to provide API context');
return (
<Inner
{...defaultProps}
{...(apiProps as any)}
{...this.props}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
ref={this.component}
/>
);
}}
</ApiConsumer>
);
}
};
}