Skip to content

Commit 9cf2310

Browse files
committed
refact: Migrate $helper.field to TypeScript
1 parent e392165 commit 9cf2310

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { describe, expect, it } from "vitest";
22
import { form } from "./field";
33

44
describe.concurrent("$helper.field.form()", () => {
5+
// TODO: Remove once window.panel is globally typed
6+
// @ts-expect-error
57
// mock the app with the component setup
68
window.panel = {
79
app: {
Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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
6-
*
7-
* @param {Object} field
8-
* @returns {mixed}
98
*/
10-
export function defaultValue(field) {
9+
export function defaultValue(field: Field): unknown {
1110
if (field.default !== undefined) {
1211
return clone(field.default);
1312
}
1413

15-
const component =
16-
window.panel.app.$options.components[`k-${field.type}-field`];
17-
14+
// TODO: Remove once window.panel is globally typed
15+
// @ts-expect-error - window.panel has no type yet
16+
const options = window.panel.app.$options;
17+
const component = options.components[`k-${field.type}-field`];
1818
const valueProp = component?.options.props?.value;
1919

2020
// if the field has no value prop,
@@ -40,12 +40,9 @@ export function defaultValue(field) {
4040
/**
4141
* Creates form values for provided fields
4242
* @unstable
43-
*
44-
* @param {Object} fields
45-
* @returns {Object}
4643
*/
47-
export function form(fields) {
48-
const form = {};
44+
export function form(fields: Record<string, Field>): Record<string, unknown> {
45+
const form: Record<string, unknown> = {};
4946

5047
for (const fieldName in fields) {
5148
const defaultVal = defaultValue(fields[fieldName]);
@@ -63,11 +60,13 @@ export function form(fields) {
6360
* and the current form values. Also works for sections.
6461
* @unstable
6562
*
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
63+
* @param field - The form field object
64+
* @param values - The current form values object
6965
*/
70-
export function isVisible(field, values) {
66+
export function isVisible(
67+
field: Field,
68+
values: Record<string, unknown>
69+
): boolean {
7170
if (field.type === "hidden" || field.hidden === true) {
7271
return false;
7372
}
@@ -76,9 +75,11 @@ export function isVisible(field, values) {
7675
return true;
7776
}
7877

79-
for (const key in field.when) {
78+
const when = field.when as Record<string, unknown>;
79+
80+
for (const key in when) {
8081
const value = values[key.toLowerCase()];
81-
const condition = field.when[key];
82+
const condition = when[key];
8283

8384
// if condition is checking for empty field
8485
if (
@@ -105,19 +106,28 @@ export function isVisible(field, values) {
105106
* @param {object} fields
106107
* @returns {object}
107108
*/
108-
export function subfields(field, fields) {
109-
let subfields = {};
109+
export function subfields(
110+
field: Field,
111+
fields: Record<string, Field>
112+
): Record<string, Field> {
113+
let subfields: Record<string, Field> = {};
110114

111115
for (const name in fields) {
112116
const subfield = fields[name];
113117

114118
subfield.section = field.name;
115119

116120
if (field.endpoints) {
121+
const endpoints = field.endpoints as {
122+
field: string;
123+
section: string;
124+
model: string;
125+
};
126+
117127
subfield.endpoints = {
118-
field: field.endpoints.field + "+" + name,
119-
section: field.endpoints.section,
120-
model: field.endpoints.model
128+
field: endpoints.field + "+" + name,
129+
section: endpoints.section,
130+
model: endpoints.model
121131
};
122132
}
123133

panel/src/helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import clipboard from "./clipboard.js";
33
import color from "./color.js";
44
import debounce from "./debounce";
55
import embed from "./embed";
6-
import field from "./field.js";
6+
import field from "./field";
77
import file from "./file.js";
88
import focus from "./focus.js";
99
import isComponent from "./isComponent";

0 commit comments

Comments
 (0)