Skip to content

Commit ada8ba9

Browse files
cnt1992janryWang
authored andcommitted
feat(refactor): perfect test suites and add builder demo in docs (#100)
* refactor: 拖拽组件替换成react-dnd * refactor: 调整交互 * test: update test cases * fix: 修复拖拽bug * chore: build * refactor: 完善测试用例
1 parent 17fc96c commit ada8ba9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2066
-1768
lines changed

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@
5353
- [国际化](./Examples/antd/International.md)
5454
- [知乎专栏](https://zhuanlan.zhihu.com/uform)
5555
- [GitHub](https://github.com/alibaba/uform)
56+
- [PlayGround DEMO](../packages/builder/src/demo/index-1-x.js)

packages/builder/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
"type": "git",
88
"url": "git+https://github.com/alibaba/uform.git"
99
},
10-
"scripts": {
11-
"start": "node scripts/start.js"
12-
},
1310
"bugs": {
1411
"url": "https://github.com/alibaba/uform/issues"
1512
},
@@ -28,6 +25,8 @@
2825
"@uform/utils": "^0.1.15",
2926
"@uform/validator": "^0.1.15",
3027
"classnames": "^2.2.5",
28+
"immutability-helper": "^3.0.0",
29+
"lodash.flow": "^3.5.0",
3130
"lodash.isequal": "^4.5.0",
3231
"lodash.merge": "^4.6.1",
3332
"lodash.pick": "^4.4.0",
@@ -37,6 +36,7 @@
3736
"moment": "^2.24.0",
3837
"prop-types": "^15.6.1",
3938
"react-dnd": "^7.4.1",
39+
"react-dnd-html5-backend": "^7.4.0",
4040
"react-powerplug": "^1.0.0",
4141
"react-redux": "^5.0.7",
4242
"redux": "^4.0.0",
@@ -46,7 +46,8 @@
4646
"uuid": "^3.2.1"
4747
},
4848
"publishConfig": {
49-
"access": "public"
49+
"access": "public",
50+
"registry": "https://registry.npm.alibaba-inc.com"
5051
},
5152
"gitHead": "4d068dad6183e8da294a4c899a158326c0b0b050",
5253
"devDependencies": {

packages/builder/src/App.js

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react'
1+
import React, { Component, createRef } from 'react'
22
import cls from 'classnames'
33
import PropTypes from 'prop-types'
44
import { connect } from 'react-redux'
@@ -16,11 +16,13 @@ import isEqual from 'lodash.isequal'
1616
import AppStyle from './style'
1717

1818
// components
19-
import FieldList from './components/fields/index'
20-
import Preview from './components/preview/index'
21-
import generateGlobalBtnList from './components/globalBtnList/index'
19+
import {
20+
FieldList,
21+
Preview,
22+
GlobalBtnList,
23+
PropsSetting
24+
} from './components/index'
2225

23-
import PropsSetting from './components/props/propsSetting'
2426
import { SchemaForm, Field } from './utils/baseForm'
2527
import defaultGlobalCfgList from './configs/supportGlobalCfgList'
2628

@@ -33,6 +35,8 @@ class App extends Component {
3335
systemError: false,
3436
accordionList: []
3537
}
38+
this.appRef = createRef(null)
39+
this.appHeaderRef = createRef(null)
3640
}
3741

3842
generateGlobalCfgList = () => {
@@ -63,10 +67,11 @@ class App extends Component {
6367
}}
6468
defaultValue={this.props.gbConfig}
6569
labelAlign='left'
70+
labelCol={10}
6671
labelTextAlign='right'
6772
>
6873
{globalCfgList.map(props => (
69-
<Field {...props} key={props.name} x-item-props={{ labelCol: 10 }} />
74+
<Field {...props} key={props.name} />
7075
))}
7176
</SchemaForm>
7277
)
@@ -93,7 +98,7 @@ class App extends Component {
9398
list.unshift({
9499
title: '全局配置',
95100
content: this.renderGlobalConfig(),
96-
expanded: true
101+
expanded: false
97102
})
98103
}
99104
return list
@@ -115,6 +120,17 @@ class App extends Component {
115120
this.setState({
116121
accordionList: this.getAccordionList()
117122
})
123+
124+
const appDom = this.appRef.current
125+
const appHeaderDom = this.appHeaderRef.current
126+
127+
if (appDom.offsetTop !== 0) {
128+
document.querySelector(
129+
'.schamaform-content'
130+
).style.height = `${window.innerHeight -
131+
appDom.offsetTop -
132+
appHeaderDom.offsetHeight}px`
133+
}
118134
}
119135

120136
componentWillUnmount() {
@@ -125,12 +141,12 @@ class App extends Component {
125141
})
126142
}
127143

128-
componentWillReceiveProps(nextProps) {
129-
let oldProperties = {}
130-
if (this.props.schema && this.props.schema.properties) {
131-
oldProperties = this.props.schema.properties
132-
}
133-
const { schema = {}, globalCfg = {}, initSchema: _initSchema } = nextProps
144+
componentDidUpdate(prevProps) {
145+
const oldProperties =
146+
prevProps.schema && prevProps.schema.properties
147+
? prevProps.schema.properties
148+
: {}
149+
const { schema = {}, globalCfg = {}, initSchema: _initSchema } = this.props
134150

135151
const { properties = {} } = schema
136152

@@ -205,21 +221,20 @@ class App extends Component {
205221
}
206222
}
207223

208-
renderGlobalBtnList() {
209-
return generateGlobalBtnList(this.props)
210-
}
211-
212224
render() {
213225
const { initSchemaData, renderEngine } = this.props
214226
const { Accordion, version: UIVersion } = this.props.UI
215227

216-
const contentHeight = window.innerHeight - 64
228+
const contentHeight = window.innerHeight
217229

218230
return this.state.systemError ? (
219231
<p>系统发生异常</p>
220232
) : (
221-
<AppStyle className={cls('schemaform-app', this.props.className)}>
222-
<div className='schemaform-header'>
233+
<AppStyle
234+
ref={this.appRef}
235+
className={cls('schemaform-app', this.props.className)}
236+
>
237+
<div ref={this.appHeaderRef} className='schemaform-header'>
223238
<a
224239
href='javascript:;'
225240
className='schemaform-back'
@@ -231,7 +246,7 @@ class App extends Component {
231246
</a>
232247
<h1>编辑表单</h1>
233248
<div className='schemaform-header-btns'>
234-
{this.renderGlobalBtnList()}
249+
<GlobalBtnList {...this.props} />
235250
</div>
236251
</div>
237252
<div className='schamaform-content' style={{ height: contentHeight }}>
@@ -253,7 +268,7 @@ class App extends Component {
253268
{UIVersion === '1.x' ? (
254269
<Accordion
255270
dataSource={this.state.accordionList}
256-
defaultExpandedKeys={['0', '1']}
271+
defaultExpandedKeys={['1']}
257272
/>
258273
) : (
259274
<Accordion
@@ -336,8 +351,7 @@ const mapDispatchToProps = dispatch => ({
336351
initSchema: data => dispatch(initSchema(data)),
337352
changeCodeMode: codemode => dispatch(changeCodeMode(codemode)),
338353
changeComponent: componentId => dispatch(changeComponent(componentId)),
339-
editComponent: (id, propsData, containerId) =>
340-
dispatch(editComponent(id, propsData, containerId))
354+
editComponent: (...args) => dispatch(editComponent(...args))
341355
})
342356

343357
class StyledAppComp extends React.Component {

packages/builder/src/__tests__/actions/index.spec.js

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import * as actions from '../../actions/index'
22

33
describe('test actions', () => {
4-
test('changeLayoutId action', () => {
5-
expect(actions.changeLayoutId('111')).toEqual({
6-
type: 'CHANGE_LAYOUTID',
7-
data: {
8-
id: '111'
9-
}
10-
})
11-
})
12-
4+
// addComponent actions
135
test('addComponent action', () => {
14-
expect(actions.addComponent({
15-
type: 'string',
16-
title: '111'
17-
}, '222', '333', 'layout', '444')).toEqual({
6+
expect(
7+
actions.addComponent(
8+
{
9+
type: 'string',
10+
title: '111'
11+
},
12+
'222',
13+
'333',
14+
'layout',
15+
'444'
16+
)
17+
).toEqual({
1818
type: 'ADD_COMPONENT',
1919
data: {
2020
id: '333',
@@ -41,50 +41,57 @@ describe('test actions', () => {
4141
})
4242

4343
test('editComponent action', () => {
44-
expect(actions.editComponent('111', {
45-
__id__: 222
46-
}, '333')).toEqual({
44+
expect(
45+
actions.editComponent('111', {
46+
__id__: 222
47+
})
48+
).toEqual({
4749
type: 'EDIT_COMPONENT',
4850
data: {
4951
id: '111',
5052
propsData: {
5153
__id__: 222
52-
},
53-
containerId: '333'
54+
}
5455
}
5556
})
5657
})
5758

5859
test('deleteComponent action', () => {
59-
expect(actions.deleteComponent('111')).toEqual({
60+
expect(actions.deleteComponent(['111'])).toEqual({
6061
type: 'DELETE_COMPONENT',
6162
data: {
62-
id: '111'
63+
id: ['111']
6364
}
6465
})
6566
})
6667

6768
test('showComponentProps action', () => {
68-
expect(actions.showComponentProps('111', {
69-
type: 'string',
70-
title: '222'
71-
}, '333')).toEqual({
69+
expect(
70+
actions.showComponentProps(
71+
['111'],
72+
{
73+
type: 'string',
74+
title: '222'
75+
}
76+
)
77+
).toEqual({
7278
type: 'SHOW_COMPONENT_PROPS',
7379
data: {
74-
id: '111',
80+
id: ['111'],
7581
comp: {
7682
type: 'string',
7783
title: '222'
78-
},
79-
containerId: '333'
84+
}
8085
}
8186
})
8287
})
8388

8489
test('editComponentProps action', () => {
85-
expect(actions.editComponentProps('111', {
86-
__id__: '222'
87-
})).toEqual({
90+
expect(
91+
actions.editComponentProps('111', {
92+
__id__: '222'
93+
})
94+
).toEqual({
8895
type: 'EDIT_COMPONENT_PROPS',
8996
data: {
9097
id: '111',
@@ -123,9 +130,11 @@ describe('test actions', () => {
123130
})
124131

125132
test('changeGbConfig action', () => {
126-
expect(actions.changeGbConfig({
127-
aaa: 1
128-
})).toEqual({
133+
expect(
134+
actions.changeGbConfig({
135+
aaa: 1
136+
})
137+
).toEqual({
129138
type: 'CHANGE_GB_CONFIG',
130139
data: {
131140
aaa: 1
@@ -134,9 +143,11 @@ describe('test actions', () => {
134143
})
135144

136145
test('initSchema action', () => {
137-
expect(actions.initSchema({
138-
aaa: 1
139-
})).toEqual({
146+
expect(
147+
actions.initSchema({
148+
aaa: 1
149+
})
150+
).toEqual({
140151
type: 'INIT_SCHEMA',
141152
data: {
142153
aaa: 1

packages/builder/src/__tests__/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ test('shoud correct render APP', () => {
1010
}
1111
renderer.render(<Index {..._props} />)
1212
const result = renderer.getRenderOutput()
13-
const matchProps = result.props.children.props.children.props
13+
const matchProps = result.props.children.props
1414
expect(matchProps).toHaveProperty('renderEngine')
1515
})

0 commit comments

Comments
 (0)