// TODO: Cannot do range well selection if multiple selection not enabled import { isEqual, max, uniq } from 'lodash'; import React, { useEffect, useState } from 'react'; import { Button, Col, Dropdown, Menu, Row, Slider, Tooltip, Typography } from 'antd'; import 'antd/dist/antd.css'; import { PlateViewerProps } from './types'; import { createDefaultWellStyle, getErrorsFromProps, getNumericPosition, getPositionLabel, isWellDisabled, isWellInRange, } from './utils'; import { useIsMounted } from '../../hooks/useIsMounted'; import { DownOutlined } from '@ant-design/icons'; const MENU_ITEM_KEY_SELECT_RANGE = 'select-range'; const MENU_ITEM_KEY_CLEAR = 'clear'; const DEFAULT_WELL_SIZE = 25; /** * A purpose built component for viewing, analysing and managing SBS plates. * This component will enable you to quickly set up a page to view samples */ const PlateViewer = (props: PlateViewerProps) => { const { rowCount, columnCount, labelConfiguration, disableWellClick, spaceBetweenWells = 10, wellStyle, wellTootipContent, wellClick, wellContent, wellSelectionConfiguration, sliderConfiguration, wellValueGetter, } = props; const isMounted = useIsMounted(); const [selectedPositions, setSelectedPositions] = useState( wellSelectionConfiguration?.defaultSelectedPositions || [], ); const showLabels = labelConfiguration?.show; const rows = Array(showLabels ? rowCount + 1 : rowCount).fill(1); const columns = Array(showLabels ? columnCount + 1 : columnCount).fill(1); const totalWellCount = columnCount * rowCount; const [sliderMin, setSliderMin] = useState( sliderConfiguration?.initialMin || sliderConfiguration?.minValue || 0, ); const [sliderMax, setSliderMax] = useState( sliderConfiguration?.initialMax || sliderConfiguration?.maxValue || 0, ); const marks: Record = { [sliderConfiguration?.minValue as number]: sliderConfiguration?.valueFormatter && sliderConfiguration?.minValue ? sliderConfiguration.valueFormatter(sliderConfiguration.minValue) : `${sliderConfiguration?.minValue}`, [sliderConfiguration?.maxValue as number]: sliderConfiguration?.valueFormatter && sliderConfiguration?.maxValue ? sliderConfiguration.valueFormatter(sliderConfiguration.maxValue) : `${sliderConfiguration?.maxValue}`, }; function toggleWellSelection(position: number) { if (selectedPositions.includes(position)) { if (wellSelectionConfiguration?.multiple) { setSelectedPositions(selectedPositions.filter((selectedPosition) => selectedPosition !== position)); } else { setSelectedPositions([]); } } else { if (wellSelectionConfiguration?.multiple) { setSelectedPositions([...selectedPositions, position]); } else { setSelectedPositions([position]); } } } useEffect(() => { if (isMounted && wellSelectionConfiguration?.onWellSelectionChange) { wellSelectionConfiguration.onWellSelectionChange( selectedPositions, selectedPositions.map((selectedPosition) => getPositionLabel(selectedPosition, columnCount)), ); } }, [selectedPositions]); // TODO: Validate proprs (cannot do slider without value getter) const errors = getErrorsFromProps(props); if (errors.length) { // tslint:disable-next-line: no-console console.error(errors); } const rowWidths: number[] = []; const positionsInRange: number[] = []; const plateContent = rows.map((_rowValue, rowIndex) => { // We need to track the width of each row so that we know the total size of the div to define the minWidth for let rowWidth = 0; const rowContent = ( 0 ? spaceBetweenWells : 0 }} align="middle"> {columns.map((_columnValue, columnIndex) => { // Ignores the header row const position = getNumericPosition(rowIndex - 1, columnIndex - 1, columnCount); const positionLabel = getPositionLabel(position, columnCount); const value = wellValueGetter ? wellValueGetter(position, positionLabel) : undefined; const inRange = isWellInRange(sliderMin, sliderMax, value, sliderConfiguration); const disableConfig = isWellDisabled(position, positionLabel, disableWellClick); const marginLeft = columnIndex > 0 ? spaceBetweenWells : 0; const styleForWell = { width: DEFAULT_WELL_SIZE, height: DEFAULT_WELL_SIZE, ...createDefaultWellStyle(!!disableConfig.disabled, selectedPositions.includes(position), inRange), ...(wellStyle ? typeof wellStyle === 'function' ? wellStyle(position, positionLabel, inRange) : wellStyle : {}), }; if (typeof styleForWell.width === 'number') { rowWidth += (styleForWell.width as number) + marginLeft; } if (showLabels) { let text = null; if (columnIndex === 0 && rowIndex === 0) { text = <>; } else if (columnIndex === 0) { text = String.fromCharCode(64 + rowIndex); } else if (rowIndex === 0) { text = columnIndex; } if (text) { if (wellSelectionConfiguration?.labelSelectionEnabled) { text = ( handleLabelSelect(rowIndex, columnIndex)}>{text} ); } return ( {text} ); } } // This needs to be after the header column if (inRange) { positionsInRange.push(position); } let tooltipContent = null; if (wellTootipContent) { tooltipContent = typeof wellTootipContent === 'function' ? wellTootipContent(position, positionLabel, inRange) : wellTootipContent; } let content = null; if (wellContent) { content = typeof wellContent === 'function' ? wellContent(position, positionLabel, inRange) : wellContent; } const wellIsClickable = !disableConfig.disabled && wellClick; return ( { if (disableConfig.disabled) { return; } if (wellSelectionConfiguration?.enabled) { toggleWellSelection(position); } if (wellClick) { wellClick(position, positionLabel, inRange, event); } }} > {content} ); })} ); rowWidths.push(rowWidth); return rowContent; }); const allWellsInRangeAreSelected = isEqual(positionsInRange, selectedPositions); const componentMinWidth = max(rowWidths); function handleSelectRange() { if (allWellsInRangeAreSelected) { setSelectedPositions([]); } else { toggleMultipleWells(positionsInRange); } } function toggleMultipleWells(positionsToSelect: number[]) { // Deselect if all already selected if (positionsToSelect.every((positionToSelect) => selectedPositions.includes(positionToSelect))) { setSelectedPositions( selectedPositions.filter((selectedPosition) => !positionsToSelect.includes(selectedPosition)), ); } // Otherwise just add the ones that are missing else { setSelectedPositions(uniq([...selectedPositions, ...positionsToSelect])); } } function handleLabelSelect(rowIndex: number, columnIndex: number): void { const positionsToConsider: number[] = []; if (columnIndex) { let i = columnIndex - 1; while (i < totalWellCount) { const disableConfig = isWellDisabled(i, getPositionLabel(i, columnCount), disableWellClick); if (!disableConfig.disabled) { positionsToConsider.push(i); } i += columnCount; } } else { const rowStartPosition = getNumericPosition(rowIndex - 1, columnIndex, columnCount); for (let i = rowStartPosition; i < rowStartPosition + columnCount; i++) { const disableConfig = isWellDisabled(i, getPositionLabel(i, columnCount), disableWellClick); if (!disableConfig.disabled) { positionsToConsider.push(i); } } } toggleMultipleWells(positionsToConsider); } const actionMenu = ( {sliderConfiguration?.enableSelectAll && wellSelectionConfiguration?.enabled && wellSelectionConfiguration.multiple ? ( {`Select ${positionsInRange.length} wells in range`} ) : null} {sliderConfiguration?.enableSelectAll && wellSelectionConfiguration?.enabled && wellSelectionConfiguration.multiple ? ( ) : null} setSelectedPositions([])} key={MENU_ITEM_KEY_CLEAR}> Clear selection ); return (
{sliderConfiguration?.enabled ? ( <>
{wellSelectionConfiguration?.showCount ? ( {`${selectedPositions.length} Selected`} ) : null}
{ setSliderMin(newMinValue); setSliderMax(newMaxValue); if (sliderConfiguration?.onChange) { sliderConfiguration.onChange(newMinValue, newMaxValue); } }} {...sliderConfiguration?.sliderProps} />
) : null} {plateContent}
); }; export default PlateViewer;