Skip to content

Commit 30c24e6

Browse files
committed
Allow boolean and number form fields to be hidden
1 parent 0704908 commit 30c24e6

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

packages/kit/src/exports/public.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,8 @@ type InputTypeMap = {
18331833
radio: string;
18341834
file: File;
18351835
hidden: string;
1836+
'hidden boolean': boolean;
1837+
'hidden number': number;
18361838
submit: string;
18371839
button: string;
18381840
reset: string;
@@ -1890,7 +1892,11 @@ type AsArgs<Type extends keyof InputTypeMap, Value> = Type extends 'checkbox'
18901892
: [type: Type]
18911893
: Type extends 'radio' | 'submit' | 'hidden'
18921894
? [type: Type, value: Value | (string & {})]
1893-
: [type: Type];
1895+
: Type extends 'hidden number'
1896+
? [type: Type, value: Value | (number & {})]
1897+
: Type extends 'hidden boolean'
1898+
? [type: Type, value: Value | (boolean & {})]
1899+
: [type: Type];
18941900

18951901
/**
18961902
* Form field accessor type that provides name(), value(), and issues() methods

packages/kit/src/runtime/form-utils.svelte.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export function create_field_proxy(target, get_input, depend, set_input, get_iss
249249
if (prop === 'as') {
250250
/**
251251
* @param {string} type
252-
* @param {string} [input_value]
252+
* @param {string | number | boolean} [input_value]
253253
*/
254254
const as_func = (type, input_value) => {
255255
const is_array =
@@ -258,9 +258,9 @@ export function create_field_proxy(target, get_input, depend, set_input, get_iss
258258
(type === 'checkbox' && typeof input_value === 'string');
259259

260260
const prefix =
261-
type === 'number' || type === 'range'
261+
type === 'number' || type === 'range' || type === 'hidden number'
262262
? 'n:'
263-
: type === 'checkbox' && !is_array
263+
: (type === 'checkbox' && !is_array) || type === 'hidden boolean'
264264
? 'b:'
265265
: '';
266266

@@ -292,6 +292,32 @@ export function create_field_proxy(target, get_input, depend, set_input, get_iss
292292
});
293293
}
294294

295+
// Handle hidden number inputs
296+
if (type === 'hidden number') {
297+
if (DEV) {
298+
if (typeof input_value !== 'number') {
299+
throw new Error(`\`hidden number\` input must be a number value.`);
300+
}
301+
}
302+
303+
return Object.defineProperties(base_props, {
304+
value: { value: input_value, enumerable: true }
305+
});
306+
}
307+
308+
// Handle hidden boolean inputs
309+
if (type === 'hidden boolean') {
310+
if (DEV) {
311+
if (typeof input_value !== 'boolean') {
312+
throw new Error(`\`hidden boolean\` input must be a boolean value.`);
313+
}
314+
}
315+
316+
return Object.defineProperties(base_props, {
317+
value: { value: input_value && 'on', enumerable: true }
318+
});
319+
}
320+
295321
// Handle select inputs
296322
if (type === 'select' || type === 'select multiple') {
297323
return Object.defineProperties(base_props, {

packages/kit/types/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@ declare module '@sveltejs/kit' {
18091809
radio: string;
18101810
file: File;
18111811
hidden: string;
1812+
'hidden boolean': boolean;
1813+
'hidden number': number;
18121814
submit: string;
18131815
button: string;
18141816
reset: string;
@@ -1866,7 +1868,11 @@ declare module '@sveltejs/kit' {
18661868
: [type: Type]
18671869
: Type extends 'radio' | 'submit' | 'hidden'
18681870
? [type: Type, value: Value | (string & {})]
1869-
: [type: Type];
1871+
: Type extends 'hidden number'
1872+
? [type: Type, value: Value | (number & {})]
1873+
: Type extends 'hidden boolean'
1874+
? [type: Type, value: Value | (boolean & {})]
1875+
: [type: Type];
18701876

18711877
/**
18721878
* Form field accessor type that provides name(), value(), and issues() methods

0 commit comments

Comments
 (0)