Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2286: added currency formatting #3808

Merged
merged 1 commit into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion imports/plugins/core/ui/client/components/textfield/textfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ import React, { Component } from "react";
import PropTypes from "prop-types";
import classnames from "classnames";
import TextareaAutosize from "react-textarea-autosize";
import { unformat } from "accounting-js";
import { Components, registerComponent } from "@reactioncommerce/reaction-components";
import { i18next } from "/client/api";
import { i18next, formatPriceString } from "/client/api";


class TextField extends Component {
constructor(props) {
super(props);
if (props.isCurrency) {
this.state = {
value: formatPriceString(props.value),
isEditing: false
};
} else {
this.state = {};
}
}

/**
* Getter: value
* @return {String} value for text input
*/
get value() {
if (this.props.isCurrency && !this.state.isEditing) {
return (this.state && this.state.value) || this.props.value || "";
}
return this.props.value || "";
}

Expand Down Expand Up @@ -69,6 +86,12 @@ class TextField extends Component {
* @return {void}
*/
onBlur = (event) => {
if (this.props.isCurrency) {
this.setState({
value: formatPriceString(event.target.value),
isEditing: false
});
}
if (this.props.onBlur) {
this.props.onBlur(event, event.target.value, this.props.name);
}
Expand All @@ -81,6 +104,13 @@ class TextField extends Component {
* @return {void}
*/
onFocus = (event) => {
if (this.props.isCurrency) {
event.target.value = unformat(event.target.value);
this.setState({
value: event.target.value,
isEditing: true
});
}
if (this.props.onFocus) {
this.props.onFocus(event, event.target.value, this.props.name);
}
Expand Down Expand Up @@ -262,6 +292,7 @@ TextField.propTypes = {
i18nKeyLabel: PropTypes.string,
i18nKeyPlaceholder: PropTypes.string,
id: PropTypes.string,
isCurrency: PropTypes.bool,
isValid: PropTypes.bool,
label: PropTypes.string,
multiline: PropTypes.bool,
Expand Down
20 changes: 12 additions & 8 deletions imports/plugins/included/product-variant/components/variantForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ class VariantForm extends Component {
<div className="col-sm-6">
<Components.TextField
i18nKeyLabel="productVariant.price"
i18nKeyPlaceholder={formatPriceString("0.00")}
placeholder={formatPriceString("0.00")}
i18nKeyPlaceholder="0.00"
placeholder="0.00"
label="Price"
name="price"
ref="priceInput"
Expand All @@ -418,13 +418,14 @@ class VariantForm extends Component {
onChange={this.handleFieldChange}
onReturnKeyDown={this.handleFieldBlur}
validation={this.props.validation}
isCurrency
/>
</div>
<div className="col-sm-6">
<Components.TextField
i18nKeyLabel="productVariant.compareAtPrice"
i18nKeyPlaceholder={formatPriceString("0.00")}
placeholder={formatPriceString("0.00")}
i18nKeyPlaceholder="0.00"
placeholder="0.00"
label="Compare At Price"
name="compareAtPrice"
ref="compareAtPriceInput"
Expand All @@ -433,6 +434,7 @@ class VariantForm extends Component {
onChange={this.handleFieldChange}
onReturnKeyDown={this.handleFieldBlur}
validation={this.props.validation}
isCurrency
/>
</div>
</div>
Expand Down Expand Up @@ -642,8 +644,8 @@ class VariantForm extends Component {
<div className="col-sm-6">
<Components.TextField
i18nKeyLabel="productVariant.price"
i18nKeyPlaceholder={formatPriceString("0.00")}
placeholder={formatPriceString("0.00")}
i18nKeyPlaceholder="0.00"
placeholder="0.00"
label="Price"
name="price"
ref="priceInput"
Expand All @@ -656,13 +658,14 @@ class VariantForm extends Component {
validation={this.props.validation}
helpText={"Purchase price"}
i18nKeyHelpText={"admin.helpText.price"}
isCurrency
/>
</div>
<div className="col-sm-6">
<Components.TextField
i18nKeyLabel="productVariant.compareAtPrice"
i18nKeyPlaceholder={formatPriceString("0.00")}
placeholder={formatPriceString("0.00")}
i18nKeyPlaceholder="0.00"
placeholder="0.00"
label="Compare At Price"
name="compareAtPrice"
ref="compareAtPriceInput"
Expand All @@ -673,6 +676,7 @@ class VariantForm extends Component {
validation={this.props.validation}
helpText={"Original price or MSRP"}
i18nKeyHelpText={"admin.helpText.compareAtPrice"}
isCurrency
/>
</div>
</div>
Expand Down