Bin
2025-12-17 d616898802dfe7e5dd648bcf53c6d1f86b6d3642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { useContext, useMemo } from "react";
import { Draggable } from "react-beautiful-dnd";
 
import { sanitizeHtml } from "../../utils/html";
import type { InputItem } from "./createData";
import { CollapsedContext } from "./Ranker";
 
import styles from "./Ranker.module.scss";
 
interface ItemProps {
  item: InputItem;
  index: number;
  readonly?: boolean;
}
 
/**
 * Item component represents a draggable item within each column. Items can be dragged within a
 * given column as well as between columns.
 */
const Item = (props: ItemProps) => {
  const { item, index, readonly } = props;
 
  // @todo document html parameter later after proper tests
  const html = useMemo(() => (item.html ? sanitizeHtml(item.html) : ""), [item.html]);
  const [collapsible, collapsedMap, toggleCollapsed] = useContext(CollapsedContext);
  const collapsed = collapsedMap[item.id] ?? false;
  const toggle = collapsible ? () => toggleCollapsed(item.id, !collapsed) : undefined;
  const classNames = [styles.item, "htx-ranker-item"];
 
  if (collapsible) classNames.push(collapsed ? styles.collapsed : styles.expanded);
 
  return (
    <Draggable draggableId={item.id} index={index} isDragDisabled={readonly}>
      {(provided) => {
        return (
          <div
            {...provided.draggableProps}
            {...provided.dragHandleProps}
            style={{ ...provided.draggableProps.style }}
            className={classNames.join(" ")}
            ref={provided.innerRef}
            data-ranker-id={item.id}
          >
            {item.title && (
              <h3 className={styles.itemTitle} onClick={toggle}>
                {item.title}
              </h3>
            )}
            {item.body && <p className={styles.itemLine}>{item.body}</p>}
            {item.html && <p className={styles.itemLine} dangerouslySetInnerHTML={{ __html: html }} />}
            <p className={styles.itemLine}>{item.id}</p>
          </div>
        );
      }}
    </Draggable>
  );
};
 
export default Item;