|
| 1 | +import { computed, Directive, effect, inject, input } from '@angular/core'; |
| 2 | +import { HlmButton, provideBrnButtonConfig } from '@spartan-ng/helm/button'; |
| 3 | +import { hlm } from '@spartan-ng/helm/utils'; |
| 4 | +import { cva, type VariantProps } from 'class-variance-authority'; |
| 5 | +import type { ClassValue } from 'clsx'; |
| 6 | + |
| 7 | +const inputGroupAddonVariants = cva('flex items-center gap-2 text-sm shadow-none', { |
| 8 | + variants: { |
| 9 | + size: { |
| 10 | + xs: "h-6 gap-1 rounded-[calc(var(--radius)-5px)] px-2 has-[>ng-icon]:px-2 [&>ng-icon:not([class*='text-'])]:text-sm", |
| 11 | + sm: 'h-8 gap-1.5 rounded-md px-2.5 has-[>ng-icon]:px-2.5', |
| 12 | + 'icon-xs': 'size-6 rounded-[calc(var(--radius)-5px)] p-0 has-[>ng-icon]:p-0', |
| 13 | + 'icon-sm': 'size-8 p-0 has-[>ng-icon]:p-0', |
| 14 | + }, |
| 15 | + }, |
| 16 | + defaultVariants: { |
| 17 | + size: 'xs', |
| 18 | + }, |
| 19 | +}); |
| 20 | + |
| 21 | +type InputGroupAddonVariants = VariantProps<typeof inputGroupAddonVariants>; |
| 22 | + |
| 23 | +@Directive({ |
| 24 | + selector: 'button[hlmInputGroupButton]', |
| 25 | + providers: [ |
| 26 | + provideBrnButtonConfig({ |
| 27 | + variant: 'ghost', |
| 28 | + }), |
| 29 | + ], |
| 30 | + hostDirectives: [ |
| 31 | + { |
| 32 | + directive: HlmButton, |
| 33 | + inputs: ['variant'], |
| 34 | + }, |
| 35 | + ], |
| 36 | + host: { |
| 37 | + '[attr.data-size]': 'size()', |
| 38 | + '[type]': 'type()', |
| 39 | + }, |
| 40 | +}) |
| 41 | +export class HlmInputGroupButton { |
| 42 | + private readonly _hlmButton = inject(HlmButton); |
| 43 | + public readonly size = input<InputGroupAddonVariants['size']>('xs'); |
| 44 | + public readonly userClass = input<ClassValue>('', { alias: 'class' }); |
| 45 | + public readonly type = input<'button' | 'submit' | 'reset'>('button'); |
| 46 | + |
| 47 | + protected readonly _computedClass = computed(() => |
| 48 | + hlm(inputGroupAddonVariants({ size: this.size() }), this.userClass()), |
| 49 | + ); |
| 50 | + |
| 51 | + constructor() { |
| 52 | + effect(() => { |
| 53 | + this._hlmButton.setClass(this._computedClass()); |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
0 commit comments