forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brandon Dail
committed
Jan 30, 2018
1 parent
2c3c59e
commit dff3e52
Showing
10 changed files
with
189 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
const React = window.React; | ||
const ReactDOM = window.ReactDOM; | ||
|
||
class IframePortal extends React.Component { | ||
state = {ref: null}; | ||
|
||
handleRef = (ref) => { | ||
if (ref !== this.state.ref) { | ||
this.setState({ref}); | ||
if (ref && ref.contentDocument && this.props.head) { | ||
ref.contentDocument.head.innerHTML = this.props.head; | ||
} | ||
} | ||
}; | ||
|
||
render() { | ||
const {ref} = this.state; | ||
let portal = null; | ||
if (ref && ref.contentDocument) { | ||
portal = ReactDOM.createPortal( | ||
this.props.children, | ||
ref.contentDocument.body, | ||
); | ||
} | ||
|
||
return ( | ||
<div> | ||
<iframe | ||
style={{ border: 'none', height: this.props.height }} | ||
ref={this.handleRef} /> | ||
{portal} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
class IframeSubtree extends React.Component { | ||
warned = false; | ||
render() { | ||
if (!this.warned) { | ||
console.error(`IFrame has not yet been implemented for React v${React.version}`); | ||
this.warned = true; | ||
} | ||
return ( | ||
<div> | ||
{this.props.children} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
|
||
export default ReactDOM.createPortal ? IframePortal : IframeSubtree; |
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
36 changes: 36 additions & 0 deletions
36
fixtures/dom/src/components/fixtures/selection-events/DraftJsEditorTestCase.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,36 @@ | ||
import TestCase from '../../TestCase'; | ||
import Iframe from '../../Iframe'; | ||
const React = window.React; | ||
const {EditorState, Editor} = window.Draft; | ||
|
||
|
||
export default class DraftJsEditorTestCase extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {editorState: EditorState.createEmpty()}; | ||
this.onChange = (editorState) => this.setState({editorState}); | ||
} | ||
render() { | ||
return ( | ||
<TestCase | ||
title="Cursor Position in a Draft.js Editor" | ||
description="Draft.js is a rich text editor system for React. | ||
This verifies that the selection restoration functionality it depends on | ||
works in an iframe."> | ||
<TestCase.Steps> | ||
<li>Enter some text into the Draft.js editor (grey outlined box)</li> | ||
<li>Change your cursor position to somewhere in the middle of the text</li> | ||
<li>Enter a new character</li> | ||
</TestCase.Steps> | ||
<TestCase.ExpectedResult> | ||
The cursor should not jump positions | ||
</TestCase.ExpectedResult> | ||
<Iframe height={60}> | ||
<div style={{ border: '1px solid grey' }}> | ||
<Editor editorState={this.state.editorState} onChange={this.onChange} /> | ||
</div> | ||
</Iframe> | ||
</TestCase> | ||
); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
fixtures/dom/src/components/fixtures/selection-events/ReorderedInputsTestCase.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,49 @@ | ||
import TestCase from '../../TestCase'; | ||
import Iframe from '../../Iframe'; | ||
const React = window.React; | ||
|
||
export default class ReorderedInputsTestCase extends React.Component { | ||
|
||
state = { count: 0 }; | ||
|
||
componentDidMount() { | ||
this.interval = setInterval(() => { | ||
this.setState({ count: this.state.count + 1 }); | ||
}, 2000); | ||
} | ||
|
||
componentWillUnmount() { | ||
clearInterval(this.interval); | ||
} | ||
|
||
renderInputs() { | ||
const inputs = [ | ||
<input key={1} defaultValue="Foo" />, | ||
<input key={2} defaultValue="Bar" />, | ||
]; | ||
if (this.state.count % 2 === 0) { | ||
inputs.reverse(); | ||
} | ||
return inputs; | ||
} | ||
|
||
render() { | ||
return ( | ||
<TestCase | ||
title="Reordered input elements in iframes" | ||
description=""> | ||
<TestCase.Steps> | ||
<li>The two inputs below swap positions every two seconds</li> | ||
<li>Select the text in either of them</li> | ||
<li>Wait for the swap to occur</li> | ||
</TestCase.Steps> | ||
<TestCase.ExpectedResult> | ||
The selection you made should be maintained | ||
</TestCase.ExpectedResult> | ||
<Iframe height={50}> | ||
{this.renderInputs()} | ||
</Iframe> | ||
</TestCase> | ||
) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
fixtures/dom/src/components/fixtures/selection-events/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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import FixtureSet from '../../FixtureSet'; | ||
import TestCase from '../../TestCase'; | ||
import Iframe from '../../Iframe'; | ||
import ReorderedInputsTestCase from './ReorderedInputsTestCase'; | ||
import DraftJsEditorTestCase from './DraftJsEditorTestCase'; | ||
const React = window.React; | ||
|
||
|
||
export default function SelectionEvents() { | ||
return ( | ||
<FixtureSet | ||
title="Selection Restoration in iframes" | ||
description=" | ||
When React commits changes it may perform operations which cause existing | ||
selection state to be lost. This is manually managed by reading the | ||
selection state before commits and then restoring it afterwards. | ||
This selection restoration process should work for elements rendered in | ||
iframes. | ||
"> | ||
<ReorderedInputsTestCase /> | ||
<DraftJsEditorTestCase /> | ||
</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