misc_pterodactyl-panel/resources/scripts/hoc/asDialog.tsx

25 lines
1,002 B
TypeScript
Raw Permalink Normal View History

2022-11-25 20:25:03 +00:00
import type { ComponentProps, ComponentType, FunctionComponent } from 'react';
import { useState } from 'react';
import { Dialog, DialogProps, DialogWrapperContext, WrapperProps } from '@/components/elements/dialog';
function asDialog(
2022-11-25 20:25:03 +00:00
initialProps?: WrapperProps,
// eslint-disable-next-line @typescript-eslint/ban-types
2022-11-25 20:25:03 +00:00
): <P extends {}>(C: ComponentType<P>) => FunctionComponent<P & DialogProps> {
return function (Component) {
return function ({ open, onClose, ...rest }) {
const [props, setProps] = useState<WrapperProps>(initialProps || {});
return (
<DialogWrapperContext.Provider value={{ props, setProps, close: onClose }}>
<Dialog {...props} open={open} onClose={onClose}>
2022-11-25 20:25:03 +00:00
<Component {...(rest as unknown as ComponentProps<typeof Component>)} />
</Dialog>
</DialogWrapperContext.Provider>
);
};
};
}
export default asDialog;