
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
cw-form-render-mobile
Advanced tools
cw-form-render-mobile
基于 form-render-mobile 二次开发的移动端动态表单库。
FormRender Mobile 是为移动端设置的开箱即用的表单解决方案,通过 JsonSchema 协议动态渲染表单。基于 FormRender2.0 和 Ant Design Mobile 实现。API 与 FormRender2.0 基本一致,如果你熟悉 FromRender2.0 那么你就已经会使用 FormRender Mobile 了。
FormRender Mobile 依赖 Ant Design Mobile,单独使用不要忘记同时安装 antd-mobile
npm i cw-form-render-mobile --save
import React from 'react';
import FormRender, { useForm } from 'cw-form-render-mobile';
const schema = {
type: 'object',
displayType: 'row',
properties: {
input: {
title: '输入框',
type: 'string',
widget: 'input',
},
radio: {
title: '单选',
type: 'string',
widget: 'radio',
props: {
options: [
{ label: '早', value: 'a' },
{ label: '中', value: 'b' },
{ label: '晚', value: 'c' },
],
},
},
},
};
export default () => {
const form = useForm();
const onFinish = formData => {
console.log('formData:', formData);
};
return <FormRender form={form} schema={schema} onFinish={onFinish} />;
};
通过 useForm() 创建的 form 实例提供以下方法:
getValues(nameList?, filterFunc?) - 获取表单数据setValues(values) - 设置表单数据getSchema() - 获取表单 schemasetSchema(schema, cover?) - 设置表单 schemagetValueByPath(path) - 根据路径获取表单值setValueByPath(path, value) - 根据路径设置表单值getSchemaByPath(path) - 根据路径获取 schemasetSchemaByPath(path, schema) - 根据路径设置 schemagetValueByName(name) - 根据字段名获取表单值setValueByName(name, value) - 根据字段名设置表单值getSchemaByName(name) - 根据字段名获取 schemasetSchemaByName(name, schema) - 根据字段名设置 schemagetFlatValues(nameList?, filterFunc?, notFilterUndefined?) - 获取扁平化的表单数据,自动移除 void 类型容器(如 collapse、group 等)的数据层级setFlatValues(values) - 设置扁平化的表单数据,显式将扁平数据转换为嵌套结构(不依赖 flattenData 配置)getFlattenSchema(path?) - 获取扁平化的 schema注意: 当表单配置了
flattenData={true}时,setValues会自动支持扁平数据,无需调用setFlatValues
validateFields(pathList?) - 触发表单验证getFieldError(path) - 获取字段错误信息getFieldsError(pathList) - 获取一组字段的错误信息setErrorFields(errors) - 设置字段错误信息removeErrorField(path) - 清除字段错误信息getHiddenValues() - 获取隐藏字段的数据resetFields(pathList?) - 重置表单字段isFieldTouched(path) - 检查字段是否被操作过isFieldsTouched(pathList?, allTouched?) - 检查一组字段是否被操作过const form = useForm();
// 获取表单数据
const values = form.getValues();
// 设置表单数据
form.setValues({ input: 'hello', radio: 'a' });
// 根据字段名获取值
const inputValue = form.getValueByName('input');
// 根据字段名设置值
form.setValueByName('input', 'new value');
// 根据字段名获取 schema
const inputSchema = form.getSchemaByName('input');
// 根据字段名动态修改 schema
form.setSchemaByName('input', {
title: '新标题',
disabled: true
});
// 方式一:配置 flattenData={true}(推荐)
<FormRender
form={form}
schema={schema}
flattenData={true} // 启用扁平化
onFinish={onFinish}
/>
// 启用 flattenData 后,setValues/getValues 自动支持扁平数据
const flatValues = form.getValues();
// 返回:{ display_name: 'xxx', model_id: {...}, vendor_id: {...} }
form.setValues({
display_name: '资产名称',
model_id: { sys_id: '123', name: '型号A' },
vendor_id: { sys_id: '456', name: '供应商B' }
});
// 自动转换为嵌套结构并设置到表单
// 典型使用场景:从后端获取扁平数据后直接回显
fetch('/api/asset/detail')
.then(res => res.json())
.then(data => {
// data 是扁平数据结构,直接使用 setValues
form.setValues(data);
});
// ========================================
// 方式二:不配置 flattenData,手动使用扁平化方法
const flatValues = form.getFlatValues(); // 获取扁平数据
// 返回:{ display_name: 'xxx', model_id: {...}, vendor_id: {...} }
const values = form.getValues(); // 获取嵌套数据
// 返回:{
// SoftwareLicense: { display_name: 'xxx', model_id: {...} },
// Financial: { vendor_id: {...} }
// }
form.setFlatValues({ // 显式设置扁平数据
display_name: '资产名称',
model_id: { sys_id: '123', name: '型号A' },
vendor_id: { sys_id: '456', name: '供应商B' }
});
// 根据条件动态修改字段配置
const handleChange = (value) => {
if (value === 'a') {
form.setSchemaByName('input', {
required: true,
props: { placeholder: '请输入内容' }
});
}
};
FAQs
通过 JSON Schema 生成标准 Form,常用于自定义搭建配置界面生成
We found that cw-form-render-mobile demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.