UNPKG

y-taro-ui

Version:

基于taro的表单解决方案 & 基础组件

188 lines (187 loc) 7.02 kB
import React, { CSSProperties } from 'react'; import { InputProps } from '../Input'; import { PickerProps } from '../Picker'; import { RadioProps } from '../Radio'; import { TextareaProps } from '../Textarea'; import { UploadProps } from '../Upload'; import { Language } from '../../language'; import { ButtonProps } from '../Button'; import { CheckboxGroupProps } from '../Checkbox'; export declare type ChildElement = React.ReactNode | React.ReactNode[] | FieldProps | FieldProps[] | (FieldProps | React.ReactNode)[][] | React.ReactElement; export { InputProps, PickerProps, RadioProps, TextareaProps, UploadProps, Language, CheckboxGroupProps, ButtonProps }; export declare type FieldType = 'INPUT' | 'PICKER' | 'RADIO' | 'TEXTAREA' | 'UPLOAD' | 'TEXT' | 'CHECKBOX' | undefined; export declare type Value = any; export declare type ObjectDependence = { name: string; value: RegExp; }; export interface FieldTypeDefine { INPUT: InputProps; PICKER: PickerProps; RADIO: RadioProps; TEXTAREA: TextareaProps; UPLOAD: UploadProps; TEXT: { value?: string; style?: React.CSSProperties; className?: string; onChange?: (value: string) => void; }; CHECKBOX: CheckboxGroupProps; } export declare type Keys = keyof FieldTypeDefine; export declare type KeysType<T extends Keys, P> = { type?: T; componentProps?: P; }; export declare type Types = { [K in Keys]: KeysType<K, FieldTypeDefine[K]>; }; export declare type ChildrenProps = Types[Keys]; export declare type shouldUpdate = ((prevValues: StoreValues, curValues: StoreValues) => boolean) | boolean; export declare type FieldProps = { shouldUpdate?: shouldUpdate; } & ChildrenProps & RenderProps; export interface Rule { required?: (() => boolean) | boolean; message?: string; validator?: (value: Value) => boolean; } export interface RenderProps { name?: string; label?: string; rules?: Rule[]; isShow?: ((props: Pick<FormInstance, 'getFieldsValue' | 'getFieldValue'>) => boolean) | boolean; dependences?: ((props: Pick<FormInstance, 'getFieldsValue' | 'getFieldValue'>) => boolean) | (string | ObjectDependence)[] | string | ObjectDependence; disabled?: boolean; initialValue?: Value; render?: (formInstance: FormInstance) => React.ReactNode; children?: React.ReactNode | ((formInstance: FormInstance) => React.ReactNode); layout?: Layout; } export declare type FormFieldChildrenProps = { formInstance: FormInstance; } & ChildrenProps & Pick<FormContextProps, 'locale' | 'language' | 'readonly' | 'textAlign' | 'layout'> & Pick<FieldProps, 'label' | 'name' | 'rules' | 'render' | 'type'>; export declare type Locale = 'en' | 'zh-cn'; export declare type TextAlign = 'right' | 'left'; export declare type Layout = 'horizontal' | 'vertical'; export declare type Config = { language: Language; } & Pick<FormProps, 'layout' | 'textAlign' | 'readonly' | 'locale' | 'disabled' | 'filterInitialValues'>; export interface YButtonProps extends ButtonProps { text?: React.ReactNode; } export declare type ValidateErrorMessage = { name?: string; label?: string; message: string; }; export declare type FieldsChange<T = any> = (props: { name: string; value: any; values: T; setFieldsValue: (fieldsValue: { value: any; name: string; }) => void; }) => void; export interface FormProps<T = any> { filterInitialValues?: boolean; readonly?: boolean; style?: CSSProperties; className?: string; layout?: Layout; onFinish?: Callbacks<T>['onFinish']; onFinishFailed?: Callbacks<T>['onFinishFailed']; onValuesChange?: Callbacks<T>['onValuesChange']; fields?: React.ReactNode | React.ReactNodeArray | FieldProps | FieldProps[] | (FieldProps | React.ReactNode)[][]; disabled?: boolean; validateFirst?: boolean; initialValues?: { [key: string]: any; }; submitter?: ((props: { onSubmit: () => void; onReset: () => void; }) => React.ReactNode) | null | { resetButton?: boolean | YButtonProps; cancelButton?: boolean | YButtonProps; submitButton?: boolean | YButtonProps; render?: (props: { onSubmit: () => void; onReset: () => void; }) => React.ReactNode; }; textAlign?: TextAlign; locale?: Locale; form?: any; children?: React.ReactNode | React.ReactNodeArray | FieldProps | FieldProps[] | (FieldProps | React.ReactNode)[][]; loading?: boolean; loadingIcon?: JSX.Element; } export declare type FieldRule = { rules?: Rule[]; label?: string | null; name: string; }; export interface FormContextProps { readonly?: boolean; disabled?: boolean; validateFirst?: boolean; layout?: Layout; textAlign?: TextAlign; locale?: Locale; language?: Language; formInstance?: FormInstance; } export interface Meta { touched: boolean; validating: boolean; errors: string[]; name: Name; } export interface FieldEntity { touched: boolean; name: Name; label?: string; rules?: Rule[]; initialValue?: StoreValue; shouldUpdate?: shouldUpdate; onStoreChange: (render: any) => void; } export interface FieldData extends FieldEntity { value: StoreValue; } export declare type StoreValue = any; export declare type StoreValues = Record<string, StoreValue>; export declare type Name = string; export interface InternalHooks { registerField: (entity: FieldEntity) => () => void; setInitialValues: (initialValues: StoreValues, init?: boolean) => void; setCallbacks: (callbacks: Callbacks) => void; getFields: (names?: Name[]) => FieldData[]; setConfigs: (config: Config) => void; resetStatus: () => void; } export interface Callbacks<T = StoreValue> { onValuesChange?: (changedValues: T, values: T) => void; onFinish?: (values: T) => void; onFinishFailed?: (errors: ValidateErrorMessage[]) => void; } export declare type RecursivePartial<T> = T extends object ? { [P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object ? RecursivePartial<T[P]> : T[P]; } : any; export interface FormInstance<T = StoreValues> { getFieldValue: (name: Name) => StoreValue; getFieldsValue(): T; getFieldsValue(names: Name[] | true, filterFunc?: (fieldEntity: FieldEntity) => boolean): any; setFieldsValue: (value: RecursivePartial<T>) => void; setInitialValues: (value: RecursivePartial<T>, init?: boolean) => void; useSubscribe: (subscribable: boolean) => void; getFields: () => FieldData[]; getField: (name: Name) => FieldData; getInternalHooks: (key: string) => InternalHooks | null; getFieldsError: () => ValidateErrorMessage[]; submit: () => void; reset: () => void; }