-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Remove loose check on non-number controlled inputs. Fix trailing dot issue. #9584
Merged
aweary
merged 3 commits into
facebook:master
from
nhunzaker:nh-controlled-text-input-numbers
May 3, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
62 changes: 62 additions & 0 deletions
62
fixtures/dom/src/components/fixtures/text-inputs/InputTestCase.js
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const React = window.React; | ||
|
||
import Fixture from '../../Fixture'; | ||
|
||
class InputTestCase extends React.Component { | ||
static defaultProps = { | ||
type: 'text', | ||
defaultValue: '', | ||
parseAs: 'text' | ||
} | ||
|
||
constructor () { | ||
super(...arguments); | ||
|
||
this.state = { | ||
value: this.props.defaultValue | ||
}; | ||
} | ||
|
||
onChange = (event) => { | ||
const raw = event.target.value; | ||
|
||
switch (this.props.type) { | ||
case 'number': | ||
const parsed = parseFloat(event.target.value, 10); | ||
|
||
this.setState({ value: isNaN(parsed) ? '' : parsed }); | ||
|
||
break; | ||
default: | ||
this.setState({ value: raw }); | ||
} | ||
} | ||
|
||
render() { | ||
const { children, type, defaultValue } = this.props; | ||
const { value } = this.state; | ||
|
||
return ( | ||
<Fixture> | ||
<div>{children}</div> | ||
|
||
<div className="control-box"> | ||
<fieldset> | ||
<legend>Controlled {type}</legend> | ||
<input type={type} value={value} onChange={this.onChange} /> | ||
<p className="hint"> | ||
Value: {JSON.stringify(this.state.value)} | ||
</p> | ||
</fieldset> | ||
|
||
<fieldset> | ||
<legend>Uncontrolled {type}</legend> | ||
<input type={type} defaultValue={defaultValue} /> | ||
</fieldset> | ||
</div> | ||
</Fixture> | ||
); | ||
} | ||
} | ||
|
||
export default InputTestCase; |
104 changes: 53 additions & 51 deletions
104
fixtures/dom/src/components/fixtures/text-inputs/index.js
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,62 +1,64 @@ | ||
const React = window.React; | ||
|
||
class TextInputFixtures extends React.Component { | ||
state = { | ||
color: '#ffaaee', | ||
}; | ||
import Fixture from '../../Fixture'; | ||
import FixtureSet from '../../FixtureSet'; | ||
import TestCase from '../../TestCase'; | ||
import InputTestCase from './InputTestCase'; | ||
|
||
renderControlled = (type) => { | ||
let id = `controlled_${type}`; | ||
class TextInputFixtures extends React.Component { | ||
render() { | ||
return ( | ||
<FixtureSet | ||
title="Inputs" | ||
description="Common behavior across controled and uncontrolled inputs" | ||
> | ||
<TestCase title="Numbers in a controlled text field with no handler"> | ||
<TestCase.Steps> | ||
<li>Move the cursor to after "2" in the text field</li> | ||
<li>Type ".2" into the text input</li> | ||
</TestCase.Steps> | ||
|
||
let onChange = e => { | ||
let value = e.target.value; | ||
if (type === 'number') { | ||
value = value === '' ? '' : parseFloat(value, 10) || 0; | ||
} | ||
this.setState({ | ||
[type] : value, | ||
}); | ||
}; | ||
<TestCase.ExpectedResult> | ||
The text field's value should not update. | ||
</TestCase.ExpectedResult> | ||
|
||
let state = this.state[type] || ''; | ||
<Fixture> | ||
<div className="control-box"> | ||
<fieldset> | ||
<legend>Value as number</legend> | ||
<input value={2} onChange={() => {}} /> | ||
</fieldset> | ||
|
||
return ( | ||
<div key={type} className="field"> | ||
<label className="control-label" htmlFor={id}>{type}</label> | ||
<input id={id} type={type} value={state} onChange={onChange} /> | ||
→ {JSON.stringify(state)} | ||
</div> | ||
); | ||
} | ||
<fieldset> | ||
<legend>Value as string</legend> | ||
<input value={"2"} onChange={() => {}} /> | ||
</fieldset> | ||
</div> | ||
</Fixture> | ||
|
||
renderUncontrolled = (type) => { | ||
let id = `uncontrolled_${type}`; | ||
return ( | ||
<div key={type} className="field"> | ||
<label className="control-label" htmlFor={id}>{type}</label> | ||
<input id={id} type={type} /> | ||
</div> | ||
); | ||
} | ||
<p className="footnote"> | ||
This issue was first introduced when we added extra logic | ||
to number inputs to prevent unexpected behavior in Chrome | ||
and Safari (see the number input test case). | ||
</p> | ||
</TestCase> | ||
|
||
render() { | ||
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input | ||
let types = [ | ||
'text', 'email', 'number', 'url', 'tel', | ||
'color', 'date', 'datetime-local', | ||
'time', 'month', 'week', 'range', 'password', | ||
]; | ||
return ( | ||
<form onSubmit={event => event.preventDefault()}> | ||
<fieldset> | ||
<legend>Controlled</legend> | ||
{types.map(this.renderControlled)} | ||
</fieldset> | ||
<fieldset> | ||
<legend>Uncontrolled</legend> | ||
{types.map(this.renderUncontrolled)} | ||
</fieldset> | ||
</form> | ||
<TestCase title="All inputs" description="General test of all inputs"> | ||
<InputTestCase type="text" defaultValue="Text" /> | ||
<InputTestCase type="email" defaultValue="[email protected]"/> | ||
<InputTestCase type="number" defaultValue={0} /> | ||
<InputTestCase type="url" defaultValue="http://example.com"/> | ||
<InputTestCase type="tel" defaultValue="555-555-5555"/> | ||
<InputTestCase type="color" defaultValue="#ff0000" /> | ||
<InputTestCase type="date" defaultValue="2017-01-01" /> | ||
<InputTestCase type="datetime-local" defaultValue="2017-01-01T01:00" /> | ||
<InputTestCase type="time" defaultValue="01:00" /> | ||
<InputTestCase type="month" defaultValue="2017-01" /> | ||
<InputTestCase type="week" defaultValue="2017-W01" /> | ||
<InputTestCase type="range" defaultValue={0.5} /> | ||
<InputTestCase type="password" defaultValue="" /> | ||
</TestCase> | ||
</FixtureSet> | ||
); | ||
} | ||
} | ||
|
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
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
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.
Might change to
{`The text field's value should not update.`}
to make syntax highlighters happy.