Created
September 18, 2015 14:14
-
-
Save sbezludny/eca1323a508a73a44a7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| var React = require('react'); | |
| var Immutable = require('immutable'); | |
| var _ = require('lodash'); | |
| var connectToStores = require('../../../../decorators/connect-to-stores.jsx'); | |
| var Dropdown = require('react-select'); | |
| var Variants = React.createClass({ | |
| contextTypes: { | |
| locale: React.PropTypes.object, | |
| stores: React.PropTypes.object | |
| }, | |
| propTypes: { | |
| data: React.PropTypes.object | |
| }, | |
| getInitialState: function() { | |
| return { | |
| selectedVariants: this.props.data | |
| }; | |
| }, | |
| componentWillReceiveProps: function(nextProps) { | |
| this.setState({ | |
| selectedVariants: nextProps.data | |
| }); | |
| }, | |
| getValuesStr: function () { | |
| if(this.state.selectedVariants.isEmpty()) { | |
| return null; | |
| } | |
| return this.state.selectedVariants | |
| .map(function(v) { | |
| return v.get('url'); | |
| }) | |
| .join(','); | |
| }, | |
| getOptions: function() { | |
| return this.props.variants.map(function(variant) { | |
| return { | |
| label: variant.get('name'), | |
| value: variant.get('url') | |
| }; | |
| }).toArray(); | |
| }, | |
| onChange: function(value) { | |
| var selectedVariants = _.compact(value.split(',')) | |
| .map(function(url){ | |
| return Immutable.Map({ | |
| url: url | |
| }); | |
| }); | |
| this.setState({ | |
| selectedVariants: selectedVariants | |
| }); | |
| this.props.onChange(null, selectedVariants); | |
| }, | |
| render: function() { | |
| return ( | |
| <div> | |
| <Dropdown | |
| className='c-dropdown--margin' | |
| name='variant-dropdown' | |
| options={this.getOptions()} | |
| value={this.getValuesStr()} | |
| delimiter={','} | |
| placeholder={this.context.locale.t('fields.dropdown.placeholder', {item: 'variants'})} | |
| noResultsText={this.context.locale.t('fields.dropdown.noResultsFound', {item: 'variants'})} | |
| multi={true} | |
| onChange={this.onChange} | |
| /> | |
| </div> | |
| ); | |
| } | |
| }); | |
| module.exports = connectToStores(['productFamily'], function(context) { | |
| var data = context.stores.get('productFamily').getData(); | |
| return { | |
| variants: data ? data.get('variants') : Immutable.List() | |
| }; | |
| })(Variants); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment