Skip to content

Commit fbcee69

Browse files
committed
Merge branch 'develop-minor' into v6/develop
2 parents af16472 + 11bdb13 commit fbcee69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+522
-339
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
return [
4-
'source' => 'panel/src/helpers/object.js'
4+
'source' => 'panel/src/helpers/object.ts'
55
];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
return [
4-
'source' => 'panel/src/helpers/string.js'
4+
'source' => 'panel/src/helpers/string.ts'
55
];

panel/src/api/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { responder, safeFetch } from "@/panel/request.js";
2-
import { toLowerKeys } from "@/helpers/object.js";
2+
import { toLowerKeys } from "@/helpers/object";
33
import { ltrim, rtrim } from "@/helpers/string";
44

55
export default (api) => {

panel/src/components/Dialogs/Elements/Buttons.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
</template>
77

88
<script>
9-
import { isObject } from "@/helpers/object.js";
10-
119
export const props = {
1210
props: {
1311
/**
@@ -94,7 +92,7 @@ export default {
9492
return false;
9593
}
9694
97-
if (isObject(button) === false) {
95+
if (this.$helper.object.isObject(button) === false) {
9896
return defaults;
9997
}
10098
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 - window.panel is not typed yet
57
// mock the app with the component setup
68
window.panel = {
79
app: {
Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
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

18+
// TODO: Remove once window.panel is globally typed
19+
// @ts-expect-error - window.panel has no type yet
1520
const component = window.panel.app.component(`k-${field.type}-field`);
1621
const valueProp = component?.props?.value;
1722

@@ -39,11 +44,12 @@ export function defaultValue(field) {
3944
* Creates form values for provided fields
4045
* @unstable
4146
*
42-
* @param {Object} fields
43-
* @returns {Object}
47+
* @example
48+
* form({ title: { type: "text", default: "Hello" }, age: { type: "number" } })
49+
* // => { title: "Hello" }
4450
*/
45-
export function form(fields) {
46-
const form = {};
51+
export function form(fields: Record<string, Field>): Record<string, unknown> {
52+
const form: Record<string, unknown> = {};
4753

4854
for (const fieldName in fields) {
4955
const defaultVal = defaultValue(fields[fieldName]);
@@ -61,11 +67,17 @@ export function form(fields) {
6167
* and the current form values. Also works for sections.
6268
* @unstable
6369
*
64-
* @param {Object} field - The form field object
65-
* @param {Object} values - The current form values object
66-
* @returns {boolean} - Whether the field is visible or not
70+
* @example
71+
* isVisible({ type: "text", when: { status: "draft" } }, { status: "draft" }) // => true
72+
* isVisible({ type: "text", when: { status: "draft" } }, { status: "published" }) // => false
73+
*
74+
* @param field - The form field object
75+
* @param values - The current form values object
6776
*/
68-
export function isVisible(field, values) {
77+
export function isVisible(
78+
field: Field,
79+
values: Record<string, unknown>
80+
): boolean {
6981
if (field.type === "hidden" || field.hidden === true) {
7082
return false;
7183
}
@@ -74,9 +86,11 @@ export function isVisible(field, values) {
7486
return true;
7587
}
7688

77-
for (const key in field.when) {
89+
const when = field.when as Record<string, unknown>;
90+
91+
for (const key in when) {
7892
const value = values[key.toLowerCase()];
79-
const condition = field.when[key];
93+
const condition = when[key];
8094

8195
// if condition is checking for empty field
8296
if (
@@ -98,24 +112,29 @@ export function isVisible(field, values) {
98112
* Adds proper endpoint and section definitions
99113
* to subfields for a form field.
100114
* @unstable
101-
*
102-
* @param {object} field
103-
* @param {object} fields
104-
* @returns {object}
105115
*/
106-
export function subfields(field, fields) {
107-
let subfields = {};
116+
export function subfields(
117+
field: Field,
118+
fields: Record<string, Field>
119+
): Record<string, Field> {
120+
const subfields: Record<string, Field> = {};
108121

109122
for (const name in fields) {
110123
const subfield = fields[name];
111124

112125
subfield.section = field.name;
113126

114127
if (field.endpoints) {
128+
const endpoints = field.endpoints as {
129+
field: string;
130+
section: string;
131+
model: string;
132+
};
133+
115134
subfield.endpoints = {
116-
field: field.endpoints.field + "+" + name,
117-
section: field.endpoints.section,
118-
model: field.endpoints.model
135+
field: endpoints.field + "+" + name,
136+
section: endpoints.section,
137+
model: endpoints.model
119138
};
120139
}
121140

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { extension, name } from "./file";
2+
import { extension, name, niceSize } from "./file";
33

44
describe.concurrent("$helper.file.extension()", () => {
55
it("returns the extension of a filename", () => {
@@ -16,3 +16,11 @@ describe.concurrent("$helper.file.name()", () => {
1616
expect(name("file")).toBe("");
1717
});
1818
});
19+
20+
describe.concurrent("$helper.file.niceSize()", () => {
21+
it("formats bytes", () => {
22+
expect(niceSize(0)).toBe("0B");
23+
expect(niceSize(1024)).toBe("1KB");
24+
expect(niceSize(1048576)).toBe("1MB");
25+
});
26+
});
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
/**
22
* Extracts the extension
33
*
4-
* @param {String} filename
5-
* @returns {String}
4+
* @example
5+
* extension("image.jpg") // => "jpg"
66
*/
7-
export const extension = (filename) => {
7+
export function extension(filename: string): string {
88
return filename.split(".").slice(-1).join("");
9-
};
9+
}
1010

1111
/**
1212
* Extracts the name without extension
1313
*
14-
* @param {String} filename
15-
* @returns {String}
14+
* @example
15+
* name("image.jpg") // => "image"
1616
*/
17-
export const name = (filename) => {
17+
export function name(filename: string): string {
1818
return filename.split(".").slice(0, -1).join(".");
19-
};
19+
}
2020

2121
/**
2222
* Creates a nice human-readable file size string with size unit
2323
*
24-
* @param {Number} size
25-
* @returns {String}
24+
* @example
25+
* niceSize(1024) // => "1KB"
26+
* niceSize(1048576) // => "1MB"
2627
*/
27-
export const niceSize = (size) => {
28+
export function niceSize(size: number): string {
2829
const formatter = Intl.NumberFormat("en", {
2930
notation: "compact",
3031
style: "unit",
@@ -33,7 +34,7 @@ export const niceSize = (size) => {
3334
});
3435

3536
return formatter.format(size);
36-
};
37+
}
3738

3839
export default {
3940
extension,

panel/src/helpers/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ 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";
1010
import isUploadEvent from "./isUploadEvent";
1111
import keyboard from "./keyboard";
1212
import link from "./link.js";
13-
import object from "./object.js";
13+
import object from "./object";
1414
import page from "./page.js";
1515
import ratio from "./ratio.js";
1616
import sort from "./sort.js";
17-
import string from "./string.js";
17+
import string from "./string";
1818
import throttle from "./throttle";
1919
import upload from "./upload.js";
2020
import url from "./url.js";

panel/src/helpers/link.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { length } from "./object.js";
2+
import { length } from "./object";
33
import link from "./link.js";
44

55
// mock $t() function

0 commit comments

Comments
 (0)