Portal/rsconcept/frontend/src/features/library/components/SelectAccessPolicy.tsx

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import { MiniButton } from '@/components/Control';
2025-01-28 23:23:03 +03:00
import { PolicyIcon } from '@/components/DomainIcons';
import { Dropdown, DropdownButton, useDropdown } from '@/components/Dropdown';
2025-01-28 23:23:03 +03:00
import { CProps } from '@/components/props';
2024-06-07 20:17:03 +03:00
import { prefixes } from '@/utils/constants';
import { describeAccessPolicy, labelAccessPolicy } from '@/utils/labels';
import { AccessPolicy } from '../models/library';
interface SelectAccessPolicyProps extends CProps.Styling {
2024-06-07 20:17:03 +03:00
value: AccessPolicy;
onChange: (value: AccessPolicy) => void;
2024-06-07 20:17:03 +03:00
disabled?: boolean;
stretchLeft?: boolean;
}
2025-02-12 15:12:59 +03:00
export function SelectAccessPolicy({ value, disabled, stretchLeft, onChange, ...restProps }: SelectAccessPolicyProps) {
2024-06-07 20:17:03 +03:00
const menu = useDropdown();
2025-02-05 01:27:32 +03:00
function handleChange(newValue: AccessPolicy) {
menu.hide();
if (newValue !== value) {
onChange(newValue);
}
}
2024-06-07 20:17:03 +03:00
return (
<div ref={menu.ref} {...restProps}>
2024-06-07 20:17:03 +03:00
<MiniButton
title={`Доступ: ${labelAccessPolicy(value)}`}
hideTitle={menu.isOpen}
2024-06-21 17:13:34 +03:00
className='h-full'
2024-06-07 20:17:03 +03:00
icon={<PolicyIcon value={value} size='1.25rem' />}
onClick={menu.toggle}
disabled={disabled}
/>
<Dropdown isOpen={menu.isOpen} stretchLeft={stretchLeft}>
{Object.values(AccessPolicy).map((item, index) => (
<DropdownButton
key={`${prefixes.policy_list}${index}`}
text={labelAccessPolicy(item)}
title={describeAccessPolicy(item)}
icon={<PolicyIcon value={item} size='1rem' />}
onClick={() => handleChange(item)}
/>
))}
</Dropdown>
</div>
);
}