import {BPComponentProps, UiConfigRendererContextType} from "./BPComponent"; import {Button, MenuItem} from "@blueprintjs/core"; import {BPInputComponent} from "./BPInputComponent"; import {BPValueComponentState} from "./BPValueComponent"; import {ItemRenderer, Select} from "@blueprintjs/select"; import {cleanLabel} from "../utils"; export interface DropdownItem{ label: string, value: string|number, uuid?: string } export type BPDropdownComponentState = BPValueComponentState & { options: DropdownItem[] } const renderDropdownItem: ItemRenderer = (item, { handleClick, handleFocus, modifiers }) => { if (!modifiers.matchesPredicate) { return null; } return ( ); }; export class BPDropdownInputComponent extends BPInputComponent { constructor(props: BPComponentProps, context: UiConfigRendererContextType) { super(props, context, { value: '0', label: 'Dropdown', options: [{label: 'none', value: ''}] }); } getUpdatedState(state: BPDropdownComponentState): BPDropdownComponentState { const children = this.context.methods.getChildren(this.props.config) const options: DropdownItem[] = children.map((value, i) => { let label1 = this.context.methods.getLabel(value) const label = cleanLabel(label1) || `Option ${i}` return {label, value: value!.value ?? label1, uuid: value!.uuid} }) // const selected = options.find(o=>o.value===val)||options[0] return super.getUpdatedState({ ...state, options }); } renderInput() { const item = this.state.options.find(o=>o.value===this.state.value)||this.state.options[0] return ( filterable={false} activeItem={item} key={this.props.config.uuid} disabled={this.state.disabled} fill={true} popoverProps={{ minimal: true, }} popoverTargetProps={{ style: { height: "var(--pt-input-height)", } }} noResults={} items={this.state.options} onItemSelect={(item)=> { if(this.state.readOnly) return this.setValue(item.value) }} itemRenderer={renderDropdownItem} > {/* children become the popover target; render value here */} ) } }