Skip to content

Commit 39a624c

Browse files
committed
winning submission from challenge 30083737 - Topcoder Connect - Wizard redesign - Controls new design
+ fix to format prices with comma
1 parent 2363173 commit 39a624c

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

components/Formsy/CheckboxGroup.jsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component, PropTypes } from 'react'
22
import { HOC as hoc } from 'formsy-react'
33
import cn from 'classnames'
4+
import { numberWithCommas } from './format'
45

56
class CheckboxGroup extends Component {
67

@@ -21,7 +22,7 @@ class CheckboxGroup extends Component {
2122
}
2223

2324
render() {
24-
const { label, name, options, layout } = this.props
25+
const { label, name, options, layout, wrapperClass } = this.props
2526
const hasError = !this.props.isPristine() && !this.props.isValid()
2627
const disabled = this.props.isFormDisabled() || this.props.disabled
2728
const errorMessage = this.props.getErrorMessage() || this.props.validationError
@@ -30,7 +31,7 @@ class CheckboxGroup extends Component {
3031
const curValue = this.props.getValue() || []
3132
const checked = curValue.indexOf(cb.value) !== -1
3233
const disabled = this.props.isFormDisabled() || cb.disabled || this.props.disabled
33-
const rClass = cn('checkbox-group-item', { disabled })
34+
const rClass = cn('checkbox-group-item', { disabled, selected: checked })
3435
const id = name+'-opt-'+key
3536
const setRef = (c) => this['element-' + key] = c
3637
return (
@@ -48,10 +49,16 @@ class CheckboxGroup extends Component {
4849
<label htmlFor={id}/>
4950
</div>
5051
<label className="tc-checkbox-label" htmlFor={id}>{cb.label}</label>
52+
{
53+
cb.quoteUp && !checked && <div className="checkbox-option-price"> {`+ $${numberWithCommas(cb.quoteUp)}`} </div>
54+
}
55+
{
56+
cb.description && checked && <div className="checkbox-option-description"> {cb.description} </div>
57+
}
5158
</div>
5259
)
5360
}
54-
const chkGrpClass = cn('checkbox-group', {
61+
const chkGrpClass = cn('checkbox-group', wrapperClass, {
5562
horizontal: layout === 'horizontal',
5663
vertical: layout === 'vertical'
5764
})

components/Formsy/RadioGroup.jsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, { Component, PropTypes } from 'react'
22
import { HOC as hoc } from 'formsy-react'
33
import cn from 'classnames'
4+
import { find } from "lodash";
5+
import { numberWithCommas } from './format'
46

57
class 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
}

components/Formsy/format.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Helper methods to format values
3+
*/
4+
5+
/**
6+
* Fomats number, separating every 3 digits with comma
7+
*
8+
* @param {Number} number
9+
*/
10+
export function numberWithCommas(number) {
11+
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
12+
}

0 commit comments

Comments
 (0)