/*************************************************************************************** * Copyright (c) 2023 BlackBerry Limited and contributors. * * Licensed under the MIT license. See LICENSE file in the project root for details. ***************************************************************************************/ import React from 'react'; import { AbstractOutputProps } from './abstract-output-component'; export enum OptionType { DEFAULT, CHECKBOX } export interface OptionState { type: OptionType; label: string; condition?: () => boolean; onClick?: (arg?: unknown) => void; arg?: () => unknown; subSection?: DropDownSubSection; // Contains a list of options for a subsection } export interface DropDownSubSection { options: OptionState[]; // Contains a list of options for a subsection condition: () => boolean; height?: number; } type OptionProps = AbstractOutputProps & OptionState; abstract class AbstractDropDownOption

extends React.Component { constructor(props: P) { super(props); } } export class DropDownOption extends AbstractDropDownOption { constructor(props: OptionProps) { super(props); this.state = { type: OptionType.DEFAULT, label: props.label, onClick: props.onClick ?? undefined, arg: props.arg ?? undefined, condition: props.condition ?? (() => true) }; } render(): JSX.Element { return ( {this.state.condition?.() && (

  • this.state.onClick?.(this.state.arg?.())}>
    {this.state.label}
  • )} ); } } export type OptionCheckBoxState = OptionState & { checked: () => boolean; }; type OptionCheckBoxProps = OptionProps & OptionCheckBoxState; export class DropDownOptionCheckBox extends AbstractDropDownOption { constructor(props: OptionCheckBoxProps) { super(props); this.state = { type: OptionType.CHECKBOX, label: props.label, checked: props.checked, onClick: props.onClick ?? undefined, arg: props.arg ?? undefined, condition: props.condition ?? (() => true) }; } render(): JSX.Element { return ( {this.state.condition?.() && (
  • this.state.onClick?.(this.state.arg?.())}>
    {this.state.label}
  • )}
    ); } } type DropDownProps = AbstractOutputProps & { dropDownOptions: OptionState[]; dropDownOpen: boolean; }; export class DropDownComponent extends React.Component { public optionsMenuRef: React.RefObject; constructor(props: DropDownProps) { super(props); this.optionsMenuRef = React.createRef(); } render(): JSX.Element { return ( {this.props.dropDownOpen && (
      {this.props.dropDownOptions?.map((option, index) => this.renderSection(option))}
    )}
    ); } renderSection(option: OptionState): React.ReactNode { if (option.subSection && option.subSection.options.length > 0) { return ( { // Rendering the upper level option label this.renderOption(option) } {option.subSection.condition?.() && (
      { // Rendering the sub section option.subSection.options.map(subOption => this.renderOption(subOption)) }
    )}
    ); } else { return this.renderOption(option); } } renderOption(option: OptionState): React.ReactNode { if (option.type === OptionType.CHECKBOX) { const checkboxOptions = option as OptionCheckBoxState; return ; } else { return ; } } }