-
Notifications
You must be signed in to change notification settings - Fork 28
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
NumberInput: add option to allow only positive integers #206
Closed
jrcastro2
wants to merge
1
commit into
inveniosoftware:master
from
jrcastro2:number-input-add-integers
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,57 @@ | ||
import React from "react"; | ||
import Input from "./Input"; | ||
import PropTypes from "prop-types"; | ||
|
||
const NumberInput = (props) => { | ||
const { | ||
fieldPath, | ||
label, | ||
placeholder, | ||
description, | ||
icon, | ||
required, | ||
disabled, | ||
type, | ||
positiveIntegerOnly, | ||
} = props; | ||
const onKeyPress = (event) => { | ||
if (!/[0-9]/.test(event.key)) { | ||
event.preventDefault(); | ||
} | ||
}; | ||
return ( | ||
<Input | ||
fieldPath={fieldPath} | ||
label={label} | ||
placeholder={placeholder} | ||
description={description} | ||
icon={icon} | ||
required={required} | ||
disabled={disabled} | ||
type={type} | ||
onKeyPress={positiveIntegerOnly && onKeyPress} | ||
/> | ||
); | ||
}; | ||
|
||
const NumberInput = (props) => <Input {...props} type="number" />; | ||
export default NumberInput; | ||
|
||
NumberInput.propTypes = { | ||
fieldPath: PropTypes.string.isRequired, | ||
label: PropTypes.string.isRequired, | ||
placeholder: PropTypes.string.isRequired, | ||
description: PropTypes.string.isRequired, | ||
icon: PropTypes.string, | ||
required: PropTypes.bool, | ||
disabled: PropTypes.bool, | ||
type: PropTypes.string, | ||
positiveIntegerOnly: PropTypes.bool, | ||
}; | ||
|
||
NumberInput.defaultProps = { | ||
icon: undefined, | ||
required: false, | ||
disabled: false, | ||
type: "number", | ||
positiveIntegerOnly: false, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is bypassing validation (backend or frontend) in an unorthodox way, without providing any feedback to the user on incorrect input. I think it might be confusing for the user
why not using the
min
html attribute ?<input type="number" min="0">
or
<input type="number" step="1" pattern="\d+">
this one should give feedback when you try to type-
this approach can be used without adding a new prop the html attributes should be automatically added by react to the output element
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand the first part, the user gets the same feedback as when he tries to type any letter, when he types it, it will not appear in the field.
I agree that we should align the validations between the backend and frontend but it's not correct at the moment either, as in the frontend it's allowed to introduce +- sign and when doing this it will not save the number (although it's displayed in the UI). Here an example of this:
Peek.2023-09-12.18-06.mp4
By doing this change it actually aligns a bit more with the backend validation.
I agree that ideally we also want to remove the + and - sign from the backend validation, but I also think we could tackle that in the future.
I also tried your suggestions without any luck, maybe I am doing something wrong: