import React from "react"; interface Props { used: number; available: number; onSet?: (used: number) => void; size: "normal" | "small"; isInteractive: boolean; } export default class SlotManager extends React.PureComponent { static defaultProps = { size: "normal", isInteractive: true, }; handleClick = (isUsed: boolean, evt: React.MouseEvent): void => { let { onSet, used, available, isInteractive } = this.props; evt.nativeEvent.stopImmediatePropagation(); evt.stopPropagation(); if (!onSet || !isInteractive) { return; } if (isUsed) { onSet(Math.max(used - 1, 0)); } else { onSet(Math.min(used + 1, available)); } }; render() { let { available, used, size, isInteractive } = this.props; let slotElements: Array = []; let totalSlots: number = Math.max(available, used); for (let i = 0; i < totalSlots; i++) { let isUsed = i < used; let slotClassNames: Array = ["ct-slot-manager__slot"]; if (isUsed) { slotClassNames.push("ct-slot-manager__slot--used"); } if (isInteractive) { slotClassNames.push("ct-slot-manager__slot--interactive"); } if (i >= available) { slotClassNames.push("ct-slot-manager__slot--over-used"); } slotElements.push(
); } return (
{slotElements}
); } }