Skip to content

Button Form Control

Example

let dbMasterConfig: T.IDBMasterConfig = {
    form: {
        fields: [
            [{
                label: ' ',
                control: T.EDBMasterFormControl.button,
                buttonSettings: {
                    label: 'Reload',
                    cssClass: 'font-monospace',
                    // link: true,
                    icon: 'pi pi-check',
                    iconPos: 'right',
                    size: 'small',
                    // severity: 'warning',
                    style: {
                        color: 'white',
                        width: '100px',
                    },
                    jsCode: [{
                        appendTo: T.EDBMasterButtonAppendTo.click,
                        code: `
                            // console.log(column);
                            // console.log(config);
                            // column.buttonSettings.disabled = true;

                            console.log(utils);
                            utils.messageService.showSuccessToast('hello', {life: 5000});
                            utils.errorsMap['product_categories_lookup_id'] = 'error from custom code';
                        `,
                    }]
                }
            }]
        ]
    }
};

Interface Documentation

/**
 * Form field configuration for UI controls.
 * Defines the appearance, behavior, and validation of individual form inputs.
 */
export interface IDBMasterConfigFormField {
    /**
     * Unique identifier for programmatic element access.
     * Useful for dynamic field manipulation.
     */
    hiddenId?: string;
    /** Display label for the form field. */
    label?: string;

    /**
     * Help text displayed below the control.
     * Supports HTML formatting for rich content.
     */
    helpText?: string;

    /**
     * Database field path where the control value is stored.
     * Supports nested paths using dot notation.
     * @example 'name', 'address.city', 'user.contact.email'
     */
    path?: string;

    /** Type of UI control to render. */
    control?: EDBMasterFormControl;

    /**
     * CSS classes for the parent div wrapping the control.
     * @default 'col-lg mt-4 col-md-{calculated based on columns.length}'
     */
    cssClassDiv?: string;

    /**
     * Auto-focus this control when form opens.
     * @default false
     */
    autofocus?: boolean;

    /**
     * Disable the control or use expression to conditionally disable.
     * When a string is provided, it's evaluated as JavaScript.
     * @example true | false | "formData.type === 'readonly'"
     */
    disabled?: boolean | string;

    /**
     * Control visibility or use expression to conditionally show/hide.
     * When a string is provided, it's evaluated as JavaScript.
     * @default true
     * @example true | false | "formData.userRole === 'admin'"
     */
    visible?: boolean | string;

    /**
     * Nested form fields for complex layouts.
     * Enables hierarchical form structures within this field.
     */
    fields?: IDBMasterConfigFormField[][];

    /** Validation rules for this field. */
    validations?: Pick<IPropertyValidation, 'required'> & {
        /**
         * Dynamic required validation function.
         * Evaluated when form data changes to determine if field is required.
         * Note: When present, this takes precedence over static 'required' property.
         * @example "formData.type === 'individual' ? true : false"
         */
        requiredFun?: string;
    };
    /** Custom validation error messages. */
    validationErrors?: {
        /** Custom error message for required field validation. */
        required?: string;
    };

    /**
     * Button control configuration.
     * Clickable button with icon, loading state, and style variants.
     *
     * **Features:**
     * - Icon support with positioning (left/right/top/bottom)
     * - Loading state with custom icon
     * - Multiple severity levels (primary, success, info, warning, danger)
     * - Style variants (raised, rounded, text, outlined, link)
     * - Badge support
     * - Size options (small, large)
     * - Custom click handlers
     *
     * @example
     * // Basic button
     * buttonSettings: {
     *     label: 'Submit',
     *     severity: 'primary',
     *     icon: 'pi pi-check'
     * }
     *
     * @example
     * // Loading button
     * buttonSettings: {
     *     label: 'Processing...',
     *     loading: true,
     *     loadingIcon: 'pi pi-spin pi-spinner',
     *     severity: 'info'
     * }
     *
     * @example
     * // Outlined button with badge
     * buttonSettings: {
     *     label: 'Notifications',
     *     icon: 'pi pi-bell',
     *     outlined: true,
     *     badge: '5',
     *     badgeClass: 'p-badge-danger'
     * }
     *
     * @example
     * // Rounded icon button
     * buttonSettings: {
     *     label: '',
     *     icon: 'pi pi-plus',
     *     rounded: true,
     *     raised: true,
     *     severity: 'success',
     *     size: 'large'
     * }
     */
    buttonSettings?: {
        /** button label text */
        label: string;

        /** Give style object in angular style. */
        style?: any;

        /** custom CSS class to assign to control */
        cssClass?: string,

        /** Add a link style to the button. */
        link?: boolean;

        /** ex: pi pi-check , give icon to button */
        icon?: string,

        /** default : left */
        iconPos?: 'left' | 'right' | 'top' | 'bottom';

        /** Whether the button is in loading state. */
        loading?: boolean;

        /** Icon to display in loading state. */
        loadingIcon?: string;

        /** Defines the style of the button. */
        severity?: uiMakerComponentSeverity | null | undefined;

        /** Add a shadow to indicate elevation. */
        raised?: boolean;

        /** Add a circular border radius to the button. */
        rounded?: boolean;

        /** Add a textual class to the button without a background initially. */
        text?: boolean;

        /** Add a border class without a background initially. */
        outlined?: boolean;

        /** Value of the badge. */
        badge?: string;

        /** Style class of the badge. */
        badgeClass?: string;

        /** Style class of the badge. */
        size?: 'small' | 'large' | undefined | null;

        /** Advisory information to display in a tooltip on hover. */
        tooltip?: string;

        /** Type of CSS position. */
        tooltipPosition?: 'left' | 'top' | 'bottom' | 'right';
        tooltipStyleClass?: string;

        jsCode?: {
            /**
             * Available variables:<br/>
             * formData: any = Entire form object<br/>
             * column: IDBMasterConfigFormField = Configuration of that form column. column.dropdownSettings?.dbData?.find will be query to get data. <br/>
             * allDropdownDataMap: {[path: string]: any[]} = Map of all dropdown data<br/>
             * globalData: any = User will send it using SET_GLOBAL_DATA_TO_USE_IN_ANY_SCRIPT event from parent. <br/>
             * utils: any = Common utility functions for user to use. <br/>
             * queryParams: any = Query params received from URL. <br/>
             * config: IDBMasterConfigFormField <br/>
             * event: any <br/>
             */
            appendTo: EDBMasterButtonAppendTo,
            /**
             * // dropdownData is available to use.
             *
             * // Return promise for long awaiting tasks.
             * new Promise(async (resolve, reject) => {
             *     await new Promise(r => setTimeout(r, 3000));
             *     dropdownData[0].name = 'Sample data';
             *     resolve();
             * });
             *
             * // Directly modify data of grid
             * dropdownData[0].name = 'Sample data';
             *
             * // Return function
             * (function setData() { dropdownData[0].name = 'Sample data'; } );
             *
             */
            code: string | (($scope: IDBMasterUIPageUtilsScope) => any),
        }[],
    };

}


