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

Fix 3702 order refunding number input #3826

Merged
merged 11 commits into from Feb 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -182,7 +182,7 @@ class InvoiceContainer extends Component {
if (refundedQuantity !== 0) {
editedItems.push(isEdited);
}
} else {
} else if (refundedQuantity !== 0) {
editedItems.push({
id: lineItem._id,
title: lineItem.title,
Expand Down
Expand Up @@ -21,20 +21,12 @@ class NumberTypeInput extends Component {
maxValue: props.maxValue || undefined,
className: {}
};

this.handleIncrementButton = this.handleIncrementButton.bind(this);
this.handleDecrementButton = this.handleDecrementButton.bind(this);
this.handleChange = this.handleChange.bind(this);
}

handleIncrementButton = (event) => {
const value = this.state.value + 1;

if (this.state.maxValue && value <= this.state.maxValue) {
this.setState({
value,
className: { edited: true }
});
this.handleChange(event, value);
}
}
Expand All @@ -43,17 +35,45 @@ class NumberTypeInput extends Component {
const value = this.state.value - 1;

if (value >= this.state.minValue) {
this.setState({
value,
className: { edited: true }
});
this.handleChange(event, value);
}
}

handleChange = (event, value) => {
if (this.props.onChange) {
this.props.onChange(event, value);
let newValue = parseInt(value, 10);
const { maxValue, minValue } = this.state;

// prevent the new value from being
// greater or less than the min and max values
if (newValue > maxValue) {
newValue = maxValue;
}
if (newValue < minValue) {
newValue = minValue;
}

// setting the value state
// if a new value set edited css class
this.setState({
value: newValue,
className: { edited: (newValue !== maxValue) }
});

// if props.onChange and the new value is a number
if (this.props.onChange && !isNaN(newValue)) {
this.props.onChange(event, newValue);
}
}

handleBlur = (event) => {
const { maxValue } = this.state;
// if input is left empty reset
// it's value to be the max value
if (isNaN(this.state.value)) {
this.setState({
value: maxValue
});
this.props.onChange(event, maxValue);
}
}

Expand All @@ -65,13 +85,14 @@ class NumberTypeInput extends Component {

return (
<div className="rui number-input">
<input
<Components.TextField
className={fieldClassName}
min={this.state.minValue}
max={this.state.maxValue}
value={this.state.value}
onChange={this.handleChange}
type="number"
onBlur={this.handleBlur}
onChange={this.handleChange}
maxValue={this.state.maxValue}
minValue={this.state.minValue}
value={this.state.value}
/>
<div className="stacked-buttons">
<Components.Button
Expand All @@ -84,6 +105,7 @@ class NumberTypeInput extends Component {
<Components.Button
className="button"
icon="fa fa-chevron-down"
disabled={this.state.minValue === this.state.value}
onClick={this.handleDecrementButton}
/>
</div>
Expand Down
11 changes: 10 additions & 1 deletion imports/plugins/core/ui/client/components/textfield/textfield.js
Expand Up @@ -28,7 +28,12 @@ class TextField extends Component {
if (this.props.isCurrency && !this.state.isEditing) {
return (this.state && this.state.value) || this.props.value || "";
}
return this.props.value || "";
// if the props.value is not a number
// return ether the value or and empty string
if (isNaN(this.props.value)) {
return this.props.value || "";
}
return this.props.value;
}

/**
Expand Down Expand Up @@ -182,6 +187,8 @@ class TextField extends Component {
placeholder={placeholder}
ref="input"
type={this.props.type || "text"}
min={this.props.minValue}
max={this.props.maxValue}
value={this.value}
style={this.props.style}
disabled={this.props.disabled}
Expand Down Expand Up @@ -297,6 +304,8 @@ TextField.propTypes = {
isValid: PropTypes.bool,
label: PropTypes.string,
maxRows: PropTypes.number,
maxValue: PropTypes.any,
minValue: PropTypes.number,
multiline: PropTypes.bool,
name: PropTypes.string,
onBlur: PropTypes.func,
Expand Down
Expand Up @@ -186,7 +186,8 @@
text-align: center;
background-color: #f7f7f7;
border: solid 1px #cccccc;
flex: 1;
flex: 0 0 auto;
padding: 0;
}

.edited {
Expand Down