Skip to content

Commit e0ee4da

Browse files
committed
add FormBlock
1 parent 790b249 commit e0ee4da

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

docs/src/js/menuList.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ module.exports = [
9191
component: require('./pages/formControl.jsx')
9292
},
9393

94+
{
95+
path: '/formBlock',
96+
component: require('./pages/formBlock.jsx')
97+
},
98+
9499
{
95100
path: '/cascade',
96101
component: require('./pages/cascade.jsx')

docs/src/js/pages/formBlock.jsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react'
2+
import Code from '../Code'
3+
import Example from '../Example'
4+
import { Form, FormControl, FormBlock } from 'rctui'
5+
import { Cn, En } from '../Language'
6+
7+
module.exports = class extends React.Component {
8+
constructor (props) {
9+
super(props)
10+
this.state = {
11+
data: {
12+
base: {
13+
email: 'test@123.com',
14+
nickname: 'test'
15+
}
16+
}
17+
}
18+
}
19+
20+
render () {
21+
return (
22+
<div>
23+
<div className="header">
24+
<h1>FormBlock</h1>
25+
<Cn tag="h2">表单区块</Cn>
26+
</div>
27+
28+
<div className="content pure-form">
29+
<Cn>
30+
表单区块用来处理复杂表单数据中嵌套的部分
31+
</Cn>
32+
33+
<h2 className="subhead">Example</h2>
34+
35+
<Example>
36+
<Form onSubmit={d => console.log(d)} button="Submit" data={this.state.data}>
37+
<FormBlock name="base">
38+
<FormControl required label="email" name="email" type="email" />
39+
<FormControl required label="nickname" name="nickname" type="text" />
40+
</FormBlock>
41+
<FormBlock layout="inline" name="extra" value={{
42+
avatar: '',
43+
birthday: '1980-01-01',
44+
city: 'nanjing'
45+
}}>
46+
<FormControl label="avatar" name="avatar" type="text" />
47+
<FormControl required label="birthday" name="birthday" type="date" />
48+
<FormControl label="city" name="city" type="text" />
49+
</FormBlock>
50+
</Form>
51+
</Example>
52+
>
53+
</div>
54+
</div>
55+
)
56+
}
57+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "rctui",
33
"author": "lobos841@gmail.com",
4-
"version": "0.7.16",
4+
"version": "0.7.18-rc",
55
"description": "a collection of components for React",
66
"main": "./src/index.js",
77
"scripts": {

src/FormBlock.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import React, { Component } from 'react'
2+
import PropTypes from './utils/proptypes'
3+
import { objectAssign } from './utils/objects'
4+
import Enhance from './higherOrders/FormItem'
5+
6+
class FormBlock extends Component {
7+
constructor (props) {
8+
super(props)
9+
this.state = {}
10+
11+
this.itemBind = this.itemBind.bind(this)
12+
this.itemUnbind = this.itemUnbind.bind(this)
13+
this.itemChange = this.itemChange.bind(this)
14+
15+
this.items = {}
16+
}
17+
18+
getChildContext () {
19+
const { columns, labelWidth, layout, hintType } = this.props
20+
21+
const np = {}
22+
if (columns) np.columns = columns
23+
if (labelWidth) np.labelWidth = labelWidth
24+
if (layout) np.layout = layout
25+
if (hintType) np.hintType = hintType
26+
27+
return {
28+
formData: this.props.value,
29+
itemBind: this.itemBind,
30+
itemUnbind: this.itemUnbind,
31+
itemChange: this.itemChange,
32+
controlProps: objectAssign({}, this.context.controlProps, np)
33+
}
34+
}
35+
36+
itemBind (item) {
37+
const { name, value } = item
38+
const data = {...this.props.value}
39+
this.items[name] = item
40+
if (value !== undefined && !data[name]) {
41+
data[name] = value
42+
}
43+
this.props.onChange(data)
44+
}
45+
46+
itemUnbind (name) {
47+
delete this.items[name]
48+
const data = {...this.props.value}
49+
delete data[name]
50+
this.props.onChange(data)
51+
}
52+
53+
itemChange (name, value) {
54+
const data = objectAssign({}, this.props.value, {[name]: value})
55+
this.props.onChange(data)
56+
}
57+
58+
validate (data) {
59+
return Object.keys(this.items).reduce((suc, key) => {
60+
return suc && (this.items[key].validate(data[key], true) === true)
61+
}, true)
62+
}
63+
64+
render () {
65+
const { className, children } = this.props
66+
return (
67+
<div className={className}>
68+
{children}
69+
</div>
70+
)
71+
}
72+
}
73+
74+
FormBlock.isFormBlock = true
75+
76+
FormBlock.propTypes = {
77+
children: PropTypes.any,
78+
className: PropTypes.string,
79+
columns: PropTypes.number,
80+
hintType: PropTypes.oneOf(['block', 'none', 'pop', 'inline']),
81+
labelWidth: PropTypes.number_string,
82+
layout: PropTypes.oneOf(['aligned', 'stacked', 'inline']),
83+
onChange: PropTypes.func.isRequired,
84+
value: PropTypes.object
85+
}
86+
87+
FormBlock.defaultProps = {
88+
value: {}
89+
}
90+
91+
FormBlock.contextTypes = {
92+
controlProps: PropTypes.object
93+
}
94+
95+
FormBlock.childContextTypes = {
96+
formData: PropTypes.object,
97+
itemBind: PropTypes.func,
98+
itemChange: PropTypes.func,
99+
itemUnbind: PropTypes.func,
100+
controlProps: PropTypes.object
101+
}
102+
103+
export default Enhance(FormBlock)
104+

src/higherOrders/FormItem.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,14 @@ export default function FormItem (Component) {
9494
const { formData } = this.context
9595
const { type } = this.props
9696

97+
let validate
98+
9799
// component's inner validate
98-
const validate = getValidate(type)
100+
if (Component.isFormBlock) {
101+
if (this._el) validate = this._el.validate.bind(this._el)
102+
} else {
103+
validate = getValidate(type)
104+
}
99105

100106
const result = validate ? validate(value, this.props, formData)
101107
: Validation.validate(value, getValueType(type), formData, this.props, this.setResult)
@@ -164,6 +170,7 @@ export default function FormItem (Component) {
164170

165171
let el = (
166172
<Component {...props}
173+
ref={(el) => { this._el = el }}
167174
hasError={this.state.result instanceof Error}
168175
onChange={this.handleChange}
169176
value={value}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export { default as FormSubmit } from './FormSubmit'
2727
export { default as Form } from './Form'
2828
export { default as FormItem } from './FormItem'
2929
export { default as FormText } from './FormText'
30+
export { default as FormBlock } from './FormBlock'
3031
export { default as If } from './If'
3132

3233
export { default as Alert } from './Alert'

0 commit comments

Comments
 (0)