import _ from "lodash" import ReactSelect from "react-select" import { CascadingListAnswerValue } from "../response" import React, { ReactNode } from "react" import { CascadingListRow, CascadingListColumn } from "../formDesign" import { Select } from "@mwater/react-library/lib/bootstrap" import { localizeString } from "../formUtils" /** Threshold above which we switch from native select to a searchable react-select */ const SEARCHABLE_THRESHOLD = 20 /** Localizes strings. Must be called as T("some string") or someThing.T("some string") */ type LocalizeString = (str: string, ...args: any[]) => string export interface Props { /** Rows in the list to choose from */ rows: CascadingListRow[] /** Columns in the table that are displayed also as dropdowns to choose the row */ columns: CascadingListColumn[] value?: CascadingListAnswerValue onValueChange: (value?: CascadingListAnswerValue) => void /** True to sort options alphabetically */ sortOptions?: boolean /** Localizer to use */ T: LocalizeString /** Locale to use */ locale?: string /** True if an alternate is selected. Resets editing mode */ alternateSelected?: boolean } interface State { /** Values of columns as they are selected */ columnValues: (string | null)[] /** True if editing the value. Ignores changes to the value and prevents saving */ editing: boolean } export class CascadingListAnswerComponent extends React.Component { constructor(props: Props) { super(props) this.state = { columnValues: props.columns.map((c) => (props.value ? props.value[c.id] || null : null)), editing: false } } /** Validate the component */ validate(): string | boolean | null { return this.state.editing ? this.props.T("Incomplete selection") : null } componentDidUpdate() { // Reset if alternate selected if (this.props.alternateSelected && (this.state.editing || this.state.columnValues.some(c => c != null))) { this.setState({ columnValues: this.props.columns.map((c) => null), editing: false }) } } /** Handle change to a dropdown */ handleChange = (index: number, value: string | null) => { // If first one reset, then reset entire control if (index === 0 && !value) { this.handleReset() return } const columnValues = this.state.columnValues.slice() columnValues[index] = value // For each afterwards, set to null let pos = index + 1 for (; pos < this.props.columns.length; pos++) { columnValues[pos] = null } // For each afterwards, set if has single value, or otherwise set to null pos = index + 1 for (; pos < this.props.columns.length; pos++) { const values = this.findValues(pos, columnValues) if (values.length == 1) { columnValues[pos] = values[0] } else { break } } // Set rest to null for (; pos < this.props.columns.length; pos++) { columnValues[pos] = null } // If all set, change value if (columnValues[this.props.columns.length - 1]) { // Find row for (const row of this.props.rows) { let exclude = false for (let colIndex = 0; colIndex < this.props.columns.length; colIndex++) { if (row[this.props.columns[colIndex].id] !== columnValues[colIndex]) { exclude = true } } if (!exclude) { // No longer editing this.setState({ columnValues: columnValues, editing: false }) this.props.onValueChange(row) return } } } this.setState({ columnValues: columnValues, editing: true }) } /** Reset control */ handleReset = () => { this.setState({ columnValues: this.props.columns.map((c) => null), editing: false }) this.props.onValueChange() } /** Find values of a particular dropdown filtering by all previous selections */ findValues(index: number, columnValues: Array) { let values: string[] = [] for (const row of this.props.rows) { let exclude = false for (let prev = 0; prev < index; prev++) { if (row[this.props.columns[prev].id] !== columnValues[prev]) { exclude = true } } if (!exclude) { values.push(row[this.props.columns[index].id]) } } // Keep unique values values = _.uniq(values) return values } renderDropdown(index: number) { // Determine available options let options: { label: string; value: string }[] = [] // Find all possible values, filtering by all previous selections const values = this.findValues(index, this.state.columnValues) for (const enumValue of this.props.columns[index].enumValues) { // Add if in values if (values.includes(enumValue.id)) { options.push({ value: enumValue.id, label: localizeString(enumValue.name, this.props.locale) }) } } // Sort options if set if (this.props.sortOptions) { options = _.sortBy(options, (opt) => opt.label) } const selectControl = options.length > SEARCHABLE_THRESHOLD ? ( value={options.find(o => o.value === this.state.columnValues[index]) ?? null} options={options} onChange={(opt) => this.handleChange(index, opt ? opt.value : null)} isClearable placeholder={this.props.T("Select...")} menuPortalTarget={document.body} styles={{ menu: (style) => ({ ...style, zIndex: 2000 }), menuPortal: (style) => ({ ...style, zIndex: 2000 }) }} /> ) : (