From bf30fd986af085a8e57e5a75b950e0beaa97f9b6 Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 10 Sep 2020 10:11:56 +0800 Subject: [PATCH 1/3] init list rule define --- src/List.tsx | 11 +++++++---- src/interface.ts | 12 +++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/List.tsx b/src/List.tsx index 743c012a..ff07bd61 100644 --- a/src/List.tsx +++ b/src/List.tsx @@ -1,6 +1,6 @@ import * as React from 'react'; import warning from 'rc-util/lib/warning'; -import { InternalNamePath, NamePath, StoreValue } from './interface'; +import { InternalNamePath, NamePath, StoreValue, ValidatorRule } from './interface'; import FieldContext from './FieldContext'; import Field from './Field'; import { move, getNamePath } from './utils/valueUtil'; @@ -19,10 +19,11 @@ interface ListOperations { interface ListProps { name: NamePath; + rules?: ValidatorRule[]; children?: (fields: ListField[], operations: ListOperations) => JSX.Element | React.ReactNode; } -const List: React.FunctionComponent = ({ name, children }) => { +const List: React.FunctionComponent = ({ name, children, rules }) => { const context = React.useContext(FieldContext); const keyRef = React.useRef({ keys: [], @@ -48,8 +49,10 @@ const List: React.FunctionComponent = ({ name, children }) => { return ( - - {({ value = [], onChange }) => { + + {({ value = [], onChange, meta }) => { + console.log('>>>>>>', meta); + const { getFieldValue } = context; const getNewValue = () => { const values = getFieldValue(prefixName || []) as StoreValue[]; diff --git a/src/interface.ts b/src/interface.ts index 9a86cd6f..6f2a426d 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -50,6 +50,11 @@ type Validator = ( export type RuleRender = (form: FormInstance) => RuleObject; +export interface ValidatorRule { + message?: string | ReactElement; + validator: Validator; +} + interface BaseRule { enum?: StoreValue[]; len?: number; @@ -60,19 +65,20 @@ interface BaseRule { required?: boolean; transform?: (value: StoreValue) => StoreValue; type?: RuleType; - validator?: Validator; whitespace?: boolean; /** Customize rule level `validateTrigger`. Must be subset of Field `validateTrigger` */ validateTrigger?: string | string[]; } -interface ArrayRule extends Omit { +type AggregationRule = BaseRule & Partial; + +interface ArrayRule extends Omit { type: 'array'; defaultField?: RuleObject; } -export type RuleObject = BaseRule | ArrayRule; +export type RuleObject = AggregationRule | ArrayRule; export type Rule = RuleObject | RuleRender; From 9f7813705c659e1e21396614e025d7d010f81110 Mon Sep 17 00:00:00 2001 From: zombiej Date: Thu, 10 Sep 2020 10:29:39 +0800 Subject: [PATCH 2/3] support error of List --- examples/list.tsx | 22 ++++++++++++++++++++-- src/List.tsx | 18 +++++++++++------- src/utils/validateUtil.ts | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/examples/list.tsx b/examples/list.tsx index 76a08a78..16e5aed9 100644 --- a/examples/list.tsx +++ b/examples/list.tsx @@ -25,8 +25,20 @@ const Demo = () => { > {() => JSON.stringify(form.getFieldsValue(), null, 2)} - - {(fields, { add, remove }) => { + { + if (value.length < 2) { + throw new Error(); + } + }, + }, + ]} + > + {(fields, { add, remove }, { errors }) => { console.log('Demo Fields:', fields); return (
@@ -49,6 +61,12 @@ const Demo = () => { ))} +
    + {errors.map(err => ( +
  • {err}
  • + ))} +
+