Skip to content

Commit 96edb0e

Browse files
committed
fix: retype form inputs
1 parent 61738b9 commit 96edb0e

File tree

8 files changed

+106
-70
lines changed

8 files changed

+106
-70
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
"husky": {
7171
"hooks": {
7272
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
73-
"pre-commit": "yarn check --integrity && lint-staged"
73+
"pre-commit": "lint-staged"
7474
}
7575
},
7676
"commitlint": {

packages/common-helpers/src/form/input.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,29 @@ function getIcon(
4747
/**
4848
* get form input default
4949
*/
50-
function getDefault<V extends iFormValue = iFormValue, Vk extends V | V[] = V>(
50+
function getDefault<V extends iFormValue | iFormValue[]>(
5151
type?: eFormTypeBase | eFormTypeSimple | eFormTypeComplex,
52-
defaults?: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]]
53-
): Vk {
52+
defaults?: [
53+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
54+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
55+
...iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>[],
56+
]
57+
): V {
5458
switch (type) {
5559
case eFormType.LOCATION:
5660
// 3 values
57-
return Array(3).fill("") as Vk;
61+
return Array(3).fill("") as V;
5862
case eFormType.ID:
5963
case eFormType.PHONE:
6064
case eFormType.CELLPHONE:
6165
case eFormType.NEW_PASSWORD:
6266
// 2 values
63-
return Array(2).fill("") as Vk;
67+
return Array(2).fill("") as V;
6468
default:
6569
// 1 value
6670
if (!defaults) return Array(1).fill("")[0];
6771

68-
return Array(defaults.length).fill("") as Vk;
72+
return Array(defaults.length).fill("") as V;
6973
}
7074
}
7175

@@ -116,14 +120,24 @@ export abstract class FormInputDefault<
116120
}
117121
}
118122

