UNPKG

1.69 kBTypeScriptView Raw
1import { Component, ComponentType, ReactNode } from 'react';
2import PropTypes from 'prop-types';
3/**
4---
5category: utilities/react
6---
7Abstract component identifier. Helpful for picking out a specific child.
8
9 ```js
10 class App extends Component {
11 render () {
12 const title = pick(Title, this.props.children)
13 const content = pick(Content, this.props.children)
14
15 return (
16 <div>
17 {title}
18 <ContextView>
19 {content}
20 </ContextView>
21 </div>
22 )
23 }
24 }
25
26 class Title extends ComponentIdentifier { static displayName = 'Title' }
27 class Content extends ComponentIdentifier { static displayName = 'Content' }
28
29 ReactDOM.render(
30 <App>
31 <Title><h2>Hello World!</h2></Title>
32 <Content><div>This text gets decorated within `App`.</div></Content>
33 </App>,
34 document.getElementById('container')
35 )
36 ```
37 @module ComponentIdentifier
38**/
39declare class ComponentIdentifier<P extends {
40 children: ReactNode;
41}> extends Component<P> {
42 static propTypes: {
43 children: PropTypes.Requireable<PropTypes.ReactNodeLike>;
44 };
45 static defaultProps: {
46 children: null;
47 };
48 static pick: (component: ComponentType, children: ReactNode) => undefined;
49 render(): JSX.Element | null;
50}
51export default ComponentIdentifier;
52export {
53/**
54 *
55 * Pick a specific child component from a component's children
56 *
57 * @param {Component} component The component to look for
58 * @param {Array} children The child components to look through
59 * @return {Component} The matching component if found, otherwise undefined
60 */
61ComponentIdentifier };
62//# sourceMappingURL=ComponentIdentifier.d.ts.map
\No newline at end of file