11import React , { Component , PropTypes } from 'react'
22import { HOC as hoc } from 'formsy-react'
33import cn from 'classnames'
4+ import { find } from "lodash" ;
5+ import { numberWithCommas } from './format'
46
57class RadioGroup extends Component {
68
@@ -15,16 +17,28 @@ class RadioGroup extends Component {
1517 this . props . onChange ( this . props . name , value )
1618 }
1719
20+ getSelectedOption ( ) {
21+ const { options = [ ] , getValue} = this . props ;
22+ const value = getValue ( )
23+ return find ( options , o => value === o . value )
24+ }
25+
1826 render ( ) {
1927 const { label, name, wrapperClass, options } = this . props
2028 const hasError = ! this . props . isPristine ( ) && ! this . props . isValid ( )
2129 const disabled = this . props . isFormDisabled ( ) || this . props . disabled
2230 const errorMessage = this . props . getErrorMessage ( ) || this . props . validationError
31+ const selectedOption = this . getSelectedOption ( )
32+ const hasPrice = find ( options , o => o . quoteUp )
2333
2434 const renderOption = ( radio , key ) => {
25- const checked = ( this . props . getValue ( ) === radio . value )
35+ const relativePrice = ( selectedOption , radio ) => {
36+ const price = ( radio . quoteUp || 0 ) - ( selectedOption . quoteUp || 0 )
37+ return ( price < 0 ? '-' : '+' ) + ' $' + numberWithCommas ( Math . abs ( price ) )
38+ }
39+ const checked = ( selectedOption && selectedOption . value === radio . value )
2640 const disabled = this . props . isFormDisabled ( ) || radio . disabled || this . props . disabled
27- const rClass = cn ( 'radio' , { disabled } )
41+ const rClass = cn ( 'radio' , { disabled, selected : checked } )
2842 const id = name + '-opt-' + key
2943 const setRef = ( c ) => this [ 'element-' + key ] = c
3044 return (
@@ -39,6 +53,15 @@ class RadioGroup extends Component {
3953 disabled = { disabled }
4054 />
4155 < label htmlFor = { id } > { radio . label } </ label >
56+ {
57+ hasPrice &&
58+ ! checked &&
59+ ( radio . quoteUp || selectedOption ) &&
60+ < div className = "radio-option-price" > { selectedOption ? relativePrice ( selectedOption , radio ) : `$${ numberWithCommas ( radio . quoteUp ) } ` } </ div >
61+ }
62+ {
63+ radio . description && checked && < div className = "radio-option-description" > { radio . description } </ div >
64+ }
4265 </ div >
4366 )
4467 }
0 commit comments