Using the new context APIs

This commit is contained in:
kobelb 2019-03-01 11:49:42 -08:00
parent 6202363c49
commit 4776f1fc86
3 changed files with 37 additions and 24 deletions

View file

@ -17,9 +17,9 @@
* under the License.
*/
import PropTypes from 'prop-types';
import React, { Component, ComponentClass, ComponentType } from 'react';
import { UICapabilities } from '../ui_capabilities';
import { UICapabilitiesContext } from './ui_capabilities_context';
function getDisplayName(component: ComponentType<any>) {
return component.displayName || component.name || 'Component';
@ -39,18 +39,14 @@ export function injectUICapabilities<P>(
public static WrappedComponent: ComponentType<P & InjectedProps> = WrappedComponent;
public static contextTypes = {
uiCapabilities: PropTypes.object.isRequired,
};
public static contextType = UICapabilitiesContext;
constructor(props: any, context: any) {
super(props, context);
}
public render() {
return (
<WrappedComponent {...this.props} {...{ uiCapabilities: this.context.uiCapabilities }} />
);
return <WrappedComponent {...this.props} {...{ uiCapabilities: this.context }} />;
}
}
return InjectUICapabilities;

View file

@ -0,0 +1,27 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { UICapabilities } from '../ui_capabilities';
export const UICapabilitiesContext = React.createContext<UICapabilities>({
navLinks: {},
catalogue: {},
management: {},
});

View file

@ -17,32 +17,22 @@
* under the License.
*/
import PropTypes from 'prop-types';
import React, { ReactNode } from 'react';
import { uiCapabilities, UICapabilities } from '../ui_capabilities';
import { uiCapabilities } from '../ui_capabilities';
import { UICapabilitiesContext } from './ui_capabilities_context';
interface Props {
children: ReactNode;
}
interface ProviderContext {
uiCapabilities: UICapabilities;
}
export class UICapabilitiesProvider extends React.Component<Props, {}> {
public static displayName: string = 'UICapabilitiesProvider';
public static childContextTypes = {
uiCapabilities: PropTypes.object.isRequired,
};
public getChildContext(): ProviderContext {
return {
uiCapabilities,
};
}
public render() {
return React.Children.only(this.props.children);
return (
<UICapabilitiesContext.Provider value={uiCapabilities}>
{this.props.children}
</UICapabilitiesContext.Provider>
);
}
}