import { BaseComponent } from "./ave-base-component.js"; class AveInput extends BaseComponent { static get observedAttributes() { return ['type', 'placeholder', 'value', 'disabled', 'required', 'name', 'id', 'size', 'error']; } constructor() { super(); this._handleInput = this._handleInput.bind(this); this._handleChange = this._handleChange.bind(this); this._handleFocus = this._handleFocus.bind(this); this._handleBlur = this._handleBlur.bind(this); } // Property getters/setters get type() { return this.getAttribute('type') || 'text'; } set type(value) { this.setAttribute('type', value); } get placeholder() { return this.getAttribute('placeholder') || ''; } set placeholder(value) { this.setAttribute('placeholder', value); } get value() { const inputElement = this.shadowRoot?.querySelector('input'); return inputElement ? inputElement.value : (this._currentValue || this.getAttribute('value') || ''); } set value(value) { this._currentValue = value; this.setAttribute('value', value); const inputElement = this.shadowRoot?.querySelector('input'); if (inputElement) { inputElement.value = value; } } get disabled() { return this.hasAttribute('disabled'); } set disabled(value) { if (value === true || value === 'true' || value === '') { this.setAttribute('disabled', ''); } else { this.removeAttribute('disabled'); } } get required() { return this.hasAttribute('required'); } set required(value) { if (value === true || value === 'true' || value === '') { this.setAttribute('required', ''); } else { this.removeAttribute('required'); } } get name() { return this.getAttribute('name') || ''; } set name(value) { this.setAttribute('name', value); } get inputId() { return this.getAttribute('id') || ''; } set inputId(value) { this.setAttribute('id', value); } get size() { return this.getAttribute('size') || 'default'; } set size(value) { this.setAttribute('size', value); } get error() { return this.hasAttribute('error'); } set error(value) { if (value === true || value === 'true' || value === '') { this.setAttribute('error', ''); } else { this.removeAttribute('error'); } } setupEventListeners() { this._registerEvent('input', 'input', this._handleInput); this._registerEvent('input', 'change', this._handleChange); this._registerEvent('input', 'focus', this._handleFocus); this._registerEvent('input', 'blur', this._handleBlur); } _handleInput(e) { // Don't update the attribute during user input to prevent re-rendering // Just store the value internally and dispatch the event this._currentValue = e.target.value; this._dispatch('ave-input', { value: e.target.value, name: this.name, type: this.type }); } _handleChange(e) { this._dispatch('ave-change', { value: e.target.value, name: this.name, type: this.type }); } _handleFocus(e) { this._dispatch('ave-focus', { value: e.target.value, name: this.name }); } _handleBlur(e) { this._dispatch('ave-blur', { value: e.target.value, name: this.name }); } _getInputClasses() { const baseClasses = [ 'flex', 'w-full', 'rounded-md', 'border', 'border-input', 'bg-background', 'text-sm', 'ring-offset-background', 'file:border-0', 'file:bg-transparent', 'file:text-sm', 'file:font-medium', 'placeholder:text-muted-foreground', 'focus-visible:outline-none', 'focus-visible:ring-2', 'focus-visible:ring-ring', 'focus-visible:ring-offset-2', 'disabled:cursor-not-allowed', 'disabled:opacity-50' ]; // Size classes const sizeClasses = { 'sm': ['h-9', 'px-3', 'py-1'], 'default': ['h-10', 'px-3', 'py-2'], 'lg': ['h-11', 'px-3', 'py-3'] }; const selectedSizeClasses = sizeClasses[this.size] || sizeClasses['default']; baseClasses.push(...selectedSizeClasses); // Error state if (this.error) { baseClasses.push('border-destructive', 'focus-visible:ring-destructive'); } return baseClasses.join(' '); } render() { const currentValue = this.getAttribute('value') || ''; this.shadowRoot.innerHTML = ` `; this.setupEventListeners(); // Ensure the input element has the correct value after rendering const inputElement = this.shadowRoot.querySelector('input'); if (inputElement && currentValue) { inputElement.value = currentValue; } } } customElements.define('ave-input', AveInput);