|
1 | | -import React, { Component } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import { func, object, arrayOf } from 'prop-types'; |
3 | 3 | import createLinkedList from '@skidding/linked-list'; |
4 | 4 | import { createModuleType } from '../../utils/module-type'; |
5 | 5 | import { PropsProxy } from '../PropsProxy'; |
6 | 6 |
|
7 | | -export class Loader extends Component { |
8 | | - static propTypes = { |
9 | | - fixture: createModuleType(object).isRequired, |
10 | | - proxies: arrayOf(createModuleType(func)), |
11 | | - onComponentRef: func |
12 | | - }; |
13 | | - |
14 | | - static defaultProps = { |
15 | | - proxies: [] |
16 | | - }; |
17 | | - |
18 | | - /** |
19 | | - * Loader for rendering React components in isolation. |
20 | | - * |
21 | | - * Renders components using fixtures and Proxy middleware. Initialized via |
22 | | - * props. |
23 | | - */ |
24 | | - constructor(props) { |
25 | | - super(props); |
26 | | - |
27 | | - this.firstProxy = createProxyLinkedList(props.proxies); |
28 | | - } |
29 | | - |
30 | | - componentWillReceiveProps({ proxies }) { |
31 | | - if (proxies !== this.props.proxies) { |
32 | | - this.firstProxy = createProxyLinkedList(proxies); |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - render() { |
37 | | - const { firstProxy } = this; |
38 | | - const { fixture, onComponentRef, onFixtureUpdate } = this.props; |
39 | | - |
40 | | - return ( |
41 | | - <firstProxy.value |
42 | | - nextProxy={firstProxy.next()} |
43 | | - fixture={fixture} |
44 | | - onComponentRef={onComponentRef || noope} |
45 | | - onFixtureUpdate={onFixtureUpdate || noope} |
46 | | - /> |
47 | | - ); |
48 | | - } |
49 | | -} |
50 | | - |
51 | | -function createProxyLinkedList(userProxies) { |
52 | | - return createLinkedList([...userProxies, PropsProxy]); |
53 | | -} |
54 | | - |
55 | | -function noope() {} |
| 7 | +/** |
| 8 | + * Loader for rendering React components in isolation. |
| 9 | + * |
| 10 | + * Renders components using fixtures and Proxy middleware. Initialized via |
| 11 | + * props. |
| 12 | + */ |
| 13 | +const Loader = ({ fixture, proxies, onComponentRef, onFixtureUpdate }) => { |
| 14 | + const firstProxy = createLinkedList([...proxies, PropsProxy]); |
| 15 | + |
| 16 | + return ( |
| 17 | + <firstProxy.value |
| 18 | + nextProxy={firstProxy.next()} |
| 19 | + fixture={fixture} |
| 20 | + onComponentRef={onComponentRef} |
| 21 | + onFixtureUpdate={onFixtureUpdate} |
| 22 | + /> |
| 23 | + ); |
| 24 | +}; |
| 25 | + |
| 26 | +Loader.propTypes = { |
| 27 | + fixture: createModuleType(object).isRequired, |
| 28 | + proxies: arrayOf(createModuleType(func)), |
| 29 | + onFixtureUpdate: func, |
| 30 | + onComponentRef: func |
| 31 | +}; |
| 32 | + |
| 33 | +Loader.defaultProps = { |
| 34 | + proxies: [], |
| 35 | + onFixtureUpdate: () => {}, |
| 36 | + onComponentRef: () => {} |
| 37 | +}; |
| 38 | + |
| 39 | +export { Loader }; |
0 commit comments