1- import { clone } from "./object.js" ;
1+ import { clone } from "./object" ;
2+
3+ type Field = Record < string , unknown > ;
24
35/**
46 * Loads the default value for a field definition
57 * @unstable
68 *
7- * @param {Object } field
8- * @returns {mixed }
9+ * @example
10+ * defaultValue({ type: "text", default: "Hello" }) // => "Hello"
11+ * defaultValue({ type: "text" }) // => null
912 */
10- export function defaultValue ( field ) {
13+ export function defaultValue ( field : Field ) : unknown {
1114 if ( field . default !== undefined ) {
1215 return clone ( field . default ) ;
1316 }
1417
15- const component =
16- window . panel . app . $options . components [ `k-${ field . type } -field` ] ;
17-
18+ // TODO: Remove once window.panel is globally typed
19+ // @ts -expect-error - window.panel has no type yet
20+ const options = window . panel . app . $options ;
21+ const component = options . components [ `k-${ field . type } -field` ] ;
1822 const valueProp = component ?. options . props ?. value ;
1923
2024 // if the field has no value prop,
@@ -41,11 +45,12 @@ export function defaultValue(field) {
4145 * Creates form values for provided fields
4246 * @unstable
4347 *
44- * @param {Object } fields
45- * @returns {Object }
48+ * @example
49+ * form({ title: { type: "text", default: "Hello" }, age: { type: "number" } })
50+ * // => { title: "Hello" }
4651 */
47- export function form ( fields ) {
48- const form = { } ;
52+ export function form ( fields : Record < string , Field > ) : Record < string , unknown > {
53+ const form : Record < string , unknown > = { } ;
4954
5055 for ( const fieldName in fields ) {
5156 const defaultVal = defaultValue ( fields [ fieldName ] ) ;
@@ -63,11 +68,17 @@ export function form(fields) {
6368 * and the current form values. Also works for sections.
6469 * @unstable
6570 *
66- * @param {Object } field - The form field object
67- * @param {Object } values - The current form values object
68- * @returns {boolean } - Whether the field is visible or not
71+ * @example
72+ * isVisible({ type: "text", when: { status: "draft" } }, { status: "draft" }) // => true
73+ * isVisible({ type: "text", when: { status: "draft" } }, { status: "published" }) // => false
74+ *
75+ * @param field - The form field object
76+ * @param values - The current form values object
6977 */
70- export function isVisible ( field , values ) {
78+ export function isVisible (
79+ field : Field ,
80+ values : Record < string , unknown >
81+ ) : boolean {
7182 if ( field . type === "hidden" || field . hidden === true ) {
7283 return false ;
7384 }
@@ -76,9 +87,11 @@ export function isVisible(field, values) {
7687 return true ;
7788 }
7889
79- for ( const key in field . when ) {
90+ const when = field . when as Record < string , unknown > ;
91+
92+ for ( const key in when ) {
8093 const value = values [ key . toLowerCase ( ) ] ;
81- const condition = field . when [ key ] ;
94+ const condition = when [ key ] ;
8295
8396 // if condition is checking for empty field
8497 if (
@@ -100,24 +113,29 @@ export function isVisible(field, values) {
100113 * Adds proper endpoint and section definitions
101114 * to subfields for a form field.
102115 * @unstable
103- *
104- * @param {object } field
105- * @param {object } fields
106- * @returns {object }
107116 */
108- export function subfields ( field , fields ) {
109- let subfields = { } ;
117+ export function subfields (
118+ field : Field ,
119+ fields : Record < string , Field >
120+ ) : Record < string , Field > {
121+ const subfields : Record < string , Field > = { } ;
110122
111123 for ( const name in fields ) {
112124 const subfield = fields [ name ] ;
113125
114126 subfield . section = field . name ;
115127
116128 if ( field . endpoints ) {
129+ const endpoints = field . endpoints as {
130+ field : string ;
131+ section : string ;
132+ model : string ;
133+ } ;
134+
117135 subfield . endpoints = {
118- field : field . endpoints . field + "+" + name ,
119- section : field . endpoints . section ,
120- model : field . endpoints . model
136+ field : endpoints . field + "+" + name ,
137+ section : endpoints . section ,
138+ model : endpoints . model
121139 } ;
122140 }
123141
0 commit comments