import autobind from "autobind-decorator"; import { action, makeObservable, observable } from "mobx"; import { observer } from "mobx-react"; import React from "react"; import { Button, Col, Modal, Row } from "react-bootstrap"; import { EModalType, IColorValueMap } from "../ui/react-pathway-mapper"; const addButtonImg = require("../images/add.svg"); const deleteButtonImg = require("../images/delete-simple.svg"); interface IValueColorObj { value: string; color: string; } interface IProfilesColorSchemeModalProps { show: boolean; handleClose: (modalId: EModalType) => void; colorValueMapping: IColorValueMap; handleColorMappingChange: (colorMapping: IColorValueMap) => void; } @observer export default class ProfilesColorSchemeModal extends React.Component< IProfilesColorSchemeModalProps, {} > { @observable colorMappings: IValueColorObj[]; @observable showUniqueValuesWarningModal: boolean; constructor(props: IProfilesColorSchemeModalProps) { super(props); makeObservable(this); this.initColorMappings(); } componentDidUpdate(prevProps) { if (prevProps.show === false && this.props.show === true) { this.initColorMappings(); } } initColorMappings() { this.colorMappings = Object.entries(this.props.colorValueMapping) .map(([value, color]) => { return { value: value, color: color, }; }) .sort((o1, o2) => { return Number(o1.value) - Number(o2.value); }); } @action.bound handleColorChange(index: number, color: string) { this.colorMappings[index].color = color; } @action.bound handleValueChange(index: number, value: string) { this.colorMappings[index].value = value; } @action.bound addDefaultColorMapping() { this.colorMappings.push({ value: "0", color: "#ffffff" }); } @action.bound removeColorMapping(index: number) { this.colorMappings.splice(index, 1); } @action.bound setDefaultColorMapping() { this.colorMappings = [ { value: "-100", color: "#0000ff" }, { value: "0", color: "#ffffff" }, { value: "100", color: "#ff0000" }, ]; } @action.bound setShowUniqueValuesWarningModal(val: boolean) { this.showUniqueValuesWarningModal = val; } @autobind handleSaveColorScheme(): boolean { const mapping = {}; this.colorMappings.forEach((pair) => { mapping[pair.value] = pair.color; }); if (Object.keys(mapping).length < 2) { this.setShowUniqueValuesWarningModal(true); return false; } else { this.props.handleColorMappingChange(mapping); return true; } } @autobind close() { this.props.handleClose(EModalType.PROFILES_COLOR_SCHEME); } render() { const mappingElements = this.colorMappings.map((mapping, index) => ( { const value = event.target.value; this.handleValueChange(index, value); }} > { const color = event.target.value; this.handleColorChange(index, color); }} /> 2 ? "icon-enabled" : "icon-disabled") } title={"Remove"} onClick={() => { this.removeColorMapping(index); }} > )); return ( { this.close(); }} bsSize="small" > Study Data Overlay Color Scheme
{mappingElements} this.setShowUniqueValuesWarningModal(false)} bsSize="small" > Warning

Please set at least two unique value-color mappings.

); } }