import type { VNode } from 'vue'; import type { ValidationState } from '../../common/static-wrappers/interfaces/validation-interface'; import type { DropdownButtonItemArgs } from '../dropdown-button/dropdown-button-item'; import { Prop, toNative } from 'vue-facing-decorator'; import PowerduckState from '../../app/powerduck-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { isNullOrEmpty } from '../../common/utils/is-null-or-empty'; import { TooltipLifecycle } from '../../common/utils/tooltip-lifecycle'; import { PortalUtils } from '../../common/utils/utils'; import DropdownButtonItem from '../dropdown-button/dropdown-button-item'; import './css/form-item-wrapper.css'; export enum HintType { Default = 0, Label = 1, } export enum MarginType { Standard = 0, None = 1, Small = 2, } export interface FormItemWrapperArgs { label?: string | VNode; mandatory?: boolean; hint?: string; hintType?: HintType; marginType?: MarginType; subtitle?: string; labelButtons?: DropdownButtonItemArgs[]; flex?: boolean; cssClass?: string; /** Extra class on the `.form-item-wrapper` root; `cssClass` lands on the group inside it. */ rootCssClass?: string; maxWidth?: number; validationState?: ValidationState; showClearValueButton?: boolean; appendIcon?: string; prependIcon?: string; appendClicked?: () => void; prependClicked?: () => void; prependIconClicked?: () => void; appendIconClicked?: () => void; /** * Accessible name of a clickable `appendIcon` ("Show password"). With one, the icon is a real * ` ); } getFormGroupBaseClass(): string { let retVal: string = null; if (this.appendIcon == null && this.prependIcon == null && this.hint == null) { retVal = 'form-group'; } else { retVal = 'input-group'; } if (this.marginType != null) { retVal += ` fiw-margintype-${this.marginType}`; } if (this.showClearValueButton) { retVal += ' fiw-has-clear-value'; } return retVal; } getPropCssClass() { return (!isNullOrEmpty(this.cssClass) ? `${this.cssClass} ` : ''); } getFlexCssClass() { return (this.flex != true ? '' : 'frm-item-flex' + ' '); } syncHintTooltips() { // Same scope the old render-time binding had: custom renders and the wrap=false // branch never bound Bootstrap tooltips (native title only) — keep it that way. if (FormItemWrapperConfig.customRender != null || this.wrap == false) { return; } const root = this.$el as HTMLElement | null; // i.icon-info, not every `i` in the label — the loose selector would also hit // labelButtons icons and put the hint text on them. const selector = this.hintType == HintType.Label ? '.col-form-label i.icon-info' : 'div.input-group-append'; const targets = isNullOrEmpty(this.hint) || root == null ? [] : Array.from(root.querySelectorAll(selector)); // Backstop for departures beforeUpdate() didn't predict; the common cases (hint cleared, // hintType/wrap flipped) were already disposed there, while still attached. this.boundHintEls .filter(el => !targets.includes(el)) .forEach(el => TooltipLifecycle.dispose(el)); targets.forEach(el => TooltipLifecycle.sync(el, this.hint)); this.boundHintEls = targets; this.boundHintType = this.hintType; } bindClearValueButton() { let elem: HTMLInputElement = this.$el.querySelector('.form-control'); if (elem == null) { elem = this.$el.querySelector('input'); } if (elem == null) { throw new Error('Unable to auto-bind to given control, has to be bound manually'); } const button: HTMLElement = this.$el.querySelector('.fiw-clear-value-button'); const valueCheck = () => { const val = elem.value; if (val) { button.classList.add('fiw-clear-value-button-visible'); } else { button.classList.remove('fiw-clear-value-button-visible'); } }; const clearInput = () => { elem.value = ''; elem.dispatchEvent(new Event('change')); }; this.handleCurrentInputState = valueCheck; this.clearInputState = clearInput; elem.addEventListener('change', valueCheck); } // overriden in bindClearValueButton handleCurrentInputState() { } // overriden in bindClearValueButton clearInputState() { } render(h) { if (FormItemWrapperConfig.customRender != null) { return FormItemWrapperConfig.customRender.call( this, h, this, ); } if (this.wrap != false) { const isHintLabel = this.hint && this.hintType == HintType.Label; return (
{this.maxWidth == null ? this.renderInnerContent(h) : (
{this.renderInnerContent(h)}
)}
); } else { return (
{this.renderInnerContent(h)}
); } } renderInnerContent(h) { return (
{this.prependIcon &&
{ this.handlePrependIconClicked(); }}>
} {this.$slots.default?.()} {this.hint && this.hintType != HintType.Label &&
{ this.handleAppendClicked(); }} title={this.hint} class="input-group-append" style="cursor: help;">
} {this.showClearValueButton && } {this.appendIcon && this.renderAppendIcon()} {this.hasValidationError && } {!isNullOrEmpty(this.subtitle) && {this.subtitle}}
); } } const FormItemWrapper = toNative(FormItemWrapperComponent); export default FormItemWrapper;