import React, { useRef } from "react"; import { animated } from "@react-spring/web"; import { Checkbox } from "@material-ui/core"; import numeral from "numeral"; import styled from "styled-components"; import { useHighlightHover } from "./utils"; import { useKeydownHandler } from "../../utils/hooks"; import { ItemAction } from "./ItemAction"; import { useTheme } from "@fiftyone/components"; const CheckboxDiv = animated(styled(ItemAction)` display: flex; justify-content: space-between; flex-direction: row; margin: 0; `); const CheckboxContentDiv = styled.div` display: flex; justify-content: space-between; flex: 1; width: calc(100% - 33px); `; const CheckboxTextDiv = styled.span` display: block; white-space: nowrap; `; interface CheckProps { name: string; count: number; onCheck: () => void; active: string; checkmark: CheckState | null; edited: boolean; setActive: (name: string) => void; disabled: boolean; } const Check = ({ name, count, checkmark, onCheck, active, setActive, edited, disabled, }: CheckProps) => { const theme = useTheme(); const { style, onMouseEnter, onMouseLeave } = useHighlightHover( false, active === name ); const ref = useRef(); return ( { setActive(name); onMouseEnter(); }} onMouseLeave={onMouseLeave} style={style} onClick={(e) => !disabled && !(e.target === ref.current || ref.current.contains(e.target)) && onCheck() } > !disabled && onCheck()} checked={checkmark === CheckState.ADD || checkmark === null} style={{ color: edited ? theme.brand : theme.fontDark, padding: "0 0.5rem 0 0", }} /> {name} {count > 0 ? numeral(count).format("0,0") : "0"} ); }; export enum CheckState { ADD, REMOVE, } const CheckerDiv = styled.div` margin: 0 -0.5rem 0.25rem -0.5rem; max-height: 346px; overflow-y: auto; scrollbar-width: none; &::-webkit-scrollbar { width: 0px; background: transparent; display: none; } &::-webkit-scrollbar-thumb { width: 0px; display: none; } `; const createSubmit = ({ name, items, changes, count, setChange, value }) => { return () => { if (name in items && name in changes) { setChange( name, value === CheckState.REMOVE || items[name] === 0 ? null : CheckState.REMOVE ); } else if (name in items) { setChange( name, count === items[name] ? CheckState.REMOVE : CheckState.ADD ); } else { setChange( name, value === CheckState.ADD ? value in items ? CheckState.REMOVE : null : CheckState.ADD ); } }; }; interface CheckerProps { items: { [key: string]: number }; setChange: (name: string, state: CheckState, canSubmit: boolean) => void; changes: { [key: string]: CheckState }; setActive: (name: string) => void; count: number; active: string; disabled: boolean; } const Checker = ({ items, changes, setChange, count, active, setActive, disabled, }: CheckerProps) => { const sorted = Object.entries({ ...items, ...changes }).sort(([a], [b]) => a < b ? -1 : 1 ); const names = sorted.map(([name]) => name); useKeydownHandler((e) => { if (names.length === 0) return; let index = null; if (e.key === "ArrowDown") { index = active === null ? 0 : names.indexOf(active) + 1; } else if (e.key === "ArrowUp") { index = active === null ? names.length - 1 : names.indexOf(active) - 1; } if (index < 0) { index = names.length - 1; } else if (index > names.length - 1) { index = 0; } if (index !== null) { setActive(names[index]); } if (e.key === "Enter" && active.length) { createSubmit({ name: active, changes, items, count, value: sorted.filter(([n]) => n === active)[0][1], setChange, })(); } }); return ( setActive(null)}> {sorted.map(([name, value]) => { const submit = createSubmit({ name, value, items, changes, setChange, count, }); const c = changes[name] === CheckState.ADD ? count : changes[name] === CheckState.REMOVE ? null : items[name]; return ( ); })} ); }; export default React.memo(Checker);