File tree 6 files changed +106
-14
lines changed
docs/src/routes/docs/(qwik)/components/rendering
6 files changed +106
-14
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @qwik.dev/core ' : patch
3
+ ---
4
+
5
+ fix: input's value is string when passing number
Original file line number Diff line number Diff line change @@ -180,11 +180,17 @@ The `bind:` is compiled away by the Qwik optimizer to a property set and an even
180
180
import { component$ , useSignal } from ' @qwik.dev/core' ;
181
181
182
182
export default component$ (() => {
183
+ const count = useSignal (0 );
183
184
const firstName = useSignal (' ' );
184
185
const acceptConditions = useSignal (false );
185
186
186
187
return (
187
188
<form >
189
+ { /* For number inputs, use `valueAsNumber` instead of `value` to get the numeric value directly */ }
190
+ <input type = " number"
191
+ value = { count .value }
192
+ onInput$ = { (_ , el ) => count .value = el .valueAsNumber }
193
+ />
188
194
<input type = " text"
189
195
value = { firstName .value }
190
196
onInput$ = { (_ , el ) => firstName .value = el .value }
Original file line number Diff line number Diff line change @@ -596,7 +596,7 @@ type SpecialAttrs = {
596
596
* For type: HTMLInputTypeAttribute, excluding 'button' | 'reset' | 'submit' | 'checkbox' |
597
597
* 'radio'
598
598
*/
599
- 'bind:value' ?: Signal < string | undefined > ;
599
+ 'bind:value' ?: Signal < string | undefined | number > ;
600
600
enterKeyHint ?: 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send' | undefined ;
601
601
height ?: Size | undefined ;
602
602
max ?: number | string | undefined ;
Original file line number Diff line number Diff line change @@ -651,5 +651,44 @@ describe.each([
651
651
</ Component >
652
652
) ;
653
653
} ) ;
654
+
655
+ // help me to get a description
656
+ it ( 'should update the sum when input values change' , async ( ) => {
657
+ const AppTest = component$ ( ( ) => {
658
+ const a = useSignal ( 1 ) ;
659
+ const b = useSignal ( 2 ) ;
660
+ return (
661
+ < div >
662
+ { a . value } + { b . value } = { a . value + b . value }
663
+ < input type = "number" id = "input1" bind :value = { a } />
664
+ < input type = "number" id = "input2" bind :value = { b } />
665
+ </ div >
666
+ ) ;
667
+ } ) ;
668
+
669
+ const { document } = await render ( < AppTest /> , { debug : debug } ) ;
670
+
671
+ await expect ( document . querySelector ( 'div' ) ) . toMatchDOM (
672
+ < div >
673
+ 1 + 2 = 3
674
+ < input type = "number" id = "input1" value = "1" />
675
+ < input type = "number" id = "input2" value = "2" />
676
+ </ div >
677
+ ) ;
678
+
679
+ const input1 = document . querySelector ( '#input1' ) ! as HTMLInputElement ;
680
+
681
+ input1 . value = '10' ;
682
+ input1 . valueAsNumber = 10 ;
683
+
684
+ await trigger ( document . body , input1 , 'input' ) ;
685
+ await expect ( document . querySelector ( 'div' ) ) . toMatchDOM (
686
+ < div >
687
+ 10 + 2 = 12
688
+ < input type = "number" id = "input1" value = "10" />
689
+ < input type = "number" id = "input2" value = "2" />
690
+ </ div >
691
+ ) ;
692
+ } ) ;
654
693
} ) ;
655
694
} ) ;
Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ export const Greeter = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl(()=>{
42
42
" value" : value ,
43
43
" onInput$" : /* #__PURE__*/ inlinedQrl ((_ , elm )=> {
44
44
const [value] = useLexicalScope ();
45
- return value .value = elm .value ;
45
+ return value .value = elm .type == " number " ? elm . valueAsNumber : elm . value ;
46
46
}, " s_6IZeYpXCNXA" , [
47
47
value
48
48
])
@@ -51,7 +51,7 @@ export const Greeter = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl(()=>{
51
51
" checked" : checked ,
52
52
" onInput$" : /* #__PURE__*/ inlinedQrl ((_ , elm )=> {
53
53
const [checked] = useLexicalScope ();
54
- return checked .value = elm .checked ;
54
+ return checked .value = elm .type == " number " ? elm . valueAsNumber : elm . checked ;
55
55
}, " s_JPI3bLCVnso" , [
56
56
checked
57
57
])
@@ -60,7 +60,7 @@ export const Greeter = /*#__PURE__*/ componentQrl(/*#__PURE__*/ inlinedQrl(()=>{
60
60
" stuff" : stuff ,
61
61
" onChange$" : /* #__PURE__*/ inlinedQrl ((_ , elm )=> {
62
62
const [stuff] = useLexicalScope ();
63
- return stuff .value = elm .stuff ;
63
+ return stuff .value = elm .type == " number " ? elm . valueAsNumber : elm . stuff ;
64
64
}, " s_eyREJ0lZTFw" , [
65
65
stuff
66
66
])
Original file line number Diff line number Diff line change @@ -1311,17 +1311,59 @@ impl<'a> QwikTransform<'a> {
1311
1311
) ,
1312
1312
) ,
1313
1313
op : ast:: AssignOp :: Assign ,
1314
- right : Box :: new ( ast:: Expr :: Member (
1315
- ast:: MemberExpr {
1316
- obj : Box :: new ( ast:: Expr :: Ident ( elm) ) ,
1317
- prop : ast:: MemberProp :: Ident (
1318
- ast:: IdentName :: new (
1319
- prop_name, DUMMY_SP ,
1320
- ) ,
1321
- ) ,
1314
+ right : Box :: new ( ast:: Expr :: Cond ( ast:: CondExpr {
1315
+ test : Box :: new ( ast:: Expr :: Bin ( ast:: BinExpr {
1316
+ left : Box :: new ( ast:: Expr :: Member (
1317
+ ast:: MemberExpr {
1318
+ obj : Box :: new ( ast:: Expr :: Ident (
1319
+ elm. clone ( ) ,
1320
+ ) ) ,
1321
+ prop : ast:: MemberProp :: Ident (
1322
+ ast:: IdentName :: new (
1323
+ "type" . into ( ) ,
1324
+ DUMMY_SP ,
1325
+ ) ,
1326
+ ) ,
1327
+ span : DUMMY_SP ,
1328
+ } ,
1329
+ ) ) ,
1322
1330
span : DUMMY_SP ,
1323
- } ,
1324
- ) ) ,
1331
+ op : ast:: BinaryOp :: EqEq ,
1332
+ right : Box :: new ( ast:: Expr :: Lit (
1333
+ ast:: Lit :: Str ( ast:: Str {
1334
+ value : "number" . into ( ) ,
1335
+ span : DUMMY_SP ,
1336
+ raw : None ,
1337
+ } ) ,
1338
+ ) ) ,
1339
+ } ) ) ,
1340
+ cons : Box :: new ( ast:: Expr :: Member (
1341
+ ast:: MemberExpr {
1342
+ obj : Box :: new ( ast:: Expr :: Ident (
1343
+ elm. clone ( ) ,
1344
+ ) ) ,
1345
+ prop : ast:: MemberProp :: Ident (
1346
+ ast:: IdentName :: new (
1347
+ "valueAsNumber" . into ( ) ,
1348
+ DUMMY_SP ,
1349
+ ) ,
1350
+ ) ,
1351
+ span : DUMMY_SP ,
1352
+ } ,
1353
+ ) ) ,
1354
+ alt : Box :: new ( ast:: Expr :: Member (
1355
+ ast:: MemberExpr {
1356
+ obj : Box :: new ( ast:: Expr :: Ident ( elm) ) ,
1357
+ prop : ast:: MemberProp :: Ident (
1358
+ ast:: IdentName :: new (
1359
+ prop_name, DUMMY_SP ,
1360
+ ) ,
1361
+ ) ,
1362
+ span : DUMMY_SP ,
1363
+ } ,
1364
+ ) ) ,
1365
+ span : DUMMY_SP ,
1366
+ } ) ) ,
1325
1367
span : DUMMY_SP ,
1326
1368
} ) ,
1327
1369
) ) ) ,
You can’t perform that action at this time.
0 commit comments