Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Select first variant initially

Marcin Gębalac25f030691fc
diff --git a/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js b/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
index 679813bbb6..1832ba07ef 100644
--- a/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
+++ b/saleor/static/js/components/variantPicker/AttributeSelectionWidget.js
@@ -5,7 +5,8 @@ export default class AttributeSelectionWidget extends Component {
static propTypes = {
attribute: PropTypes.object.isRequired,
- handleChange: PropTypes.func.isRequired
+ handleChange: PropTypes.func.isRequired,
+ selected: PropTypes.string.isRequired
};
handleChange = (event) => {
@@ -14,7 +15,7 @@ export default class AttributeSelectionWidget extends Component {
}
render() {
- const { display, pk, values } = this.props.attribute;
+ const { attribute: { display, pk, values }, selected } = this.props;
return (
<div className="form-group">
<label className="control-label">{display}</label>
@@ -25,6 +26,7 @@ export default class AttributeSelectionWidget extends Component {
<input
name={pk}
onChange={this.handleChange}
+ defaultChecked={selected === value.pk.toString()}
type="radio"
value={value.pk}
/>
diff --git a/saleor/static/js/components/variantPicker/VariantPicker.js b/saleor/static/js/components/variantPicker/VariantPicker.js
index ba818fd561..3d510e9788 100644
--- a/saleor/static/js/components/variantPicker/VariantPicker.js
+++ b/saleor/static/js/components/variantPicker/VariantPicker.js
@@ -17,11 +17,14 @@ export default class VariantPicker extends Component {
constructor(props) {
super(props);
+ const { variants } = this.props;
+ const matchedVariant = variants.length ? variants[0] : null;
+ const selection = matchedVariant ? matchedVariant.attributes : {};
this.state = {
errors: {},
quantity: 1,
- selection: {},
- variant: null
+ variant: matchedVariant,
+ selection: selection
};
}
@@ -55,15 +58,9 @@ export default class VariantPicker extends Component {
handleAttributeChange = (attrId, valueId) => {
this.setState({
- selection: Object.assign(this.state.selection, { [attrId]: valueId })
+ selection: Object.assign({}, this.state.selection, { [attrId]: valueId })
}, () => {
- let matchedVariant = null;
- this.props.variants.forEach(variant => {
- if (_.isEqual(this.state.selection, variant.attributes)) {
- matchedVariant = variant;
- }
- });
- this.setState({variant: matchedVariant});
+ this.matchVariantFromSelection();
});
}
@@ -71,9 +68,19 @@ export default class VariantPicker extends Component {
this.setState({quantity: parseInt(event.target.value)});
}
+ matchVariantFromSelection() {
+ let matchedVariant = null;
+ this.props.variants.forEach(variant => {
+ if (_.isEqual(this.state.selection, variant.attributes)) {
+ matchedVariant = variant;
+ }
+ });
+ this.setState({ variant: matchedVariant });
+ }
+
render() {
const { attributes } = this.props;
- const { quantity, variant, errors } = this.state;
+ const { errors, selection, quantity, variant } = this.state;
const addToCartBtnClasses = classNames({
'btn btn-lg btn-block btn-primary': true,
@@ -87,6 +94,7 @@ export default class VariantPicker extends Component {
attribute={attribute}
handleChange={this.handleAttributeChange}
key={i}
+ selected={selection[attribute.pk]}
/>
)}
<QuantityInput