chore: fix ts errors

This commit is contained in:
Alex Varchuk 2022-06-27 18:03:21 +03:00
parent 0045be0b75
commit 4c26e68ddd
8 changed files with 87 additions and 101 deletions

View File

@ -340,6 +340,7 @@ async function getPageHTML(
const specUrl = redocOptions.specUrl || (isURL(pathToSpec) ? pathToSpec : undefined);
const store = await createStore(spec, specUrl, redocOptions);
const sheet = new ServerStyleSheet();
// @ts-ignore
html = renderToString(sheet.collectStyles(React.createElement(Redoc, { store })));
css = sheet.getStyleTags();
state = await store.toJS();

View File

@ -8,36 +8,24 @@ export interface CopyButtonWrapperProps {
children: (props: { renderCopyButton: () => React.ReactNode }) => React.ReactNode;
}
export class CopyButtonWrapper extends React.PureComponent<
CopyButtonWrapperProps,
{ tooltipShown: boolean }
> {
constructor(props) {
super(props);
this.state = {
tooltipShown: false,
};
}
export const CopyButtonWrapper = (
props: CopyButtonWrapperProps & { tooltipShown?: boolean },
): JSX.Element => {
const [tooltipShown, setTooltipShown] = React.useState(false);
render() {
return this.props.children({ renderCopyButton: this.renderCopyButton });
}
copy = () => {
const copy = () => {
const content =
typeof this.props.data === 'string'
? this.props.data
: JSON.stringify(this.props.data, null, 2);
typeof props.data === 'string' ? props.data : JSON.stringify(props.data, null, 2);
ClipboardService.copyCustom(content);
this.showTooltip();
showTooltip();
};
renderCopyButton = () => {
const renderCopyButton = () => {
return (
<button onClick={this.copy}>
<button onClick={copy}>
<Tooltip
title={ClipboardService.isSupported() ? 'Copied' : 'Not supported in your browser'}
open={this.state.tooltipShown}
open={tooltipShown}
>
Copy
</Tooltip>
@ -45,15 +33,12 @@ export class CopyButtonWrapper extends React.PureComponent<
);
};
showTooltip() {
this.setState({
tooltipShown: true,
});
const showTooltip = () => {
setTooltipShown(true);
setTimeout(() => {
this.setState({
tooltipShown: false,
});
setTooltipShown(false);
}, 1500);
}
}
};
return props.children({ renderCopyButton: renderCopyButton }) as JSX.Element;
};

View File

@ -8,19 +8,18 @@ const directionMap = {
down: '0',
};
class IntShelfIcon extends React.PureComponent<{
const IntShelfIcon = (props: {
className?: string;
float?: 'left' | 'right';
size?: string;
color?: string;
direction: 'left' | 'right' | 'up' | 'down';
style?: React.CSSProperties;
}> {
render() {
}): JSX.Element => {
return (
<svg
className={this.props.className}
style={this.props.style}
className={props.className}
style={props.style}
version="1.1"
viewBox="0 0 24 24"
x="0"
@ -31,8 +30,7 @@ class IntShelfIcon extends React.PureComponent<{
<polygon points="17.3 8.3 12 13.6 6.7 8.3 5.3 9.7 12 16.4 18.7 9.7 " />
</svg>
);
}
}
};
export const ShelfIcon = styled(IntShelfIcon)`
height: ${props => props.size || '18px'};

View File

@ -17,9 +17,8 @@ export interface CallbackTitleProps {
onClick?: () => void;
}
export class CallbackTitle extends React.PureComponent<CallbackTitleProps> {
render() {
const { name, opened, className, onClick, httpVerb, deprecated } = this.props;
export const CallbackTitle = (props: CallbackTitleProps) => {
const { name, opened, className, onClick, httpVerb, deprecated } = props;
return (
<CallbackTitleWrapper className={className} onClick={onClick || undefined}>
@ -29,8 +28,7 @@ export class CallbackTitle extends React.PureComponent<CallbackTitleProps> {
{deprecated ? <Badge type="warning"> {l('deprecated')} </Badge> : null}
</CallbackTitleWrapper>
);
}
}
};
const CallbackTitleWrapper = styled.button`
border: 0;

View File

@ -1,10 +1,18 @@
import * as React from 'react';
import { StyledComponent } from 'styled-components';
import { DropdownProps, MimeLabel, SimpleDropdown } from '../../common-elements/Dropdown';
export interface DropdownOrLabelProps extends DropdownProps {
Label?: React.ComponentClass;
Dropdown?: React.ComponentClass;
Label?: StyledComponent<any, any, GenericObject, never>;
Dropdown?: StyledComponent<
React.NamedExoticComponent<DropdownProps>,
any,
{
fullWidth?: boolean | undefined;
},
never
>;
}
export function DropdownOrLabel(props: DropdownOrLabelProps): JSX.Element {

View File

@ -37,6 +37,6 @@ export class ErrorBoundary extends React.Component<
</ErrorWrapper>
);
}
return React.Children.only(this.props.children);
return <React.Fragment>{React.Children.only(this.props.children)}</React.Fragment>;
}
}

View File

@ -19,17 +19,13 @@ const JsonViewerWrap = styled.div`
}
`;
class Json extends React.PureComponent<JsonProps> {
node: HTMLDivElement;
const Json = (props: JsonProps) => {
const [node, setNode] = React.useState<HTMLDivElement>();
render() {
return <CopyButtonWrapper data={this.props.data}>{this.renderInner}</CopyButtonWrapper>;
}
renderInner = ({ renderCopyButton }) => {
const renderInner = ({ renderCopyButton }) => {
const showFoldingButtons =
this.props.data &&
Object.values(this.props.data).some(value => typeof value === 'object' && value !== null);
props.data &&
Object.values(props.data).some(value => typeof value === 'object' && value !== null);
return (
<JsonViewerWrap>
@ -37,19 +33,19 @@ class Json extends React.PureComponent<JsonProps> {
{renderCopyButton()}
{showFoldingButtons && (
<>
<button onClick={this.expandAll}> Expand all </button>
<button onClick={this.collapseAll}> Collapse all </button>
<button onClick={expandAll}> Expand all </button>
<button onClick={collapseAll}> Collapse all </button>
</>
)}
</SampleControls>
<OptionsContext.Consumer>
{options => (
<PrismDiv
className={this.props.className}
className={props.className}
// tslint:disable-next-line
ref={node => (this.node = node!)}
ref={node => setNode(node!)}
dangerouslySetInnerHTML={{
__html: jsonToHTML(this.props.data, options.jsonSampleExpandLevel),
__html: jsonToHTML(props.data, options.jsonSampleExpandLevel),
}}
/>
)}
@ -58,8 +54,8 @@ class Json extends React.PureComponent<JsonProps> {
);
};
expandAll = () => {
const elements = this.node.getElementsByClassName('collapsible');
const expandAll = () => {
const elements = node?.getElementsByClassName('collapsible');
for (const collapsed of Array.prototype.slice.call(elements)) {
const parentNode = collapsed.parentNode as Element;
parentNode.classList.remove('collapsed');
@ -67,8 +63,8 @@ class Json extends React.PureComponent<JsonProps> {
}
};
collapseAll = () => {
const elements = this.node.getElementsByClassName('collapsible');
const collapseAll = () => {
const elements = node?.getElementsByClassName('collapsible');
// skip first item to avoid collapsing whole object/array
const elementsArr = Array.prototype.slice.call(elements, 1);
@ -79,7 +75,7 @@ class Json extends React.PureComponent<JsonProps> {
}
};
collapseElement = (target: HTMLElement) => {
const collapseElement = (target: HTMLElement) => {
let collapsed;
if (target.className === 'collapser') {
collapsed = target.parentElement!.getElementsByClassName('collapsible')[0];
@ -93,27 +89,26 @@ class Json extends React.PureComponent<JsonProps> {
}
};
clickListener = (event: MouseEvent) => {
this.collapseElement(event.target as HTMLElement);
};
const clickListener = React.useCallback((event: MouseEvent) => {
collapseElement(event.target as HTMLElement);
}, []);
focusListener = (event: KeyboardEvent) => {
const focusListener = React.useCallback((event: KeyboardEvent) => {
if (event.key === 'Enter') {
this.collapseElement(event.target as HTMLElement);
collapseElement(event.target as HTMLElement);
}
}, []);
React.useEffect(() => {
if (node) {
node?.addEventListener('click', clickListener);
node?.addEventListener('focus', focusListener);
}
}, [clickListener, focusListener, node]);
return <CopyButtonWrapper data={props.data}>{renderInner}</CopyButtonWrapper>;
};
componentDidMount() {
this.node!.addEventListener('click', this.clickListener);
this.node!.addEventListener('focus', this.focusListener);
}
componentWillUnmount() {
this.node!.removeEventListener('click', this.clickListener);
this.node!.removeEventListener('focus', this.focusListener);
}
}
export const JsonViewer = styled(Json)`
${jsonStyles};
`;

View File

@ -42,7 +42,8 @@ export class AdvancedMarkdown extends React.Component<AdvancedMarkdownProps> {
{ key: idx },
);
}
return <part.component key={idx} {...{ ...part.props, ...part.propsSelector(store) }} />;
const PartComponent = part.component as React.FunctionComponent;
return <PartComponent key={idx} {...{ ...part.props, ...part.propsSelector(store) }} />;
});
}
}