-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(Ref): restore docs for a component (#4039)
* docs(Ref): restore docs for a component * fix description * add a UT
- Loading branch information
1 parent
bf87dd2
commit b96e411
Showing
9 changed files
with
287 additions
and
20 deletions.
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react' | ||
import { Grid, Table, Ref, Segment } from 'semantic-ui-react' | ||
|
||
function RefExampleRef() { | ||
const objectRef = React.useRef(null) | ||
const [functionalRef, setFunctionalRef] = React.useState(null) | ||
const [isMounted, setIsMounted] = React.useState(false) | ||
|
||
React.useEffect(() => { | ||
setIsMounted(true) | ||
return () => setIsMounted(false) | ||
}, []) | ||
|
||
return ( | ||
<Grid> | ||
<Grid.Column width={6}> | ||
<Segment.Group> | ||
<Ref innerRef={setFunctionalRef}> | ||
<Segment>An example node with functional ref</Segment> | ||
</Ref> | ||
<Ref innerRef={objectRef}> | ||
<Segment> | ||
An example node with ref via <code>React.useRef()</code> | ||
</Segment> | ||
</Ref> | ||
</Segment.Group> | ||
</Grid.Column> | ||
<Grid.Column width={10}> | ||
{isMounted && ( | ||
<Table> | ||
<Table.Header> | ||
<Table.Row> | ||
<Table.HeaderCell>Type</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
<code>nodeName</code> | ||
</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
<code>textContent</code> | ||
</Table.HeaderCell> | ||
</Table.Row> | ||
</Table.Header> | ||
|
||
<Table.Body> | ||
<Table.Row> | ||
<Table.Cell singleLine> | ||
Functional ref via <code>React.useState()</code> hook | ||
</Table.Cell> | ||
<Table.Cell>{functionalRef.nodeName}</Table.Cell> | ||
<Table.Cell>{functionalRef.textContent}</Table.Cell> | ||
</Table.Row> | ||
|
||
<Table.Row> | ||
<Table.Cell singleLine> | ||
From <code>React.useRef()</code> hook | ||
</Table.Cell> | ||
<Table.Cell>{objectRef.current.nodeName}</Table.Cell> | ||
<Table.Cell>{objectRef.current.textContent}</Table.Cell> | ||
</Table.Row> | ||
</Table.Body> | ||
</Table> | ||
)} | ||
</Grid.Column> | ||
</Grid> | ||
) | ||
} | ||
|
||
export default RefExampleRef |
54 changes: 54 additions & 0 deletions
54
docs/src/examples/addons/Ref/Types/RefForwardingExample.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,54 @@ | ||
import React from 'react' | ||
import { Grid, Ref, Segment } from 'semantic-ui-react' | ||
|
||
const ExampleButton = React.forwardRef((props, ref) => ( | ||
<div> | ||
<button {...props} ref={ref} /> | ||
</div> | ||
)) | ||
|
||
function RefForwardingExample() { | ||
const forwardedRef = React.useRef(null) | ||
const [isMounted, setIsMounted] = React.useState(false) | ||
|
||
React.useEffect(() => { | ||
setIsMounted(true) | ||
return () => setIsMounted(false) | ||
}, []) | ||
|
||
return ( | ||
<Grid columns={2}> | ||
<Grid.Column> | ||
<Segment> | ||
<p> | ||
A button below uses <code>React.forwardRef()</code> API. | ||
</p> | ||
|
||
<Ref innerRef={forwardedRef}> | ||
<ExampleButton>A button</ExampleButton> | ||
</Ref> | ||
</Segment> | ||
</Grid.Column> | ||
|
||
<Grid.Column> | ||
{isMounted && ( | ||
<Segment secondary> | ||
<pre> | ||
{JSON.stringify( | ||
{ | ||
nodeName: forwardedRef.current.nodeName, | ||
nodeType: forwardedRef.current.nodeType, | ||
textContent: forwardedRef.current.textContent, | ||
}, | ||
null, | ||
2, | ||
)} | ||
</pre> | ||
</Segment> | ||
)} | ||
</Grid.Column> | ||
</Grid> | ||
) | ||
} | ||
|
||
export default RefForwardingExample |
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,30 @@ | ||
import React from 'react' | ||
|
||
import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection' | ||
|
||
const RefTypesExamples = () => ( | ||
<ExampleSection title='Types'> | ||
<ComponentExample | ||
title='Ref' | ||
description={ | ||
<span> | ||
A component exposes the <code>innerRef</code> prop that always returns | ||
the DOM node of both functional and class component children. | ||
</span> | ||
} | ||
examplePath='addons/Ref/Types/RefExampleRef' | ||
/> | ||
<ComponentExample | ||
title='Forward Ref' | ||
description={ | ||
<span> | ||
<code>React.forwardRef()</code> API is also handled by this component. | ||
</span> | ||
} | ||
examplePath='addons/Ref/Types/RefForwardingExample' | ||
/> | ||
</ExampleSection> | ||
) | ||
|
||
export default RefTypesExamples |
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,28 @@ | ||
import React from 'react' | ||
import { Message } from 'semantic-ui-react' | ||
|
||
import Types from './Types' | ||
|
||
const RefExamples = () => ( | ||
<> | ||
<Message info> | ||
<p> | ||
Currently, it's recommended to use <code>Ref</code> component to get | ||
refs to HTML elements from Semantic UI React components as not all | ||
components support native ref forwarding via{' '} | ||
<code>React.forwardRef()</code>. | ||
</p> | ||
<p> | ||
As it uses deprecated <code>ReactDOM.findDOMNode()</code> you may | ||
receive warnings in React's StrictMode. We are working on it in{' '} | ||
<a href='https://github.com/Semantic-Org/Semantic-UI-React/issues/3819'> | ||
Semantic-Org/Semantic-UI-React#3819 | ||
</a> | ||
. | ||
</p> | ||
</Message> | ||
<Types /> | ||
</> | ||
) | ||
|
||
export default RefExamples |
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,17 @@ | ||
import _ from 'lodash' | ||
import componentMenu from '../../src/componentMenu' | ||
|
||
const getComponentMenu = () => componentMenu | ||
const getComponentMenu = () => | ||
_.sortBy( | ||
[ | ||
...componentMenu, | ||
{ | ||
displayName: 'Ref', | ||
type: 'addon', | ||
external: true, | ||
}, | ||
], | ||
'displayName', | ||
) | ||
|
||
export default getComponentMenu |
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,19 @@ | ||
import React from 'react' | ||
import createReferenceProxy from 'src/modules/Popup/lib/createReferenceProxy' | ||
|
||
describe('createReferenceProxy', () => { | ||
it('handles nodes', () => { | ||
const node = document.createElement('div') | ||
const proxy = createReferenceProxy(node) | ||
|
||
expect(proxy.getBoundingClientRect()).to.include({ height: 0, width: 0 }) | ||
}) | ||
|
||
it('handles ref objects', () => { | ||
const ref = React.createRef() | ||
const proxy = createReferenceProxy(ref) | ||
|
||
ref.current = document.createElement('div') | ||
expect(proxy.getBoundingClientRect()).to.include({ height: 0, width: 0 }) | ||
}) | ||
}) |