DocSpace-buildtools/packages/asc-web-components/access-right-select/index.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

import React, { useState } from "react";
import PropTypes from "prop-types";
2022-01-25 08:17:38 +00:00
import ComboBox from "../combobox/index.js";
import DropDownItem from "../drop-down-item/index.js";
import Badge from "../badge/index.js";
import {
StyledAccessRightWrapper,
StyledAccessRightIcon,
StyledAccessRightDescriptionItem,
StyledAccessRightItem,
StyledAccessRightItemIcon,
StyledAccessRightItemContent,
StyledAccessRightItemTitleAndBadge,
} from "./styled-accessright.js";
2022-01-27 12:58:56 +00:00
const AccessRightSelect = ({ options, onSelect, selectedOption, ...props }) => {
const [currentItem, setCurrentItem] = useState(selectedOption);
function onSelectCurrentItem() {
setCurrentItem(this);
onSelect && onSelect(this);
}
const formatToAccessRightItem = (data) => {
2022-01-25 08:17:38 +00:00
return (
<>
{data.map((it) => {
return it.isSeparator ? (
<DropDownItem isSeparator />
) : (
2022-01-27 12:58:56 +00:00
<DropDownItem key={it.key} onClick={onSelectCurrentItem.bind(it)}>
2022-01-25 08:17:38 +00:00
<StyledAccessRightItem>
<StyledAccessRightItemIcon src={it.icon} />
<StyledAccessRightItemContent>
<StyledAccessRightItemTitleAndBadge>
{it.title}
2022-01-27 12:58:56 +00:00
{it.quota && (
2022-01-25 08:17:38 +00:00
<Badge
2022-01-27 12:58:56 +00:00
label={it.quota}
backgroundColor={it.color}
2022-01-25 08:17:38 +00:00
fontSize="8px"
/>
)}
</StyledAccessRightItemTitleAndBadge>
<StyledAccessRightDescriptionItem>
{it.description}
</StyledAccessRightDescriptionItem>
</StyledAccessRightItemContent>
</StyledAccessRightItem>
</DropDownItem>
);
})}
</>
);
};
return (
2022-01-25 08:17:38 +00:00
<StyledAccessRightWrapper>
<StyledAccessRightIcon src={currentItem?.icon} />
<ComboBox
2022-01-27 12:58:56 +00:00
advancedOptions={formatToAccessRightItem(options)}
2022-01-25 08:17:38 +00:00
directionX="left"
directionY="bottom"
opened
noBorder
options={[]}
scaled={false}
selectedOption={{
default: true,
key: 0,
label: currentItem?.title,
}}
size="content"
/>
</StyledAccessRightWrapper>
);
};
AccessRightSelect.propTypes = {
/** List of rights */
2022-01-27 12:58:56 +00:00
options: PropTypes.arrayOf(PropTypes.object).isRequired,
/** Will be triggered whenever an AccessRightSelect is selected option */
onSelect: PropTypes.func,
/** The option that is selected by default */
selectedOption: PropTypes.object,
};
export default AccessRightSelect;