Skip to content

Commit

Permalink
feat: add generic type to setValue method
Browse files Browse the repository at this point in the history
  • Loading branch information
eternalsky committed Oct 29, 2024
1 parent 2f6e5e5 commit d6e026b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class Field {
* @param reRender - 设置完成后是否重新渲染,默认为 true
* @param triggerChange - 是否触发 watch change,默认为 true
*/
setValue(name: string, value: unknown, reRender = true, triggerChange = true) {
setValue<T = unknown>(name: string, value: T, reRender = true, triggerChange = true) {
const oldValue = this.getValue(name);
if (name in this.fieldsMeta) {
this.fieldsMeta[name].value = value;
Expand Down
32 changes: 21 additions & 11 deletions test/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import React, { useState, useMemo, Component, RefObject, createRef, forwardRef, useImperativeHandle } from 'react';
import React, {
useState,
useMemo,
Component,
type RefObject,
createRef,
forwardRef,
useImperativeHandle,
PureComponent,
isValidElement,
} from 'react';
import { Input, Form } from '@alifd/next';
import Field from '../src';

Expand Down Expand Up @@ -28,7 +38,7 @@ describe('field', () => {

describe('render', () => {
it('should support Form', () => {
class Demo extends React.Component {
class Demo extends Component {
field = new Field(this);

render() {
Expand Down Expand Up @@ -77,11 +87,11 @@ describe('field', () => {
});

it('should support React.createRef in Form', () => {
class Demo extends React.Component<{
class Demo extends Component<{
onClick: (ref: RefObject<any>) => void;
}> {
field = new Field(this);
ref = React.createRef<any>();
ref = createRef<any>();
render() {
return (
<Form field={this.field}>
Expand Down Expand Up @@ -109,7 +119,7 @@ describe('field', () => {
});

it('should support PureComponent', () => {
class Demo extends React.PureComponent {
class Demo extends PureComponent {
field = new Field(this, { forceUpdate: true });

render() {
Expand All @@ -127,7 +137,7 @@ describe('field', () => {
});

it('should support origin input/checkbox/radio', () => {
class Demo extends React.Component {
class Demo extends Component {
field = new Field(this);

render() {
Expand Down Expand Up @@ -341,7 +351,7 @@ describe('field', () => {
});

it('should support control through `setState`', () => {
class Demo extends React.Component {
class Demo extends Component {
state = {
show: true,
inputValue: 'start',
Expand Down Expand Up @@ -370,7 +380,7 @@ describe('field', () => {
});

it('should support control through `setState` when `parseName` is true', () => {
class Demo extends React.Component {
class Demo extends Component {
state = {
show: true,
inputValue: 'start',
Expand Down Expand Up @@ -434,14 +444,14 @@ describe('field', () => {
});
});

describe('behaviour', () => {
describe('behavior', () => {
it('getValue & getValues & setValue & setValues', () => {
const field = new Field({});
field.init('input', { initValue: 1 });
field.init('input2', { initValue: 2 });
field.init('input3.name', { initValue: 3 });

field.setValue('input', 2);
field.setValue<number>('input', 2);
assert(field.getValue('input') === 2);
assert(field.getValue('input3.name') === 3);
assert(Object.keys(field.getValues()).length === 3);
Expand Down Expand Up @@ -524,7 +534,7 @@ describe('field', () => {
assert(field.getError('input2')?.[0] === 'error 2');

field.setError('input', <span>hello</span>);
assert(React.isValidElement(field.getError('input')?.[0]) === true);
assert(isValidElement(field.getError('input')?.[0]) === true);
});
it('getState', () => {
const field = new Field({});
Expand Down

0 comments on commit d6e026b

Please sign in to comment.