redoc/src/components/RedocStandalone.tsx

60 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-11-23 12:28:05 +03:00
import * as PropTypes from 'prop-types';
2018-01-22 21:30:53 +03:00
import * as React from 'react';
2018-01-22 21:30:53 +03:00
import { RedocNormalizedOptions, RedocRawOptions } from '../services/RedocNormalizedOptions';
import { ErrorBoundary } from './ErrorBoundary';
2018-01-22 21:30:53 +03:00
import { Loading } from './Loading/Loading';
import { Redoc } from './Redoc/Redoc';
import { StoreBuilder } from './StoreBuilder';
2017-11-19 22:27:44 +03:00
export interface RedocStandaloneProps {
2017-11-20 16:02:35 +03:00
spec?: object;
specUrl?: string;
2017-11-21 14:00:33 +03:00
options?: RedocRawOptions;
onLoaded?: (e?: Error) => any;
}
export class RedocStandalone extends React.PureComponent<RedocStandaloneProps> {
static propTypes = {
spec: (props, _, componentName) => {
if (!props.spec && !props.specUrl) {
return new Error(
2018-01-23 20:09:23 +03:00
`One of props 'spec' or 'specUrl' was not specified in '${componentName}'.`,
);
}
return null;
},
specUrl: (props, _, componentName) => {
if (!props.spec && !props.specUrl) {
return new Error(
`One of props 'spec' or 'specUrl' was not specified in '${componentName}'.`,
);
}
return null;
},
2017-11-23 12:28:05 +03:00
options: PropTypes.object,
};
render() {
const { spec, specUrl, options = {}, onLoaded } = this.props;
const hideLoading = options.hideLoading !== undefined;
const normalizedOpts = new RedocNormalizedOptions(options);
return (
<ErrorBoundary>
<StoreBuilder spec={spec} specUrl={specUrl} options={options} onLoaded={onLoaded}>
{({ loading, store }) =>
!loading ? (
<Redoc store={store!} />
) : hideLoading ? null : (
2018-07-19 13:05:53 +03:00
<Loading color={normalizedOpts.theme.colors.primary.main} />
)
}
</StoreBuilder>
</ErrorBoundary>
);
}
}