-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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
[Input] should accommodate number and string values #7791
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74cd782
[Input] implement controlled tests for both string and numeric values
rosskevin 91e4316
[Input] isControlled should accomodate number | string values as indi…
rosskevin 587169a
extensive testing of isDirty and hasValue to support both strings and…
rosskevin 19d4af8
[Input] ensure a value of `0` will mark the field as dirty as it’s a …
rosskevin 055eb8c
Refactor Input hasValue/isDirty using logic from master
rosskevin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,44 @@ import classNames from 'classnames'; | |
import withStyles from '../styles/withStyles'; | ||
import Textarea from './Textarea'; | ||
|
||
export const isString = (value: ?(string | number)) => typeof value === 'string'; | ||
export const isNumber = (value: ?(string | number)) => !isNaN(parseFloat(value)); | ||
|
||
/** | ||
* @param value | ||
* @returns {boolean} true if string or number (including zero) | ||
*/ | ||
export function hasValue(value: ?(number | string)) { | ||
if (isString(value)) { | ||
return true; | ||
} else if (isNumber(value)) { | ||
return true; | ||
} | ||
return !!value; | ||
} | ||
|
||
/** | ||
* Determine if field is dirty (a.k.a. filled). | ||
* | ||
* Response determines if label is presented above field or as placeholder. | ||
* | ||
* @param obj | ||
* @param SSR | ||
* @returns {boolean} False when not present or empty string. | ||
* True when number or string with length. | ||
*/ | ||
export function isDirty(obj, SSR = false) { | ||
if (!obj) { | ||
return false; | ||
} | ||
|
||
if (isNumber(obj.value) || (SSR && isNumber(obj.defaultValue))) { | ||
return true; | ||
} | ||
|
||
return ( | ||
obj && | ||
((obj.value && obj.value.toString().length) || | ||
(SSR && obj.defaultValue && obj.defaultValue.toString().length)) > 0 | ||
(hasValue(obj.value) && obj.value.toString().length > 0) || | ||
(SSR && hasValue(obj.defaultValue) && obj.defaultValue.toString().length > 0) | ||
); | ||
} | ||
|
||
|
@@ -369,8 +402,16 @@ class Input extends Component<DefaultProps, AllProps, State> { | |
} | ||
}; | ||
|
||
/** | ||
* A controlled input accepts its current value as a prop, as well as a callback | ||
* to change that value. | ||
* | ||
* @returns {boolean} | ||
* @see https://facebook.github.io/react/docs/forms.html#controlled-components | ||
*/ | ||
isControlled() { | ||
return typeof this.props.value === 'string'; | ||
const { onChange, value } = this.props; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onChange shouldn't be taken into account. React isn't using it for native components. |
||
return !!onChange && hasValue(value); | ||
} | ||
|
||
checkDirty(obj) { | ||
|
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
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.
The master branch was looking for invalid values instead of valid ones. Have you considered this option https://github.com/callemall/material-ui/blob/master/src/TextField/TextField.js#L110 ? Maybe we could make the implementation shorter/simpler.