|
| 1 | +import '@patternfly/elements/pf-icon/pf-icon.js'; |
| 2 | +import { LitElement, html, type TemplateResult } from 'lit'; |
| 3 | +import { customElement } from 'lit/decorators/custom-element.js'; |
| 4 | +import { property } from 'lit/decorators/property.js'; |
| 5 | +import { ifDefined } from 'lit/directives/if-defined.js'; |
| 6 | + |
| 7 | +import styles from './pf-helper-text.css'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Helper Text Statuses |
| 11 | + */ |
| 12 | +export type HelperTextStatus = 'default' | 'success' | 'warning' | 'error' | 'indeterminate'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Helper Text |
| 16 | + * @slot icon - Custom icon to override default |
| 17 | + * @slot - Place element content here |
| 18 | + */ |
| 19 | +@customElement('pf-helper-text') |
| 20 | +export class PfHelperText extends LitElement { |
| 21 | + static readonly styles: CSSStyleSheet[] = [styles]; |
| 22 | + |
| 23 | + /** |
| 24 | + * Defines the helper text status and its corresponding icon. |
| 25 | + * Options include 'default', 'success', 'warning', 'error', 'indeterminate'. |
| 26 | + */ |
| 27 | + @property({ attribute: 'status' }) |
| 28 | + status: HelperTextStatus = 'default'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The icon name to use. Overrides the default icon for a given status. |
| 32 | + * Requires pf-icon to be imported. |
| 33 | + */ |
| 34 | + @property({ attribute: 'icon' }) |
| 35 | + icon: string | undefined; |
| 36 | + |
| 37 | + /** |
| 38 | + * The icon set to use, e.g., 'fas' for Font Awesome Solid. |
| 39 | + * Required when using the 'icon' property. |
| 40 | + */ |
| 41 | + @property({ attribute: 'icon-set' }) |
| 42 | + iconSet: string | undefined; |
| 43 | + |
| 44 | + /** |
| 45 | + * מחשב את האייקון שיוצג, בהתבסס על הסטטוס או המאפיין icon |
| 46 | + */ |
| 47 | + private get _iconName(): string | undefined { |
| 48 | + // אם המשתמש הגדיר אייקון ספציפי, השתמש בו |
| 49 | + if (this.icon) { |
| 50 | + return this.icon; |
| 51 | + } |
| 52 | + |
| 53 | + // אם לא הוגדר אייקון ספציפי, החזר אייקון ברירת מחדל לפי הסטטוס |
| 54 | + switch (this.status) { |
| 55 | + case 'success': |
| 56 | + return 'circle-check'; |
| 57 | + case 'warning': |
| 58 | + return 'exclamation-triangle'; |
| 59 | + case 'error': |
| 60 | + return 'exclamation-circle'; |
| 61 | + case 'indeterminate': |
| 62 | + return 'minus-circle'; |
| 63 | + default: |
| 64 | + return undefined; // במצב 'default' אין אייקון ברירת מחדל |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // בקובץ pf-helper-text.ts |
| 69 | + render(): TemplateResult<1> { |
| 70 | + const iconName = this._iconName; // מקבל את שם האייקון או undefined |
| 71 | + const hasIcon = !!iconName; |
| 72 | + |
| 73 | + return html` |
| 74 | + |
| 75 | + <slot name="icon"> |
| 76 | + |
| 77 | + ${hasIcon ? |
| 78 | + html` |
| 79 | + <pf-icon icon="${iconName}" set="${ifDefined(this.iconSet)}" ></pf-icon> |
| 80 | + |
| 81 | + ` |
| 82 | + : ''} |
| 83 | + </slot> |
| 84 | + |
| 85 | + <slot></slot> |
| 86 | + `; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +declare global { |
| 91 | + interface HTMLElementTagNameMap { |
| 92 | + 'pf-helper-text': PfHelperText; |
| 93 | + } |
| 94 | +} |
0 commit comments