Skip to content

Commit

Permalink
feat(admin/components/form): 添加 ObjectAccessor#setObject
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Oct 28, 2024
1 parent ecf6766 commit 2f8c1d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
36 changes: 28 additions & 8 deletions admin/src/components/form/access.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//
// SPDX-License-Identifier: MIT

import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';
import { Accessor, FieldAccessor, ObjectAccessor } from './access';

test('field access', () => {
const f = FieldAccessor('name', 5);
t(f);
});

test('object access', () => {
describe('object access', () => {
interface Object {
f1: number;
f2: string;
Expand All @@ -22,13 +22,33 @@ test('object access', () => {
expect(f.object()).toEqual({ 'f1': 7, 'f2': 'f2' });
expect(f.isPreset()).toEqual<boolean>(false);

// 验证
const v = (_: Object) => { return new Map<keyof Object, string>([['f1', 'err']]); };
expect(f.object(v)).toBeUndefined();
expect(f.accessor('f1').getError(), 'err');
test('validation', () => {
const v = (_: Object) => { return new Map<keyof Object, string>([['f1', 'err']]); };
expect(f.object(v)).toBeUndefined();
expect(f.accessor('f1').getError(), 'err');

f.reset();
expect(f.isPreset()).toEqual<boolean>(true);
const f1 = f.accessor('f1');
expect(f1).toEqual(f.accessor('f1'));

f.reset();
expect(f.isPreset()).toBeTruthy();
expect(f1).toEqual(f.accessor('f1')); // 同一个 Accessor 接口只有一个对象
});


test('setPreset', () => {
f.setPreset({ 'f1': 1, 'f2': '2' });
expect(f.isPreset()).toBeFalsy();
f.reset();
expect(f.isPreset()).toBeTruthy();
expect(f.accessor('f1').getValue()).toEqual(1);
});

test('setObject', () => {
f.setObject({ 'f1': 11, 'f2': '22' });
expect(f.accessor('f1').getValue()).toEqual(11);
expect(f.accessor('f2').getValue()).toEqual('22');
});
});

function t(a: Accessor<number>) {
Expand Down
26 changes: 24 additions & 2 deletions admin/src/components/form/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Err<T extends object> = {

/**
* 每个表单元素通过调用此接口实现对表单数据的存取和一些基本信息的控制
*
* @template T 关联的值类型
*/
export interface Accessor<T> {
/**
Expand Down Expand Up @@ -131,6 +133,8 @@ export class ObjectAccessor<T extends object> {
readonly #errGetter: Store<Err<T>>;
readonly #errSetter: SetStoreFunction<Err<T>>;

#accessor: Map<keyof T, Accessor<T[keyof T]>>;

/**
* 构造函数
*
Expand All @@ -142,6 +146,8 @@ export class ObjectAccessor<T extends object> {

[this.#valGetter, this.#valSetter] = createStore<T>({...preset});
[this.#errGetter, this.#errSetter] = createStore<Err<T>>({});

this.#accessor = new Map<keyof T, Accessor<T[keyof T]>>();
}

/**
Expand Down Expand Up @@ -183,10 +189,13 @@ export class ObjectAccessor<T extends object> {
* @param hasError 是否需要展示错误信息,对应 {@link Accessor#hasError} 方法;
*/
accessor<FT = T[keyof T]>(name: keyof T, hasError?: boolean): Accessor<FT> {
let a: Accessor<FT>|undefined = this.#accessor.get(name)as Accessor<FT>;
if (a) { return a as Accessor<FT>; }

const self = this;
const changes: Array<ChangeFunc<FT>> = [];

return {
a = {
name(): string { return name as string; },

hasError(): boolean { return hasError ?? false; },
Expand Down Expand Up @@ -214,6 +223,8 @@ export class ObjectAccessor<T extends object> {
this.setValue(self.#preset[name] as FT);
}
};
this.#accessor.set(name, a as Accessor<T[keyof T]>);
return a;
}

/**
Expand Down Expand Up @@ -241,6 +252,15 @@ export class ObjectAccessor<T extends object> {
return v;
}

/**
* 修改整个对象的值
*/
setObject(obj: T) {
Object.entries(obj).forEach(([k, v]) => {
this.accessor(k as keyof T).setValue(v);
});
}

/**
* 根据 {@link Problem} 设置当前对象的错误信息
*/
Expand Down Expand Up @@ -319,6 +339,8 @@ export class FormAccessor<T extends object, R = never, P = never> {

setPreset(v: T) { return this.#object.setPreset(v); }

setObject(v: T) { return this.#object.setObject(v); }

/**
* 当前内容是否都是默认值
*/
Expand All @@ -344,7 +366,7 @@ export class FormAccessor<T extends object, R = never, P = never> {
const obj = this.#object.object(this.#validation);
if (!obj) { return false; }

const ret = await this.#request(obj);
const ret = await this.#request(unwrap(obj));
if (ret.ok) {
if (this.#success) {
this.#success(ret);
Expand Down

0 comments on commit 2f8c1d1

Please sign in to comment.