UNPKG

2.22 kBJSXView Raw
1/**
2 * Radio component.
3 * @class ApRadio
4 */
5
6'use strict'
7
8import React, {PropTypes as types} from 'react'
9import classnames from 'classnames'
10import {ApUUIDMixin} from 'apeman-react-mixins'
11import ApRadio from './ap_radio'
12
13/** @lends ApRadioGroup */
14const ApRadioGroup = React.createClass({
15
16 // --------------------
17 // Specs
18 // --------------------
19
20 propTypes: {
21 /** Document id prefix */
22 prefix: types.string,
23 /** Name of Radio input */
24 name: types.string.isRequired,
25 /** Value and label titles */
26 options: types.object.isRequired,
27 /** Checked state for each values */
28 checked: types.object.isRequired,
29 /** Handle for change event */
30 onChange: types.func,
31 /** Icon class name for normal state */
32 icon: types.string,
33 /** Icon class name for checked state */
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 // Handle
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
106export default ApRadioGroup