ohif-viewer/platform/ui/src/components/ListMenu/ListMenu.tsx

64 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-06-25 20:27:42 +02:00
import React, { useState } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
const flex = 'flex flex-row justify-between items-center';
const theme = 'bg-indigo-dark text-white';
const ListMenu = ({ items = [], renderer, onClick }) => {
2020-06-25 20:27:42 +02:00
const [selectedIndex, setSelectedIndex] = useState(null);
const ListItem = ({ item, index, isSelected }) => {
const onClickHandler = () => {
setSelectedIndex(index);
onClick({ item, selectedIndex: index });
item.onClick?.({ ...item, index, isSelected });
};
2020-06-25 20:27:42 +02:00
return (
<div
className={classnames(flex, theme, {
'cursor-pointer': !item.disabled,
'ohif-disabled': item.disabled,
})}
onClick={onClickHandler}
data-cy={item.id}
>
{renderer && renderer({ ...item, index, isSelected })}
2020-06-25 20:27:42 +02:00
</div>
);
};
return (
<div
className="bg-secondary-dark flex flex-col gap-[4px] overflow-scroll rounded-md p-1"
style={{ maxHeight: 'calc(100vh - 5rem)' }}
>
{items.map((item, index) => {
2020-06-25 20:27:42 +02:00
return (
<ListItem
key={`ListItem${index}`}
index={index}
isSelected={selectedIndex === index}
item={item}
2020-06-25 20:27:42 +02:00
/>
);
})}
</div>
);
};
const noop = () => {};
2020-06-25 20:27:42 +02:00
ListMenu.propTypes = {
items: PropTypes.array.isRequired,
2020-06-25 20:27:42 +02:00
renderer: PropTypes.func.isRequired,
onClick: PropTypes.func,
2020-06-25 20:27:42 +02:00
};
ListMenu.defaultProps = {
onClick: noop,
2020-06-25 20:27:42 +02:00
};
export default ListMenu;