misc_pterodactyl-panel/resources/scripts/components/server/users/PermissionTitleBox.tsx

57 lines
1.7 KiB
TypeScript
Raw Normal View History

import { useField } from 'formik';
2022-11-25 20:25:03 +00:00
import type { ReactNode } from 'react';
import { memo, useCallback } from 'react';
import isEqual from 'react-fast-compare';
import tw from 'twin.macro';
2022-11-25 20:25:03 +00:00
import TitledGreyBox from '@/components/elements/TitledGreyBox';
import Input from '@/components/elements/Input';
interface Props {
2022-11-25 20:25:03 +00:00
children?: ReactNode;
className?: string;
isEditable?: boolean;
title: string;
permissions: string[];
}
2022-11-25 20:25:03 +00:00
function PermissionTitleBox({ isEditable, title, permissions, className, children }: Props) {
const [{ value }, , { setValue }] = useField<string[]>('permissions');
const onCheckboxClicked = useCallback(
(e: React.ChangeEvent<HTMLInputElement>) => {
if (e.currentTarget.checked) {
2022-11-25 20:25:03 +00:00
setValue([...value, ...permissions.filter(p => !value.includes(p))]);
} else {
2022-11-25 20:25:03 +00:00
setValue(value.filter(p => !permissions.includes(p)));
}
},
2022-11-25 20:25:03 +00:00
[permissions, value],
);
return (
<TitledGreyBox
title={
<div css={tw`flex items-center`}>
<p css={tw`text-sm uppercase flex-1`}>{title}</p>
{isEditable && (
<Input
type={'checkbox'}
2022-11-25 20:25:03 +00:00
checked={permissions.every(p => value.includes(p))}
onChange={onCheckboxClicked}
/>
)}
</div>
}
className={className}
>
{children}
</TitledGreyBox>
);
2022-11-25 20:25:03 +00:00
}
const MemoizedPermissionTitleBox = memo(PermissionTitleBox, isEqual);
2022-11-25 20:25:03 +00:00
export default MemoizedPermissionTitleBox;