redoc/src/components/OptionsProvider.ts

35 lines
793 B
TypeScript
Raw Normal View History

import * as PropTypes from 'prop-types';
2018-01-22 21:30:53 +03:00
import * as React from 'react';
2017-11-21 14:00:33 +03:00
import { RedocNormalizedOptions } from '../services/RedocNormalizedOptions';
export interface OptionsProviderProps {
2017-11-21 14:00:33 +03:00
options: RedocNormalizedOptions;
}
export class OptionsProvider extends React.Component<OptionsProviderProps> {
static childContextTypes = {
redocOptions: PropTypes.object.isRequired,
};
getChildContext() {
return {
2017-11-21 14:00:33 +03:00
redocOptions: this.props.options,
};
}
render() {
return React.Children.only(this.props.children);
}
}
export class ComponentWithOptions<P = {}, S = {}> extends React.Component<P, S> {
static contextTypes = {
redocOptions: PropTypes.object,
};
get options(): RedocNormalizedOptions {
return this.context.redocOptions || {};
}
}