/**
 * Validation rules for schema properties and form fields.
 * Define constraints that values must satisfy before being saved.
 */

export interface IPropertyValidation {
    /**
     * Field is mandatory and must have a non-null, non-empty value.
     * Applicable to all data types.
     */
    required?: boolean;
    /**
     * Minimum allowed value.
     * Applicable to: number, date
     */
    min?: number;
    /**
     * Maximum allowed value.
     * Applicable to: number, date
     */
    max?: number;
    /**
     * Minimum string/array length.
     * Applicable to: string, array
     */
    minLength?: number;
    /**
     * Maximum string/array length.
     * Applicable to: string, array
     */
    maxLength?: number;
    /**
     * Value must be unique across all records.
     * @deprecated API Maker maintains uniqueness internally.
     * Avoid using on tables with frequent updates due to performance impact.
     */
    unique?: boolean;
    /**
     * Validate email address format.
     * Applicable to: string
     */
    email?: boolean;
    /**
     * Custom validation function.
     * Return true if valid, false or error message if invalid.
     */
    validatorFun?: Function;

    /**
     * Enumeration constraint - value must be from this array.
     * @example ['active', 'inactive', 'pending']
     */
    enum?: any[];
}


export enum EDBMasterButtonAppendTo {
    visible = 'visible',
    disabled = 'disabled',
    click = 'click',
}


/**
 * Available form control types for UI generation.
 * Each control type has specific settings and behavior.
 */
export enum EDBMasterFormControl {
    /** Single-line text input. */
    input = 'input',
    /** Numeric input with spinner buttons and formatting. */
    inputNumber = 'inputNumber',
    /** Text input with pattern-based masking (phone, SSN, etc.). */
    inputMask = 'inputMask',
    /** One-time password multi-character input. */
    inputOtp = 'inputOtp',
    /** Password input with visibility toggle and strength meter. */
    password = 'password',
    /** Date and/or time picker with calendar popup. */
    date_picker = 'date_picker',
    /** Multi-line text input. */
    textarea = 'textarea',
    /** Rich text WYSIWYG editor. */
    editor = 'editor',
    /** Binary checkbox (true/false). */
    checkbox = 'checkbox',
    /** Radio button group for single selection. */
    radio = 'radio',
    /** Color picker with multiple format support. */
    color_picker = 'color_picker',
    /** Dropdown select with single selection. */
    dropdown = 'dropdown',
    /** Autocomplete with type-ahead search. */
    auto_complete = 'auto_complete',
    /** Multi-select dropdown with chip display. */
    multi_select = 'multi_select',
    /** File upload with validation. */
    file_upload = 'file_upload',
    /** Nested data grid for one-to-many relationships. */
    grid = 'grid',
    /** Visual separator line. */
    divider = 'divider',
    /** Star-based rating input. */
    rating = 'rating',
    /** Circular dial for numeric input. */
    knob = 'knob',

    /** Collapsible accordion panels (field container). */
    accordion = 'accordion',
    /** Tabbed view for organizing fields (field container). */
    tab_view = 'tab_view',

    /** Clickable button with custom actions. */
    button = 'button',
    /** Image display with preview. */
    image = 'image',
    /** Custom HTML content. */
    customHTML = 'customHTML',
}

export enum EDBMasterCustomActionButtonAppendTo {
    click = 'click',
}