Files
clash-proxy/src/components/setting/mods/setting-comp.tsx

87 lines
2.0 KiB
TypeScript
Raw Normal View History

import { ChevronRightRounded } from "@mui/icons-material";
2022-08-06 03:48:03 +08:00
import {
Box,
List,
ListItem,
ListItemButton,
2022-08-06 03:48:03 +08:00
ListItemText,
ListSubheader,
} from "@mui/material";
import CircularProgress from "@mui/material/CircularProgress";
import React, { ReactNode, useState } from "react";
import isAsyncFunction from "@/utils/is-async-function";
2022-01-16 03:22:37 +08:00
2022-08-06 03:48:03 +08:00
interface ItemProps {
label: ReactNode;
extra?: ReactNode;
children?: ReactNode;
secondary?: ReactNode;
onClick?: () => void | Promise<any>;
2022-08-06 03:48:03 +08:00
}
export const SettingItem: React.FC<ItemProps> = (props) => {
const { label, extra, children, secondary, onClick } = props;
const clickable = !!onClick;
2022-08-06 03:48:03 +08:00
const primary = (
<Box sx={{ display: "flex", alignItems: "center", fontSize: "14px" }}>
<span>{label}</span>
{extra ? extra : null}
2022-08-06 03:48:03 +08:00
</Box>
);
const [isLoading, setIsLoading] = useState(false);
const handleClick = () => {
if (onClick) {
if (isAsyncFunction(onClick)) {
setIsLoading(true);
onClick()!.finally(() => setIsLoading(false));
} else {
onClick();
}
}
};
return clickable ? (
<ListItem disablePadding>
<ListItemButton onClick={handleClick} disabled={isLoading}>
<ListItemText primary={primary} secondary={secondary} />
{isLoading ? (
<CircularProgress color="inherit" size={20} />
) : (
<ChevronRightRounded />
)}
</ListItemButton>
</ListItem>
) : (
2022-08-06 03:48:03 +08:00
<ListItem sx={{ pt: "5px", pb: "5px" }}>
<ListItemText primary={primary} secondary={secondary} />
2022-08-06 03:48:03 +08:00
{children}
</ListItem>
);
};
2022-01-16 03:22:37 +08:00
export const SettingList: React.FC<{
title: string;
children: ReactNode;
}> = (props) => (
2022-01-16 03:22:37 +08:00
<List>
<ListSubheader
sx={[
{ background: "transparent", fontSize: "16px", fontWeight: "700" },
({ palette }) => {
return {
color: palette.text.primary,
};
},
]}
disableSticky
>
2022-01-16 03:22:37 +08:00
{props.title}
</ListSubheader>
{props.children}
</List>
);