redoc/src/components/OptionsProvider.ts
2018-01-23 15:04:50 +02:00

35 lines
793 B
TypeScript

import * as PropTypes from 'prop-types';
import * as React from 'react';
import { RedocNormalizedOptions } from '../services/RedocNormalizedOptions';
export interface OptionsProviderProps {
options: RedocNormalizedOptions;
}
export class OptionsProvider extends React.Component<OptionsProviderProps> {
static childContextTypes = {
redocOptions: PropTypes.object.isRequired,
};
getChildContext() {
return {
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 || {};
}
}