1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict'
|
7 |
|
8 | import React, {PropTypes as types} from 'react'
|
9 | import classnames from 'classnames'
|
10 | import {ApUUIDMixin} from 'apeman-react-mixins'
|
11 | import ApRadio from './ap_radio'
|
12 |
|
13 |
|
14 | const ApRadioGroup = React.createClass({
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | propTypes: {
|
21 |
|
22 | prefix: types.string,
|
23 |
|
24 | name: types.string.isRequired,
|
25 |
|
26 | options: types.object.isRequired,
|
27 |
|
28 | checked: types.object.isRequired,
|
29 |
|
30 | onChange: types.func,
|
31 |
|
32 | icon: types.string,
|
33 |
|
34 | checkedIcon: types.string
|
35 | },
|
36 |
|
37 | mixins: [
|
38 | ApUUIDMixin
|
39 | ],
|
40 |
|
41 | getInitialState () {
|
42 | return {}
|
43 | },
|
44 |
|
45 | getDefaultProps () {
|
46 | return {
|
47 | prefix: null,
|
48 | name: null,
|
49 | title: '',
|
50 | checked: {},
|
51 | options: {},
|
52 | onChange: null,
|
53 | icon: ApRadio.DEFAULT_ICON,
|
54 | checkedIcon: ApRadio.DEFAULT_CHECKED_ICON
|
55 | }
|
56 | },
|
57 |
|
58 | render () {
|
59 | const s = this
|
60 | let { props } = s
|
61 |
|
62 | let {
|
63 | prefix,
|
64 | name,
|
65 | options,
|
66 | checked,
|
67 | icon,
|
68 | checkedIcon
|
69 | } = props
|
70 |
|
71 | prefix = prefix || s.uuid
|
72 |
|
73 | return (
|
74 | <div className={ classnames('ap-Radio-group', props.className) }>
|
75 | {
|
76 | Object.keys(options || {}).map((value) => (
|
77 | <ApRadio key={ value }
|
78 | name={ name }
|
79 | value={ value }
|
80 | id={ `${prefix}-${value}`}
|
81 | checked={ !!checked[value] }
|
82 | title={ options[value] }
|
83 | icon={ icon }
|
84 | checkedIcon={ checkedIcon }
|
85 | onChange={ s.handleChange }
|
86 | />
|
87 | ))
|
88 | }
|
89 | </div>
|
90 | )
|
91 | },
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | handleChange (e) {
|
98 | const s = this
|
99 | let { props } = s
|
100 | if (props.onChange) {
|
101 | props.onChange(e)
|
102 | }
|
103 | }
|
104 | })
|
105 |
|
106 | export default ApRadioGroup
|