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

react/fix inputNumber handleAdjust logic #518

Merged
merged 15 commits into from
Apr 1, 2019

Conversation

clairesunstudio
Copy link
Contributor

@clairesunstudio clairesunstudio commented Mar 26, 2019

Fixed

To test:

  • In InputCurrency, InputNumber stories, remove min and max props and click the up/down buttons.
  • In InputNumber story, set step to 0.1 and use up/down button.

@clairesunstudio clairesunstudio requested review from smurrayatwork and avrilpearl and removed request for smurrayatwork March 26, 2019 18:26
@clairesunstudio clairesunstudio changed the title fix inputNumber handleAdjust logic react/fix inputNumber handleAdjust logic Mar 26, 2019
Copy link
Contributor

@avrilpearl avrilpearl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clairesunstudio --> this looks good to me, obviously made a bunch of updates to the previous logic so feel free to merge it once you review those.

@clairesunstudio
Copy link
Contributor Author

changes looks good to me

one small thing i noticed that in InputCurrency, if you put in a dollar value becomes 0, vs if you put in any other invalid character value becomes ""

const handleBlur = () => {
const inputEl = ref.current;
if (is.empty(inputEl.value)) {
const stringValue = inputEl.value;
const isNotNumber = !stringValue || isNaN(Number(numbro.unformat(stringValue)));
Copy link
Contributor

@smurrayatwork smurrayatwork Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isNotNumber = !stringValue || isNaN(Number(numbro.unformat(stringValue)));
const isNotNumber = is.empty(stringValue) || Number.isNaN(Number(numbro.unformat(stringValue)));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is doing the same thing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because stringValue can't be number 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number.isNaN() is the same as isNaN() since we already made the parameter a number

newValue = props.min;
}
let newValue = inputEl.value ? Number(inputEl.value) : inputEl.value;
if (hasNumberProperty(props, 'max') && newValue > props.max) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (hasNumberProperty(props, 'max') && newValue > props.max) {
if (hasNumberProperty(props, 'max') && (is.number(newValue) && newValue > props.max)) {

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is not needed, you can only enter numbers, periods, commas, or null in an input type number, so this will be a number null, or a string of . or , which will evaluate to false above.

if (!is.empty(inputEl.value)) {
inputEl.value = Number(numbro(newValue)
.format({ mantissa: countDecimals(props.step) }));
if (hasNumberProperty(props, 'min') && newValue < props.min) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (hasNumberProperty(props, 'min') && newValue < props.min) {
if (hasNumberProperty(props, 'min') && (is.number(newValue) && newValue < props.min)) {

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is not needed, you can only enter numbers, periods, commas, or null in an input type number, so this will be a number null, or a string of . or , which will evaluate to false above.

}
handleChange(e);
let newValue = inputEl.value ? Number(inputEl.value) : inputEl.value;
if (direction === 'up' && (!hasNumberProperty(props, 'max') || newValue < props.max)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (direction === 'up' && (!hasNumberProperty(props, 'max') || newValue < props.max)) {
if (direction === 'up' && (!hasNumberProperty(props, 'max') || (is.number(newValue) && newValue < props.max))) {

This comment was marked as outdated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is not needed, you can only enter numbers, periods, commas, or null in an input type number, so this will be a number, null, or a string of . or , - null and strings will evaluate to false above which is correct so this is fine.

handleChange(e);
let newValue = inputEl.value ? Number(inputEl.value) : inputEl.value;
if (direction === 'up' && (!hasNumberProperty(props, 'max') || newValue < props.max)) {
newValue = newValue ? (newValue + props.step).toFixed(countDecimals(props.step)) : props.step;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if newValue is a string, toFixed won't work because it doesn't exist.

Suggested change
newValue = newValue ? (newValue + props.step).toFixed(countDecimals(props.step)) : props.step;
newValue = newValue ? Number(newValue + props.step).toFixed(countDecimals(props.step)) : props.step;

This comment was marked as outdated.

This comment was marked as outdated.

Copy link
Contributor

@avrilpearl avrilpearl Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newvalue would already be a number at this point - we do not need to cast it again. --> newValue < props.max would only evaluate to true if is was a number less than min prop

props.onChange(e, newValue, props.id);
}
});
} else if (direction === 'down' && (!hasNumberProperty(props, 'min') || newValue > props.min)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (direction === 'down' && (!hasNumberProperty(props, 'min') || newValue > props.min)) {
} else if (direction === 'down' && (!hasNumberProperty(props, 'min') || (is.number(newValue) && newValue > props.min))) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check is not needed, you can only enter numbers, periods, commas, or null in an input type number, so this will be a number, null, or a string of . or , - null and strings will evaluate to false above which is correct so this is fine.

}
});
} else if (direction === 'down' && (!hasNumberProperty(props, 'min') || newValue > props.min)) {
newValue = newValue ? (newValue + props.step * -1).toFixed(countDecimals(props.step)) : (props.step * -1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
newValue = newValue ? (newValue + props.step * -1).toFixed(countDecimals(props.step)) : (props.step * -1);
newValue = newValue ? Number(newValue + props.step * -1).toFixed(countDecimals(props.step)) : (props.step * -1);

This comment was marked as outdated.

Copy link
Contributor

@avrilpearl avrilpearl Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newvalue would already be a number at this point - we do not need to cast it again. --> newValue > props.min would only evaluate to true if is was a number greater than min prop

@clairesunstudio clairesunstudio merged commit 15ea3e9 into develop Apr 1, 2019
@clairesunstudio clairesunstudio deleted the react/input-buttons-bug-fix branch April 1, 2019 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants