import { ReactNode, ChangeEvent, FocusEvent, CSSProperties, PropsWithChildren } from 'react'; import { StringIndexedObject } from '@befe/brick-utils'; import { ReactFinalForm, FinalForm, FieldValue, ReactFinalFormFieldMetaState } from './base'; import { FormContextValue } from './form'; /** * 字段状态, * 详情可参考 [FinalForm FieldState](https://final-form.org/docs/final-form/types/FieldState) * @public */ export interface FieldState extends ReactFinalFormFieldMetaState { /** * Whether or not the field currently has focus. */ active?: boolean; /** * A place for arbitrary values to be placed by mutators. */ data?: StringIndexedObject; /** * true when the value of the field is not equal to the initial value, false if the values are equal. * * (using the [isEqual](https://final-form.org/docs/final-form/types/FieldConfig#isequal) comparator provided at field registration) */ dirty?: boolean; /** * true when the value of the field is not equal to the value last submitted, false if the values are equal. * * (using the [isEqual](https://final-form.org/docs/final-form/types/FieldConfig#isequal) comparator provided at field registration) */ dirtySinceLastSubmit?: boolean; /** * The current validation error for this field. */ error?: any; /** * The initial value of the field. undefined if it was never initialized. */ initial?: Val; /** * true if the field has a validation error or a submission error. false otherwise. */ invalid?: boolean; /** * The length of the array if the value is an array. undefined otherwise. */ length?: number; /** * true if this field's value has ever been changed. false otherwise. * * Once true, it will remain true for the lifetime of the field, or until the form or field state is reset. */ modified?: boolean; /** * true if this field's value has ever been changed since the last submission. false otherwise. * * Once true, it will remain true until the next submit action, or until the form or field state is reset. */ modifiedSinceLastSubmit?: boolean; /** * true if the current value is === to the initial value, false if the values are !==. */ pristine?: boolean; /** * The submission error for this field. */ submitError?: any; /** * true if a form submission has been tried and failed. * false otherwise. */ submitFailed?: boolean; /** * true if the form has been successfully submitted. * false otherwise. */ submitSucceeded?: boolean; /** * true if the form is currently being submitted asynchronously. * false otherwise. */ submitting?: boolean; /** * true if this field has ever gained and lost focus. * false otherwise. * * Useful for knowing when to display error messages. */ touched?: boolean; /** * true if this field has no validation or submission errors. * false otherwise. */ valid?: boolean; /** * true if this field is currently waiting on its asynchronous field-level validation function to resolve. * false otherwise. */ validating?: boolean; /** * true if this field has ever gained focus. * false otherwise. */ visited?: boolean; } /** * 字段给其控件提供的 `state data` and `event handlers` api 对象, * 以便于可以通过解构的方式直接给到控件,如 `` * * Keep in mind that the values in meta are dependent on you having subscribed to them * with the [subscription](https://final-form.org/docs/react-final-form/types/FieldProps#subscription) prop * @public */ export interface FieldControlProps { /** * 字段值 */ value: Val; /** * checkbox 类型字段值,用于 , , 等 */ checked?: boolean; /** * 字段名 */ name: string; /** * change 回调,用于操作 field 进行值变化 * 即 update field state value */ onChange: (event: ChangeEvent | any) => void; /** * focus 回调,用于操作 field 进行 blur, * 即 update field state as focused (active) */ onFocus: (event?: FocusEvent) => void; /** * blur 回调,用于操作 field 进行 blur, * 即 update field state as blurred (inactive) */ onBlur: (event?: FocusEvent) => void; /** * 尺寸,值为 Form props.size */ size?: FormContextValue['size']; } /** * Field 提供给其内容 render function 的 api,主要包括 * - field 状态 * - 一些操作 field 状态 api * - 一个集成上述两项方便传递给常用控件的对象 * * @public */ export interface FieldRenderProps { /** * 字段的 value,同 `control.value` */ value: FieldControlProps['value']; /** * 操作字段进行 change value,同 `control.onChange` */ change: FieldControlProps['onChange']; /** * 操作字段进行 focus,同 `control.onFocus` */ focus: FieldControlProps['onFocus']; /** * 操作字段进行 blur,同 `control.onFocus` */ blur: FieldControlProps['onBlur']; /** * 字段给其中控件提供的 api 对象, * 详见 FieldControlProps */ control: FieldControlProps; /** * 字段的状态 */ fieldState: FieldState; } export interface FieldProps { /** * 自定义 class */ className?: string; /** * 字段标识 */ name: string; /** * 标签宽度 * * 设置 0 将隐藏 label */ labelWidth?: CSSProperties['width']; /** * 控件宽度 */ controlWidth?: CSSProperties['width']; /** * 字段标签 */ label?: ReactNode; /** * 是否必填 * (暂定)只影响标签前的必填标记,而不负责校验 */ required?: boolean; /** * 标签中的问号 tips 内容 */ help?: ReactNode; /** * 静态提示信息 * false 为禁用提示信息,用于 control 需要自行提示的情况 */ hint?: ReactNode | boolean; /** * 字段初始值,用以比较字段值是否已经更新 */ initialValue?: Val; /** * 校验函数 * - invalid: returns error message * - valid: returns undefined * * 注意,由于需要支持行内箭头函数,仅改变此 prop, 不会引起 field 的 rerender, * 如果需要 field rerender 以注册新的 校验函数,需要同时改动其他 props,例如最简单的 key * * @type ( * value: FieldValue, * formValues: FormValues, * filedState: FieldState * ) => FormValidationResult | Promise */ validate?: FinalForm.FieldValidator; /** * 触发改字段校验,其他字段名 * 如果为 `undefined` 则所有其他字段均会触发校验 * 注意,由于需要支持行内 `[]` 的书写方式,仅改变此 prop, 不会引起 field 的 rerender,如果需要 field rerender 以注册新的 校验函数,需要同时改动其他 props,例如最简单的 key */ validateFields?: string[]; /** * FieldState 触发 field render 的订阅字段,如不提供,则 FieldState 的所有字段变化都会触发 render,一般而言只在需要进行性能优化时才配置 */ subscription?: StringIndexedObject; /** * 接收 api FieldRenderProps 作为参数的 render 函数 */ children?: ((fieldRenderProps: FieldRenderProps) => (ReactNode)) | ReactNode; /** * (暂定)是否为隐藏字段,设为 true 的 Field 不会渲染 node * * 以便于有时需要防止如 id 之类的静态字段,但注意不要滥用 */ hidden?: boolean; /** * filed 的(值)类型 * * 用以指示 react-final-form 按何种类型控件来管理此 field 的值 * * - 设为 checkbox 可使 fieldRenderProps.controlled 中包括 checked 值 * - 不设置会尝试自动根据 children 结果进行判断,但注意此判断有一定不足: * - 只认识 brick 组件 * - 只判断第一个节点,即 `` 可判断,`
` 不可判断 * - 只根据初次渲染结果进行判断,即如果首次渲染结果为 ``, 这那么后续变成 `` 是没有 control.checked 的 */ type?: 'checkbox'; /** * 用以判断两值是否相等的函数 * * 默认相当于 (old, new) => old === new * * 影响 fieldState.dirty 的判断 */ isEqual?: ReactFinalForm.UseFieldConfig['isEqual']; } export declare const Field: { (props: PropsWithChildren>): import("react/jsx-runtime").JSX.Element | null; displayName: string; defaultProps: { className: string; required: boolean; }; };