11import { css , html } from "lit" ;
22import { ifDefined } from "lit/directives/if-defined.js" ;
33import { BaseElement } from "../../core/base-element.ts" ;
4+ import { type Cell , isCell } from "@commontools/runner" ;
45
56/**
67 * CTInput - Text input field with support for various types and validation
@@ -9,7 +10,7 @@ import { BaseElement } from "../../core/base-element.ts";
910 *
1011 * @attr {string} type - Input type: "text" | "email" | "password" | "number" | "search" | "tel" | "url" | "date" | "time" | "datetime-local"
1112 * @attr {string} placeholder - Placeholder text
12- * @attr {string} value - Input value
13+ * @attr {string|Cell<string> } value - Input value (supports both plain string and Cell<string>)
1314 * @attr {boolean} disabled - Whether the input is disabled
1415 * @attr {boolean} readonly - Whether the input is read-only
1516 * @attr {boolean} required - Whether the input is required
@@ -182,7 +183,7 @@ export class CTInput extends BaseElement {
182183
183184 declare type : InputType ;
184185 declare placeholder : string ;
185- declare value : string ;
186+ declare value : Cell < string > | string ;
186187 declare disabled : boolean ;
187188 declare readonly : boolean ;
188189 declare error : boolean ;
@@ -203,7 +204,6 @@ export class CTInput extends BaseElement {
203204 super ( ) ;
204205 this . type = "text" ;
205206 this . placeholder = "" ;
206- this . value = "" ;
207207 this . disabled = false ;
208208 this . readonly = false ;
209209 this . error = false ;
@@ -226,6 +226,21 @@ export class CTInput extends BaseElement {
226226 return this . _input ;
227227 }
228228
229+ private getValue ( ) : string {
230+ if ( isCell ( this . value ) ) {
231+ return this . value . get ?.( ) || "" ;
232+ }
233+ return this . value || "" ;
234+ }
235+
236+ private setValue ( newValue : string ) : void {
237+ if ( isCell ( this . value ) ) {
238+ this . value . set ( newValue ) ;
239+ } else {
240+ this . value = newValue ;
241+ }
242+ }
243+
229244 override firstUpdated ( ) {
230245 // Cache the input element reference
231246 this . _input = this . shadowRoot ?. querySelector ( "input" ) || null ;
@@ -241,7 +256,7 @@ export class CTInput extends BaseElement {
241256 type ="${ this . type } "
242257 class ="${ this . error ? "error" : "" } "
243258 placeholder ="${ ifDefined ( this . placeholder || undefined ) } "
244- .value ="${ this . value } "
259+ .value ="${ this . getValue ( ) } "
245260 ?disabled ="${ this . disabled } "
246261 ?readonly ="${ this . readonly } "
247262 ?required ="${ this . required } "
@@ -265,54 +280,60 @@ export class CTInput extends BaseElement {
265280
266281 private _handleInput ( event : Event ) {
267282 const input = event . target as HTMLInputElement ;
268- const oldValue = this . value ;
269- this . value = input . value ;
283+ const oldValue = this . getValue ( ) ;
284+ this . setValue ( input . value ) ;
270285
271286 // Emit custom input event
272287 this . emit ( "ct-input" , {
273288 value : input . value ,
274289 oldValue,
290+ name : this . name ,
275291 } ) ;
276292 }
277293
278294 private _handleChange ( event : Event ) {
279295 const input = event . target as HTMLInputElement ;
280- const oldValue = this . value ;
281- this . value = input . value ;
296+ const oldValue = this . getValue ( ) ;
297+ this . setValue ( input . value ) ;
282298
283299 // Emit custom change event
284300 this . emit ( "ct-change" , {
285301 value : input . value ,
286302 oldValue,
303+ name : this . name ,
287304 } ) ;
288305 }
289306
290307 private _handleFocus ( _event : Event ) {
291308 this . emit ( "ct-focus" , {
292- value : this . value ,
309+ value : this . getValue ( ) ,
310+ name : this . name ,
293311 } ) ;
294312 }
295313
296314 private _handleBlur ( _event : Event ) {
297315 this . emit ( "ct-blur" , {
298- value : this . value ,
316+ value : this . getValue ( ) ,
317+ name : this . name ,
299318 } ) ;
300319 }
301320
302321 private _handleKeyDown ( event : KeyboardEvent ) {
303322 this . emit ( "ct-keydown" , {
304323 key : event . key ,
305- value : this . value ,
324+ value : this . getValue ( ) ,
306325 shiftKey : event . shiftKey ,
307326 ctrlKey : event . ctrlKey ,
308327 metaKey : event . metaKey ,
309328 altKey : event . altKey ,
329+ name : this . name ,
310330 } ) ;
311331
312332 // Special handling for Enter key
313333 if ( event . key === "Enter" ) {
314334 this . emit ( "ct-submit" , {
315- value : this . value ,
335+ value : this . getValue ( ) ,
336+ name : this . name ,
316337 } ) ;
317338 }
318339 }
0 commit comments