UNPKG

1.8 kBJSXView Raw
1/**
2 * Set of form field.
3 * @class ApFieldSet
4 */
5
6'use strict'
7
8import React, {PropTypes as types} from 'react'
9import classnames from 'classnames'
10import ReactDOM from 'react-dom'
11import {ApLayoutMixin} from 'apeman-react-mixin-layout'
12import uuid from 'uuid'
13import {ApStyle} from 'apeman-react-style'
14import numcal from 'numcal'
15
16/** @lends ApFieldSet */
17const ApFieldSet = React.createClass({
18
19 // --------------------
20 // Specs
21 // --------------------
22
23 propTypes: {
24 plain: types.bool
25 },
26
27 mixins: [
28 ApLayoutMixin
29 ],
30
31 statics: {},
32
33 getInitialState () {
34 return {}
35 },
36
37 getDefaultProps () {
38 return {
39 plain: false,
40 uuid: uuid.v4()
41 }
42 },
43
44 render () {
45 const s = this
46 let { props, layouts } = s
47 let { uuid } = props
48 let style = {
49 [`.ap-field-set-${uuid} .ap-field-label`]: layouts.label
50 }
51 return (
52 <fieldset id={ props.id }
53 className={ classnames('ap-field-set', {
54 'ap-field-set-plain': props.plain
55 }, `ap-field-set-${uuid}`, props.className) }>
56 <ApStyle className='ap-field-set-style'
57 data={ style }/>
58 { props.children }
59 </fieldset>
60 )
61 },
62
63 // ------------------
64 // For ApLayoutMixin
65 // ------------------
66
67 getInitialLayouts () {
68 const s = this
69 return {
70 label: {}
71 }
72 },
73
74 calcLayouts () {
75 const s = this
76 let node = ReactDOM.findDOMNode(s)
77
78 let labels = node.querySelectorAll('.ap-field-label-inner')
79 let labelWidth = numcal.max.apply(
80 numcal, [ ...labels ].map((label) => label.getBoundingClientRect().width + 2)
81 ) || null
82 return {
83 label: {
84 minWidth: labelWidth ? parseInt(labelWidth) : null
85 }
86 }
87 }
88})
89
90export default ApFieldSet