实际上是数据驱动式编程更向前走一步,咱们知道如今MVVM框架,像React、vue、Angular这些给前端带来了很大的进步,更优雅的工程化体系结构,更健壮的代码体系结构。一样给开发者带来了数据驱动式开发的体验,可是业务代码仍是常常会出现业务逻辑与UI表达形式的穿插混合的状况,很难统一。javascript
github: https://github.com/chalecao/c...
感谢star!!!css
面向数据的编程其实核心思想仍是作样式行为分离,中间的解耦利器即是数据。html
样式 mix 行为 = 样式 + 数据 + 行为
因而咱们抽象出来的config-component组件,做为驱动样式行为分离的通用组件,驱动面向数据编程。前端
例如实现下面的表单:vue
基于antd组件库,比较下不一样的代码结构:java
左边的图是基于ANTD正常的写法,咱们要写Form,要写检验规则,要写子组件等等,关键是这些逻辑是糅合在一块儿的。右边是基于config-component提供的ConfigForm组件作了封装,只须要提供JSON schema配置数据便可驱动你的页面,分离数据校验和UI逻辑,UI逻辑基于UIconfig,数据校验基于schema作校验。node
✅ change your code style to face data, use JSON schema config to driven you page.数据驱动式开发更进一步转向面向数据编程,结合React Hooks开发更高效,专一于数据和业务逻辑.基于JSON配置数据驱动你的页面或者表单。✅ add schema mechanism to ensure you data correct and force you to care you data and handle the abnormal situation.开发中引入schema机制验证核心数据的正确性,不管是表单仍是前台组件都可适用。react
✅ support get data async, verify data when it changes.基于schema动态校验数据,支持异步数据更新,响应式作校验。git
安装:github
npm install config-component --save
config-component默认提供了2中类型组件,
For common component:
import {ConfigComponent} from 'config-component' ... <ConfigComponent initialValues={...} schema={...} uiConfig={...} />
For form component:
import {ConfigForm} from 'config-component' ... <ConfigComponent initialValues={...} schema={...} uiConfig={...} onSubmit={()=>{...}} componentSet={...} />
params:
ℹ️schema: the core data you care, ConfigComponent will verify you core data with schema,you can specify alt props in item, when error occurs, will show alt as default valueℹ️initialValues: init value in you comp
ℹ️uiConfig: define your ui interface with json config, support event hooks, see example in playground file.
ℹ️onSubmit: used only in form when submit data.
ℹ️componentSet: support Ant Design or Fusion or you selfdefine Components.
代码示例:
import React, { useContext } from 'react' import * as yup from 'yup' import * as AntdComponents from 'antd' import moment from 'moment' import { ConfigForm } from 'config-component' import 'antd/dist/antd.css' const schema = yup.object().shape({ firstName: yup.string().required(), lastName: yup.string().required(), startTime: yup.array().required(), useTime: yup.array().required().when('startTime', (startTime, schem) => { return schem.test( 'check-start', 'useTime required', value => { return !!value }, ).test( 'check-start', 'useTime start >= startTime start', value => { return value && startTime[0].milliseconds(0).valueOf() <= value[0].milliseconds(0).valueOf() }, ).test( 'check-end', 'useTime end >= startTime end', value => { return value && startTime[1].milliseconds(0).valueOf() <= value[1].milliseconds(0).valueOf() }, ).required() }), agree: yup.boolean().required().test( 'check-agree', 'agree must checked', value => { return !!value }, ).required(), }) const initialValues = { firstName: 'Tony', lastName: 'Stark', startTime: [moment('2019-09-01'), moment('2019-09-03')], } export default function App() { const formConfig = { initialValues, schema, onSubmit: values => console.log('Your values are:', values), componentSet: AntdComponents, } const formItemLayout = { labelCol: { xs: { span: 24 }, sm: { span: 8 }, }, wrapperCol: { xs: { span: 24 }, sm: { span: 16 }, }, } const tailFormItemLayout = { wrapperCol: { xs: { span: 24, offset: 0, }, sm: { span: 16, offset: 8, }, }, } const onChangeWrapper = schemaKey => (form, e) => { const { onChange, value } = form.getFieldProps(schemaKey) console.log('prevalue', value) onChange(e) } return ( <ConfigForm {...formConfig} uiConfig={{ layout: formItemLayout, items: [ { label: 'First Name: ', comp: 'Input', schemaKey: 'firstName', props: {}, }, { label: 'Last Name: ', comp: 'Input', schemaKey: 'lastName', props: { onChange: onChangeWrapper('lastName'), }, }, { label: 'Start Time: ', comp: 'DatePicker.RangePicker', schemaKey: 'startTime', props: {}, }, { label: 'Use Time: ', comp: 'DatePicker.RangePicker', schemaKey: 'useTime', props: {}, }, { layout: tailFormItemLayout, comp: [ { comp: 'Checkbox', schemaKey: 'agree', props: { color: '#999', }, }, { comp: 'span', children: '赞成协议', props: { style: { marginLeft: '10px', color: '#999', }, }, }, ], props: {}, }, { type: 'submit', layout: tailFormItemLayout, comp: [ { type: 'submit', comp: 'Button', children: '提交', props: { type: 'primary', htmlType: 'submit', key: 'submit', }, }, { type: 'reset', comp: 'Button', children: '重置', props: { type: 'primary', key: 'reset', style: { marginLeft: '10px' }, }, }, ], props: {}, }, ], }} /> ) }
playground:https://chalecao.github.io/co...
online example: https://codesandbox.io/s/conf...
online example: https://codesandbox.io/s/conf...
面向数据的编程,核心关注数据