feat: add validate to Form facade, deprecate FieldArray.delete but use remove instead (#94)

* feat: expose validate api in form facade

* refactor: deprecate FieldArray.delete but expose remove instead

* refactor: only change api on facade
This commit is contained in:
YuanHeDx 2025-03-26 17:43:10 +08:00 committed by GitHub
parent 62e53350f1
commit c75f92d379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 19 additions and 2 deletions

View File

@ -85,7 +85,7 @@ export class FieldArrayModel<TValue = FieldValue> extends FieldModel<Array<TValu
} }
/** /**
* Field模型 * Delete the element in given index and delete the corresponding FieldModel as well
* @param index * @param index
*/ */
delete(index: number) { delete(index: number) {

View File

@ -19,7 +19,12 @@ export function toFieldArray<TValue>(model: FieldArrayModel<TValue>): FieldArray
map: <T = any>(cb: (f: Field, index: number) => T) => map: <T = any>(cb: (f: Field, index: number) => T) =>
model.map<T>((f, index) => cb(toField(f), index)), model.map<T>((f, index) => cb(toField(f), index)),
append: (value) => toField<TValue>(model.append(value)), append: (value) => toField<TValue>(model.append(value)),
/**
* @deprecated: use remove instead
* @param index
*/
delete: (index: number) => model.delete(index), delete: (index: number) => model.delete(index),
remove: (index: number) => model.delete(index),
swap: (from: number, to: number) => model.swap(from, to), swap: (from: number, to: number) => model.swap(from, to),
move: (from: number, to: number) => model.move(from, to), move: (from: number, to: number) => model.move(from, to),
} as FieldArray<TValue>; } as FieldArray<TValue>;

View File

@ -14,6 +14,7 @@ export function toForm<TValue>(model: FormModel): Form<TValue> {
state: toFormState(model.state), state: toFormState(model.state),
getValueIn: <TValue = FieldValue>(name: FieldName) => model.getValueIn(name), getValueIn: <TValue = FieldValue>(name: FieldName) => model.getValueIn(name),
setValueIn: <TValue>(name: FieldName, value: TValue) => model.setValueIn(name, value), setValueIn: <TValue>(name: FieldName, value: TValue) => model.setValueIn(name, value),
validate: model.validate.bind(model),
}; };
Object.defineProperty(res, '_formModel', { Object.defineProperty(res, '_formModel', {

View File

@ -79,10 +79,16 @@ export interface FieldArray<TFieldValue extends FieldValue = FieldValue>
*/ */
append: (value: TFieldValue) => Field<TFieldValue>; append: (value: TFieldValue) => Field<TFieldValue>;
/** /**
* @deprecated use remove instead
* Delete the value and the related field at certain index of the array. * Delete the value and the related field at certain index of the array.
* @param index the index of the element to delete * @param index the index of the element to delete
*/ */
delete: (index: number) => void; delete: (index: number) => void;
/**
* Delete the value and the related field at certain index of the array.
* @param index the index of the element to delete
*/
remove: (index: number) => void;
/** /**
* Move an array element from one position to another. * Move an array element from one position to another.
* @param from from position * @param from from position

View File

@ -1,5 +1,5 @@
import { FormModel } from '../core/form-model'; import { FormModel } from '../core/form-model';
import { Errors, Validate, ValidateTrigger, Warnings } from './validate'; import { Errors, FormValidateReturn, Validate, ValidateTrigger, Warnings } from './validate';
import { Field, FieldArray, FieldName, FieldValue } from './field'; import { Field, FieldArray, FieldName, FieldValue } from './field';
import { Context } from './common'; import { Context } from './common';
@ -81,6 +81,11 @@ export interface Form<TValues = any> {
* @param name path * @param name path
*/ */
setValueIn<TValue>(name: FieldName, value: TValue): void; setValueIn<TValue>(name: FieldName, value: TValue): void;
/**
* Trigger validate for the whole form.
*/
validate: () => Promise<FormValidateReturn>;
} }
export interface FormRenderProps<TValues> { export interface FormRenderProps<TValues> {