UNPKG

react-antd-admin-panel

Version:

Modern TypeScript-first React admin panel builder with Ant Design 6

166 lines 4.41 kB
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime"; import React from 'react'; import { BaseBuilder } from '../base/BaseBuilder'; /** * Condition - Conditional rendering builder * Renders content based on boolean conditions * * @example * // Simple condition * const cond = new Condition() * .when(() => user.isAdmin, <AdminPanel />) * .otherwise(<AccessDenied />); * * // Multiple conditions (switch-like) * const cond = new Condition() * .add('admin', (data) => data.role === 'admin', <AdminView />) * .add('user', (data) => data.role === 'user', <UserView />) * .default(<GuestView />); */ export class Condition extends BaseBuilder { _items = []; _default; _data; /** * Add a conditional item */ add(key, condition, content) { this._items.push({ key, condition, content }); return this; } /** * Add a condition with content (fluent API) */ when(condition, content) { this._items.push({ condition, content }); return this; } /** * Set default content when no conditions match */ default(content) { this._default = content; return this; } /** * Alias for default() */ otherwise(content) { return this.default(content); } /** * Set data to evaluate conditions against */ data(value) { this._data = value; return this; } /** * Check which condition matches and return its content */ _resolveContent() { // Find first matching condition for (const item of this._items) { if (item.condition(this._data)) { return typeof item.content === 'function' ? item.content() : item.content; } } // No match, return default if (this._default) { return typeof this._default === 'function' ? this._default() : this._default; } return null; } /** * Clear all conditions */ clear() { this._items = []; this._default = undefined; return this; } /** * Get count of conditions */ count() { return this._items.length; } /** * Render the matching content */ render() { if (this._config.hidden) { return null; } return this._resolveContent(); } } /** * ConditionGroup - Manage multiple conditions that can dynamically update * Useful for form-driven UIs where different sections appear based on user input * * @example * const group = new ConditionGroup() * .add('basic', (data) => data.type === 'basic', basicSection) * .add('advanced', (data) => data.type === 'advanced', advancedSection) * .checkCondition(formData); // Updates active conditions */ export class ConditionGroup extends BaseBuilder { _conditions = new Map(); _activeKeys = []; /** * Add a named condition */ add(key, condition, content) { this._conditions.set(key, { key, condition, content }); return this; } /** * Check conditions and update active items */ checkCondition(data) { this._activeKeys = []; for (const [key, item] of this._conditions.entries()) { if (item.condition(data)) { this._activeKeys.push(key); } } return this; } /** * Get currently active condition keys */ getActiveKeys() { return [...this._activeKeys]; } /** * Clear all conditions */ clear() { this._conditions.clear(); this._activeKeys = []; return this; } /** * Render all matching conditions */ render() { if (this._config.hidden) { return null; } return (_jsx(_Fragment, { children: this._activeKeys.map((key, index) => { const item = this._conditions.get(key); if (!item) return null; const content = typeof item.content === 'function' ? item.content() : item.content; return _jsx(React.Fragment, { children: content }, key || index); }) })); } } //# sourceMappingURL=Condition.js.map