@ bluprynt/ forms- 查看器
无头反应组件用于渲染和编辑由JSON驱动的动态形式. 与 @bluprynt/forms-core 处理能见度,验证,和段导航——你提供UI组件,取景器将所有东西一起接通.
密钥能力
- 只读渲染 —— 说
FormViewer使用自定义视图组件制作定义和文档中的窗体,并自动显示可见度和验证度. - 可编辑表单 —— 说
FormEditor使每个字段的可编辑窗体onChange处理器、数组和数组列项的操作(添加、去除、移动),并在每个变化上进行活的再验证. - 区域导航 —— 说
FormSections列出可见的顶层区域(包括用于未组合字段的合成根)并跟踪活性区域. - 验证显示 —— 说
FormFieldsValidation按字段分组实地一级错误;FormDocumentValidation使文档级别出错( schema 错配, 缺失)submittedAt(见a/c.5/49/70,附件). - 无头建筑 ——所有渲染都委托给您提供的组件地图(
ViewerComponentMap, (中文(简体) ),EditorComponentMap),因此软件包的UI观点为零.
安装
npm install @bluprynt/forms-viewer @bluprynt/forms-core ajv react react-dom
@bluprynt/forms-core, (中文(简体) ), react,以及 react-dom 属于对等依赖,必须安装在您的工程中.
快速启动
设置窗体提供者
这个 Form 组件汇编定义,计算能见度,运行验证,并通过上下文曝光一切.
import type { FC } from 'react'
import { Form } from '@bluprynt/forms-viewer'
import type { FormDefinition, FormDocument } from '@bluprynt/forms-viewer'
const definition: FormDefinition = { /* ... */ }
const document: FormDocument = { /* ... */ }
const App: FC = () => {
return (
<Form definition={definition} data={document}>
{/* FormViewer, FormEditor, FormSections, etc. */}
</Form>
)
}
Form 道具 :
| Prop | 类型 | 默认 | 说明 |
|---|---|---|---|
definition | FormDefinition | —— 说 | 形式方案 |
data | FormDocument | —— 说 | 窗体值文档 |
section | ROOT | number | —— 说 | 活动区域 ID( 使用) ROOT 未组合字段) |
showInlineValidation | boolean | true | 字段组件是否收到校验错误 |
children | ReactNode | —— 说 | 儿童成分 |
使用表格编辑器( 只读)
使用 FormViewer 当您只需要不编辑而显示已提交的窗体值时。 它使每个字段通过您的组件映射以只读模式,应用能见度规则并显示验证状态.
import type { FC } from 'react'
import { Form, FormViewer } from '@bluprynt/forms-viewer'
import type { ViewerComponentMap } from '@bluprynt/forms-viewer'
const components: ViewerComponentMap = {
string: ({ field, value }) => <p>{field.label}: {value}</p>,
number: ({ field, value }) => <p>{field.label}: {value}</p>,
boolean: ({ field, value }) => <p>{field.label}: {value ? 'Yes' : 'No'}</p>,
date: ({ field, value }) => <p>{field.label}: {value}</p>,
select: ({ field, value, options }) => (
<p>{field.label}: {options.find(o => o.value === value)?.label}</p>
),
array: ({ field, children }) => <div>{field.label}: {children}</div>,
file: ({ field, value }) => <p>{field.label}: {value?.name}</p>,
section: ({ section, children }) => (
<fieldset>
<legend>{section.title}</legend>
{children}
</fieldset>
),
}
const ReadOnlyForm: FC = () => {
return (
<Form definition={definition} data={document}>
<FormViewer components={components} />
</Form>
)
}
使用窗体编辑器( Ediable)
使用 FormEditor 当用户需要填写或编辑表格时。 它提供每个战地 onChange 处理器和对每次更改都发出回调 FormDocument JSON和当前验证结果.
import { useState, type FC } from 'react'
import { Form, FormEditor } from '@bluprynt/forms-viewer'
import type { EditorComponentMap, FormDocument, FormValidationResult } from '@bluprynt/forms-viewer'
const components: EditorComponentMap = {
string: ({ field, value, onChange }) => (
<input value={value ?? ''} onChange={e => onChange(e.target.value)} />
),
number: ({ field, value, onChange }) => (
<input type="number" value={value ?? ''} onChange={e => onChange(Number(e.target.value))} />
),
boolean: ({ field, value, onChange }) => (
<input type="checkbox" checked={value ?? false} onChange={e => onChange(e.target.checked)} />
),
date: ({ field, value, onChange }) => (
<input type="date" value={value ?? ''} onChange={e => onChange(e.target.value)} />
),
select: ({ field, value, options, onChange }) => (
<select value={value ?? ''} onChange={e => onChange(e.target.value)}>
{options.map(o => <option key={o.value} value={o.value}>{o.label}</option>)}
</select>
),
array: ({ field, children, onAddItem }) => (
<div>
{children}
<button onClick={onAddItem}>Add item</button>
</div>
),
file: ({ field, value, onChange }) => (
<input type="file" onChange={e => { /* handle file */ }} />
),
section: ({ section, children }) => (
<fieldset>
<legend>{section.title}</legend>
{children}
</fieldset>
),
}
const EditableForm: FC = () => {
const [data, setData] = useState<FormDocument>(initialDocument)
const handleChange = (doc: FormDocument, validation: FormValidationResult) => {
setData(doc)
// validation.valid, validation.fieldErrors, etc.
}
return (
<Form definition={definition} data={data}>
<FormEditor components={components} onChange={handleChange} />
</Form>
)
}
导航使用窗体部分
使用 FormSections 来将窗体区域变成标签、侧边栏菜单或任何其他导航 UI,并让用户在它们之间切换。 它列举了可见的顶层区段并跟踪活动区段.
import { useState, type FC } from 'react'
import { Form, FormViewer, FormSections, ROOT } from '@bluprynt/forms-viewer'
const FormWithSections: FC = () => {
const [activeSection, setActiveSection] = useState<typeof ROOT | number>(ROOT)
return (
<Form definition={definition} data={document} section={activeSection}>
<FormSections
container={({ children }) => <nav>{children}</nav>}
item={({ section, active, select }) => (
<button onClick={select} style={{ fontWeight: active ? 'bold' : 'normal' }}>
{section.title}
</button>
)}
defaultSectionTitle="General"
onSelect={setActiveSection}
/>
<FormViewer components={viewerComponents} />
</Form>
)
}
使用窗体校正和窗体文档校正
使用 FormDocumentValidation 以显示文档级别错误 — 计划错配、 无效版本、 缺失 submittedAt以及类似的结构问题。 使用 FormFieldsValidation 以显示字段级错误——与表单中定义的验证规则不符的值(需要、分/最大、图案等).
import type { FC } from 'react'
import { Form, FormFieldsValidation, FormDocumentValidation } from '@bluprynt/forms-viewer'
const ValidationSummary: FC = () => {
return (
<Form definition={definition} data={document}>
<FormDocumentValidation
container={({ children }) => <div className="doc-errors">{children}</div>}
error={({ code, message }) => <p>{message} ({code})</p>}
/>
<FormFieldsValidation
container={({ children }) => <div className="field-errors">{children}</div>}
field={({ field, errors, children }) => (
<div>
<strong>{field?.label}</strong>
{children}
</div>
)}
error={({ message, rule }) => <p>{message} [{rule}]</p>}
/>
</Form>
)
}
组件映射类型
查看器组件映射
只读渲染的组件映射 。 每个键将字段类型映射到反应组件.
| 密钥 | 道具类型 | 说明 |
|---|---|---|
string | StringViewProps | 文本字段显示 |
number | NumberViewProps | 数字字段显示 |
boolean | BooleanViewProps | 布尔字段显示 |
date | DateViewProps | 日期字段显示 |
select | SelectViewProps | 选择字段显示( 收到) options(中文(简体) ) |
array | ArrayViewProps | 矩阵字段显示( 收件人) children 用于项目, itemDef(中文(简体) ) |
array_obj? | ArrayObjViewProps | 数组显示. 接收 itemsDef,解决 kind (单位:千美元)'list' | 'table'和 children: ReactNode[][]索引 children[row][subField] 并与 itemsDef。 。 。 . |
file | FileViewProps | 文件字段显示 |
section | SectionViewProps | 区域包装(收件) children(中文(简体) ) |
multiselect? | MultiselectViewProps | 多选择显示 。 值为 (string | number)[]; 获得 options。 。 。 . |
static? | StaticViewProps | 仅显示文本块 。 接收 仅限 field 没有 value, (中文(简体) ), errors 或者说 suggestion。 。 。 . |
blockchain? | BlockchainViewProps | CAIP 地址显示 。 值为 a string,以逐字记录形式储存. |
sustainability? | SustainabilityViewProps | 可持续性参考显示。 值为 { address, ccri?, cmc? }。 。 。 . |
error? | ErrorProps | 可选内行出错渲染器 |
所有视图字段道具扩展 BaseViewFieldProps 规定 field: FieldContentItem 财务报告和审定财务报表 errors: FieldValidationError[]。 。 。 . StaticViewProps 是例外: 它只携带 field: StaticFieldContentItem,其 text 内容及其 label 是一个可选标题.
array_obj, (中文(简体) ), multiselect, (中文(简体) ), static, (中文(简体) ), blockchain 财务报告和审定财务报表 sustainability 为: 选项 键,所以现有的地图会不断编译。 未提供部件的字段只能扔出.
编辑器组件映射
可编辑渲染的组件映射 。 用突变回调来扩展取景道具.
| 密钥 | 道具类型 | 说明 |
|---|---|---|
string | StringEditProps | 文本输入为 onChange(value) |
number | NumberEditProps | 数字输入 onChange(value) |
boolean | BooleanEditProps | 布尔输入 onChange(value) |
date | DateEditProps | 输入日期与 onChange(value) |
select | SelectEditProps | 选择输入方式 onChange(value) 财务报告和审定财务报表 options |
array | ArrayEditProps | 矩阵编辑器 onAddItem, (中文(简体) ), onRemoveItem(index), (中文(简体) ), onMoveItem(from, to) |
array_obj? | ArrayObjEditProps | 与同为三行处理器的一行编辑器. onAddItem 附加一行; 每个单元格 onChange 到达每个子战地组件. |
file | FileEditProps | 文件输入方式 onChange(value) |
section | SectionEditProps | 区域包装(收件) children(中文(简体) ) |
multiselect? | MultiselectEditProps | 复选框组 . onChange((string | number)[] | undefined) - 发送整个阵列. |
static? | StaticEditProps | 仅显示文本块 。 页:1 StaticViewProps; 没有什么可编辑的. |
blockchain? | BlockchainEditProps | 文本输入为 onChange(value)。CAIP格式从未被引擎检查过. |
sustainability? | SustainabilityEditProps | 对象编辑器 onChange(value) - 发送整个 { address, ccri, cmc }释放 undefined 当每个部分都是空白的. |
error? | ErrorProps | 可选内行出错渲染器 |
所有编辑字段道具扩展 BaseEditFieldProps,则添加 onChange(value) 加可选
onAcceptSuggestion() 编号 : onRejectSuggestion() 处理器到基本视图道具. StaticEditProps 已经
不 静态块没有 onChange,因此同一部分为两种地图服务.
每个字段——只读可编辑——还收到字段撰写元数据
field (单位:千美元)description, (中文(简体) ), placeholder, (中文(简体) ), answer_guidelines, (中文(简体) ), reference_id)和可选
suggestion (单位:千美元){ value, confidence, source }当文档中包含一个字段。 正在接受
将建议值复制到字段值并保留建议;拒绝删除
建议并留下价值。 见 变化情况。 。 。 .
导出 API
| 导出 | 仁爱 | 说明 |
|---|---|---|
Form | 构成部分 | 背景提供者——编译定义,计算能见度和验证 |
useFormContext | 钩子 | 访问形式上下文(定义、数据、引擎、能见度、验证) |
FormViewer | 构成部分 | 只读窗体渲染器 |
FormEditor | 构成部分 | 带有更改回调的可编辑窗体渲染器 |
FormSections | 构成部分 | 区域导航列表 |
FormFieldsValidation | 构成部分 | 字段级验证错误显示 |
FormDocumentValidation | 构成部分 | 文件级验证错误显示 |
ROOT | 符号 | 识别根( 未组合字段) 部分 |
ViewerComponentMap | 类型 | 构成部分地图 FormViewer |
EditorComponentMap | 类型 | 构成部分地图 FormEditor |
BaseViewFieldProps | 类型 | 视图字段的基本道具 |
BaseEditFieldProps | 类型 | 编辑字段组件的基础道具 |
ErrorProps | 类型 | 内置错误组件的道具 |
FormSectionEntry | 类型 | 区域条目 FormSections |
FormSectionItemProps | 类型 | 区域项目渲染器的道具 |
FieldValidationFieldEntry | 类型 | 字段 + 错误组 FormFieldsValidation |
FormValuesEditor | 类 | 用于对照定义读取和书写形式值的流利编辑器 |
DocumentError | 类 | 表示文档级别验证失败出错 |
FormDefinition | 类型 | JSON 方案描述形式结构、字段、章节和规则 |
FormDocument | 类型 | 包含用户提交的形式值和可选建议的 JSON 文件 |
FieldSuggestion | 类型 | 机器拟议答案: { value, confidence, source } |
FormSuggestions | 类型 | 由字符串字段 ID 键入的建议分隔图 |
FormValidationResult | 类型 | 文件一级和外地一级错误的验证结果 |
FieldType | 类型 | 支助领域类型联盟(string, (中文(简体) ), number, (中文(简体) ), boolean, (中文(简体) ), date, (中文(简体) ), select, (中文(简体) ), multiselect, (中文(简体) ), array, (中文(简体) ), array_obj, (中文(简体) ), file, (中文(简体) ), static, (中文(简体) ), blockchain, (中文(简体) ), sustainability(中文(简体) ) |
FileValue | 类型 | 包含名称、 MIME 类型、 大小和 URL 的文件字段值 |
SustainabilityValue | 类型 | 可持续性领域价值: { address, ccri?, cmc? } |