import * as React from 'react'; import { cloneChoice, getMmuiFirstCheckedDropChoice } from './utils'; import { MmuiDropChoiceComponent, Props } from './MmuiDropChoiceComponent'; export interface MmuiDropChoiceSingleProps extends Props { } export class MmuiDropChoiceSingleComponent< P extends MmuiDropChoiceSingleProps > extends MmuiDropChoiceComponent

{ /** * Constructor * @param props * */ constructor(props: P) { super(props); } // eslint-disable-next-line @typescript-eslint/no-unused-vars removeToken(choiceId) { return; } getCheckedChoice() { return getMmuiFirstCheckedDropChoice(this.state.choices); } updateChoice(changedChoice) { let choice, newChoice, newChoices = [], childChoice, newChildChoice; for (let p = 0; p < this.state.choices.length; p++) { choice = this.state.choices[p]; newChoice = cloneChoice(choice); if (choice.children && choice.children.length > 0) { newChoice.children = []; for (let c = 0; c < choice.children.length; c++) { childChoice = choice.children[c]; newChildChoice = cloneChoice(childChoice); newChildChoice.isChecked = newChildChoice.id == changedChoice.id; newChoice.children.push(newChildChoice); } } else { newChoice.isChecked = newChoice.id == changedChoice.id; } newChoices.push(newChoice); } this.setState({ choices: newChoices, }); } applyFilter(filterValue: string) { let choice, newChoice, newChoices = [], childChoice, newChildChoice, hasVisibleChildren; for (let p = 0; p < this.state.choices.length; p++) { choice = this.state.choices[p]; newChoice = cloneChoice(choice); if (choice.children && choice.children.length > 0) { newChoice.children = []; hasVisibleChildren = false; for (let c = 0; c < choice.children.length; c++) { childChoice = choice.children[c]; newChildChoice = cloneChoice(childChoice); if (filterValue) { newChildChoice.isVisible = newChildChoice.display .toLowerCase() .includes(filterValue.toLowerCase()); } else { newChildChoice.isVisible = true; } hasVisibleChildren = hasVisibleChildren || newChildChoice.isVisible; newChoice.children.push(newChildChoice); } newChoice.isVisible = hasVisibleChildren; } else { if (filterValue) { newChoice.isVisible = newChoice.display .toLowerCase() .includes(filterValue.toLowerCase()); } else { newChoice.isVisible = true; } } newChoices.push(newChoice); } this.setState({ choices: newChoices, }); } /** * Build the summary of selected choices using just text. */ getTextSummaryRender() { let choice, childChoice, textSummaryDisplay = this.state.title, hasSummary = false; for (let p = 0; p < this.state.choices.length; p++) { choice = this.state.choices[p]; if (choice.children && choice.children.length > 0) { for (let c = 0; c < choice.children.length; c++) { childChoice = choice.children[c]; if (childChoice.isChecked) { textSummaryDisplay = childChoice.display; hasSummary = true; break; } } if (hasSummary) { break; } } else if (choice.isChecked) { textSummaryDisplay = choice.display; break; } } return (

{textSummaryDisplay}
); } getTokenSummaryRender() { return this.getTextSummaryRender(); } renderChoiceInput(choice) { const choiceInputProps: any = { id: choice.id, name: choice.name, value: choice.value, checked: choice.isChecked, }, choiceStyle: any = { display: 'block' }; if (choice.isParent) { choiceInputProps['data-isparent'] = 'true'; } else { choiceInputProps['data-parentid'] = choice.parentValue; } if (!choice.isVisible) { choiceStyle.display = 'none'; } return (
); } renderChoice(choice) { if (choice.children) { let parentHeader, childChoiceRenders = choice.children.map((childChoice) => { return this.renderChoiceInput(childChoice); }); if (choice.isVisible) { parentHeader = {choice.display}; } return ( <> {parentHeader}
{childChoiceRenders}
); } return this.renderChoiceInput(choice); } }