119-
export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V | V[]>
120-
extends FormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>
121-
implements iFormInput<V, Vk>
123+
export class FormInput<
124+
V extends iFormValue | iFormValue[] = iFormValue | iFormValue[],
125+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex =
126+
| eFormTypeBase
127+
| eFormTypeSimple
128+
| eFormTypeComplex,
129+
>
130+
extends FormInputDefault<T>
131+
implements iFormInput<V, T>
122132
{
123133
// private
124134
private _options: iSelectOption[];
125-
private _values: Vk[];
126-
private _defaults?: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]];
135+
private _values: V[];
136+
private _defaults?: [
137+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
138+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
139+
...iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>[],
140+
];
127141
// public readonly
128142
public readonly name: string;
129143
public readonly title?: string;
@@ -137,9 +151,9 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
137151
* @param _onUpdatedValues hook that is called when the values are updated
138152
*/
139153
constructor(
140-
formInput: iFormInput<V, Vk>,
141-
private _onUpdatedValues?: (updatedValues: Vk[]) => Vk[] | undefined | void,
142-
rerender?: (fi?: Partial<iFormInput<V, Vk>>) => void
154+
formInput: iFormInput<V, T>,
155+
private _onUpdatedValues?: (updatedValues: V[]) => V[] | undefined | void,
156+
rerender?: (fi?: Partial<iFormInput<V, T>>) => void
143157
) {
144158
super(formInput, rerender);
145159

@@ -161,7 +175,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
161175
if (this.required && !this._values.length) {
162176
const values = this.options.map(({ value }) => value);
163177

164-
this._values = values.slice(0, Math.max(1, this.min)) as Vk[];
178+
this._values = values.slice(0, Math.max(1, this.min)) as V[];
165179
}
166180
} else if (this.type !== eFormType.FILE) {
167181
const length = Math.max(1, this.min); // negative values fallback
@@ -181,7 +195,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
181195
if (isChoiceType(this.type)) {
182196
// autoset single value if required
183197
if (this.required && !this._values.length) {
184-
const values = <Vk[]>this.options.map(({ value }) => value);
198+
const values = <V[]>this.options.map(({ value }) => value);
185199

186200
this._values = values.slice(0, Math.max(1, this.min));
187201
}
@@ -190,18 +204,18 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
190204
this.rerender();
191205
}
192206

193-
get values(): Vk[] {
207+
get values(): V[] {
194208
return this._values;
195209
}
196-
set values(updatedValues: Vk[] | undefined) {
210+
set values(updatedValues: V[] | undefined) {
197211
if (updatedValues === undefined) {
198212
// set defaults
199213
this._values = [];
200214

201215
if (isChoiceType(this.type)) {
202216
// autoset single value if required
203217
if (this.required && !this._values.length) {
204-
const values = <Vk[]>this.options.map(({ value }) => value);
218+
const values = <V[]>this.options.map(({ value }) => value);
205219

206220
this._values = values.slice(0, Math.max(1, this.min));
207221
}
@@ -218,12 +232,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
218232
}
219233
}
220234

221-
get defaults(): [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]] | undefined {
235+
get defaults() {
222236
return this._defaults;
223237
}
224-
set defaults(
225-
updatedDefaults: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]] | undefined
226-
) {
238+
set defaults(updatedDefaults) {
227239
this._defaults = updatedDefaults;
228240
this.rerender();
229241
}
@@ -233,7 +245,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
233245
*
234246
* @override
235247
*/
236-
public setRerender(rerender: (fi?: Partial<iFormInput<V>>) => void) {
248+
public setRerender(rerender: (fi?: Partial<iFormInput<V, T>>) => void) {
237249
super.setRerender(rerender);
238250

239251
return this;
@@ -242,7 +254,7 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
242254
/**
243255
* add new model to the models
244256
*/
245-
public addValue(newValue: Vk = getDefault(this.type, this.defaults)) {
257+
public addValue(newValue: V = getDefault(this.type, this.defaults)) {
246258
if (this.values.length < this.max) {
247259
this.values = [...this.values, newValue];
248260
}
@@ -264,10 +276,10 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
264276
* Clone this object
265277
*/
266278
public clone(
267-
overrides?: Omit<iFormInput<V, Vk>, "name"> & { name?: string },
268-
onUpdatedValues?: (updatedValues: Vk[]) => Vk[] | undefined | void
279+
overrides?: Omit<iFormInput<V, T>, "name"> & { name?: string },
280+
onUpdatedValues?: (updatedValues: V[]) => V[] | undefined | void
269281
) {
270-
const oldFormInput: iFormInput<V, Vk> = {
282+
const oldFormInput: iFormInput<V, T> = {
271283
...this,
272284
options: this.options,
273285
values: this.values,
@@ -281,12 +293,17 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
281293
);
282294
}
283295

296+
public isEqual(other: FormInput): boolean {
297+
return isEqual(FormInput.getObject(this), FormInput.getObject(other));
298+
}
299+
284300
/**
285301
* Get simple object
286302
*/
287-
public getObject<Vi extends iFormValue = iFormValue, Vik extends Vi | Vi[] = Vi>(
288-
input: iFormInput<Vi, Vik>
289-
): iFormInput<Vi, Vik> {
303+
static getObject<
304+
Vi extends iFormValue | iFormValue[] = iFormValue | iFormValue[],
305+
Ti extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple,
306+
>(input: iFormInput<Vi, Ti>): iFormInput<Vi, Ti> {
290307
return {
291308
required: input.required,
292309
type: input.type,
@@ -303,8 +320,4 @@ export class FormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V
303320
multiple: input.multiple,
304321
};
305322
}
306-
307-
public isEqual(other: FormInput): boolean {
308-
return isEqual(this.getObject(this), this.getObject(other));
309-
}
310323
}

packages/common-helpers/src/form/utils.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ import {
1919
* @param value any value
2020
* @returns {boolean}
2121
*/
22-
export function notEmptyValue<V extends iFormValue = iFormValue>(
23-
value: V | V[],
24-
defaults?: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]]
22+
export function notEmptyValue<V extends iFormValue | iFormValue[] = iFormValue | iFormValue[]>(
23+
value: V,
24+
defaults?: [
25+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
26+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
27+
...iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>[],
28+
]
2529
): boolean {
2630
if (Array.isArray(value)) {
2731
// if not required bypass
@@ -36,10 +40,10 @@ export function notEmptyValue<V extends iFormValue = iFormValue>(
3640
/**
3741
* check if single FormInput value from values is valid
3842
*/
39-
export function isValidValue<V extends iFormValue = iFormValue>(
40-
value: V | V[],
41-
input: tFormInput
42-
): boolean {
43+
export function isValidValue<
44+
V extends iFormValue | iFormValue[] = iFormValue | iFormValue[],
45+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple,
46+
>(value: V, input: tFormInput<V, T>): boolean {
4347
// empty values are falsy
4448
if (!notEmptyValue(value, input.defaults)) return false;
4549

packages/common-types/src/form/class.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
import type { iSelectOption } from "../values.js";
1515

1616
export declare abstract class tFormInputDefault<
17-
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple.TEXT,
17+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple,
1818
> implements iFormInputDefault<T>
1919
{
2020
// public
@@ -32,9 +32,15 @@ export declare abstract class tFormInputDefault<
3232
public setRerender(rerender: (fi?: Partial<iFormInputDefault<T>>) => void): this;
3333
}
3434

35-
export declare class tFormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V | V[]>
36-
extends tFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>
37-
implements iFormInput<V, Vk>
35+
export declare class tFormInput<
36+
V extends iFormValue | iFormValue[] = iFormValue | iFormValue[],
37+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex =
38+
| eFormTypeBase
39+
| eFormTypeSimple
40+
| eFormTypeComplex,
41+
>
42+
extends tFormInputDefault<T>
43+
implements iFormInput<V, T>
3844
{
3945
// public readonly
4046
public readonly name: string;
@@ -44,38 +50,45 @@ export declare class tFormInput<V extends iFormValue = iFormValue, Vk extends V
4450
public readonly max: number;
4551
// public
4652
public options: iSelectOption[];
47-
public values: Vk[];
48-
public defaults?: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]];
53+
public values: V[];
54+
public defaults?: [
55+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
56+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
57+
...iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>[],
58+
];
4959
/**
5060
* set rerender function
5161
*
5262
* @override
5363
*/
54-
public setRerender(rerender: (fi?: Partial<iFormInput<V>>) => void): this;
64+
public setRerender(rerender: (fi?: Partial<iFormInput<V, T>>) => void): this;
5565
/**
5666
* add new model to the models
5767
*/
58-
public addValue(newValue?: Vk): this;
68+
public addValue(newValue?: V): this;
5969
/**
6070
* remove the given value from the values
6171
*/
6272
public removeValue(index: number): this;
63-
6473
/**
6574
* Clone this object
6675
*/
6776
public clone(
68-
overrides?: Omit<iFormInput<V, Vk>, "name"> & { name?: string },
69-
onUpdatedValues?: (updatedValues: Vk[]) => Vk[] | undefined | void
70-
): tFormInput<V, Vk>;
77+
overrides?: Omit<iFormInput<V, T>, "name"> & { name?: string },
78+
onUpdatedValues?: (updatedValues: V[]) => V[] | undefined | void
79+
): tFormInput<V, T>;
7180
/**
72-
* Get simple object
81+
* Check if this object is equal to another
7382
*/
74-
public getObject<Vi extends iFormValue = iFormValue, Vik extends Vi | Vi[] = Vi>(
75-
input: iFormInput<Vi, Vik>
76-
): iFormInput<Vi, Vik>;
77-
7883
public isEqual(other: tFormInput): boolean;
84+
85+
/**
86+
* Get simple object
87+
*/
88+
static getObject<
89+
Vi extends iFormValue | iFormValue[],
90+
Ti extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple,
91+
>(input: iFormInput<Vi, Ti>): iFormInput<Vi, Ti>;
7992
}
8093

8194
export interface iForm {

packages/common-types/src/form/types.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export type iFormValue = string | number | boolean | File | Date;
101101
* Simple input
102102
*/
103103
export interface iFormInputDefault<
104-
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple.TEXT,
104+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex = eFormTypeSimple,
105105
> {
106106
type?: T;
107107
required?: boolean;
@@ -116,17 +116,23 @@ export interface iFormInputDefault<
116116
/**
117117
* Complex input, sub input support
118118
*/
119-
export interface iFormInput<V extends iFormValue = iFormValue, Vk extends V | V[] = V | V[]>
120-
extends iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex> {
119+
export interface iFormInput<
120+
V extends iFormValue | iFormValue[],
121+
T extends eFormTypeBase | eFormTypeSimple | eFormTypeComplex,
122+
> extends iFormInputDefault<T> {
121123
name: string;
122124
options?: (string | number | iFormOption)[];
123125
/**
124126
* An array of values to simplify validation
125127
*
126128
* @old value
127129
*/
128-
values?: Vk[];
129-
defaults?: [iFormInputDefault, iFormInputDefault, ...iFormInputDefault[]];
130+
values?: V[];
131+
defaults?: [
132+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
133+
iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>,
134+
...iFormInputDefault<eFormTypeBase | eFormTypeSimple | eFormTypeComplex>[],
135+
];
130136
/**
131137
* Visible over the field, should describe it
132138
*/

packages/components-vue/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@
8080
"storybook": "^8.1.1",
8181
"ts-md5": "^1.3.1",
8282
"validator": "^13.11.0",
83-
"vue": "^3.4.0",
83+
"vue": "^3.5.13",
8484
"vue-router": "^4.2.5"
8585
},
8686
"peerDependencies": {
87-
"vue": "^3.4.0",
87+
"vue": "^3.5.13",
8888
"vue-router": "^4.2.5"
8989
},
9090
"peerDependenciesMeta": {

packages/components-vue/src/composables/input.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
2-
iFormInput,
32
tFormAutocomplete,
3+
tFormInput,
44
tLocaleForm,
55
tTextInputType,
66
} from "@open-xamu-co/ui-common-types";
@@ -9,7 +9,7 @@ import { eFormType } from "@open-xamu-co/ui-common-enums";
99

1010
import { useHelpers } from "../composables/utils";
1111

12-
export default function useInput({ input }: { input: iFormInput }) {
12+
export default function useInput({ input }: { input: tFormInput }) {
1313
const { t, tet } = useHelpers(useI18n);
1414

1515
/**

0 commit comments

Comments
 (0)