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

60 lines
1.4 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 ListMenu = ({ items = [], renderer, onClick }) => {
2020-06-25 20:27:42 +02:00
const [selectedIndex, setSelectedIndex] = useState(null);
const ListItem = ({ item, index, isSelected }) => {
2020-06-25 20:27:42 +02:00
const flex = 'flex flex-row justify-between items-center';
const theme = 'bg-indigo-dark';
const onClickHandler = () => {
setSelectedIndex(index);
onClick({ item, selectedIndex: index });
if (item.onClick) {
item.onClick({ ...item, index, isSelected });
}
};
2020-06-25 20:27:42 +02:00
return (
<div
className={classnames(flex, theme, 'cursor-pointer')}
onClick={onClickHandler}
data-cy={item.id}
>
{renderer && renderer({ ...item, index, isSelected })}
2020-06-25 20:27:42 +02:00
</div>
);
};
return (
<div className="flex flex-col rounded-md bg-secondary-dark pt-2 pb-2">
{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;