Skip to content

Commit

Permalink
feat(Field): useWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
guoyunhe committed Nov 30, 2023
1 parent d3ef446 commit 53f15f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/field/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useMemo } from 'react';
import { useState, useMemo, useEffect } from 'react';
import Field from '@alifd/field';

import { log } from '../util';
Expand All @@ -13,6 +13,14 @@ class NextField extends Field {
return this.getUseField({ useMemo, useState })(options);
}

static useWatch(field, name) {
if (!useState || !useEffect) {
log.warning('need react version > 16.8.0');
return;
}
return this.getUseWatch({ useEffect, useState })(field, name);
}

constructor(com, options = {}) {
const newOptions = Object.assign({}, options, {
afterValidateRerender: scrollToFirstError,
Expand Down
13 changes: 8 additions & 5 deletions types/field/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FieldOption } from '@alifd/field';
import innerField from '@alifd/field';

export * from '@alifd/field';
export default class Field extends innerField{
export default class Field extends innerField {
/**
*
* @param contextComp 传入调用class的this
Expand All @@ -23,14 +23,17 @@ export default class Field extends innerField{
* @param names
* @param callback
*/
validate(
names?: string[] | string,
callback?: (errors: object[], values: object) => void
): void;
validate(names?: string[] | string, callback?: (errors: object[], values: object) => void): void;

/**
* react hooks 风格使用 Field
* @param options
*/
static useField(options?: FieldOption): Field;

/**
* 将 Field 的 Value 转换为 React 的 State
* @param options
*/
static useField<T>(field: Field, name: string): T;
}

0 comments on commit 53f15f7

Please sign in to comment.