|
1 |
| -import RangeInputFixtures from './range-inputs'; |
2 |
| -import TextInputFixtures from './text-inputs'; |
3 |
| -import SelectFixtures from './selects'; |
4 |
| -import TextAreaFixtures from './textareas'; |
5 |
| -import InputChangeEvents from './input-change-events'; |
6 |
| -import NumberInputFixtures from './number-inputs'; |
7 |
| -import PasswordInputFixtures from './password-inputs'; |
8 |
| -import ButtonFixtures from './buttons'; |
9 |
| -import DateInputFixtures from './date-inputs'; |
10 |
| -import ErrorHandling from './error-handling'; |
11 |
| -import EventPooling from './event-pooling'; |
12 |
| -import CustomElementFixtures from './custom-elements'; |
13 |
| -import MediaEventsFixtures from './media-events'; |
14 |
| -import PointerEventsFixtures from './pointer-events'; |
15 |
| -import MouseEventsFixtures from './mouse-events'; |
16 |
| -import SelectionEventsFixtures from './selection-events'; |
17 |
| - |
18 | 1 | const React = window.React;
|
| 2 | +const fixturePath = window.location.pathname; |
19 | 3 |
|
20 | 4 | /**
|
21 | 5 | * A simple routing component that renders the appropriate
|
22 | 6 | * fixture based on the location pathname.
|
23 | 7 | */
|
24 |
| -function FixturesPage() { |
25 |
| - switch (window.location.pathname) { |
26 |
| - case '/text-inputs': |
27 |
| - return <TextInputFixtures />; |
28 |
| - case '/range-inputs': |
29 |
| - return <RangeInputFixtures />; |
30 |
| - case '/selects': |
31 |
| - return <SelectFixtures />; |
32 |
| - case '/textareas': |
33 |
| - return <TextAreaFixtures />; |
34 |
| - case '/input-change-events': |
35 |
| - return <InputChangeEvents />; |
36 |
| - case '/number-inputs': |
37 |
| - return <NumberInputFixtures />; |
38 |
| - case '/password-inputs': |
39 |
| - return <PasswordInputFixtures />; |
40 |
| - case '/buttons': |
41 |
| - return <ButtonFixtures />; |
42 |
| - case '/date-inputs': |
43 |
| - return <DateInputFixtures />; |
44 |
| - case '/error-handling': |
45 |
| - return <ErrorHandling />; |
46 |
| - case '/event-pooling': |
47 |
| - return <EventPooling />; |
48 |
| - case '/custom-elements': |
49 |
| - return <CustomElementFixtures />; |
50 |
| - case '/media-events': |
51 |
| - return <MediaEventsFixtures />; |
52 |
| - case '/pointer-events': |
53 |
| - return <PointerEventsFixtures />; |
54 |
| - case '/mouse-events': |
55 |
| - return <MouseEventsFixtures />; |
56 |
| - case '/selection-events': |
57 |
| - return <SelectionEventsFixtures />; |
58 |
| - default: |
59 |
| - return <p>Please select a test fixture.</p>; |
| 8 | +class FixturesPage extends React.Component { |
| 9 | + static defaultProps = { |
| 10 | + fixturePath: fixturePath === '/' ? '/home' : fixturePath, |
| 11 | + }; |
| 12 | + |
| 13 | + state = { |
| 14 | + isLoading: true, |
| 15 | + error: null, |
| 16 | + Fixture: null, |
| 17 | + }; |
| 18 | + |
| 19 | + componentDidMount() { |
| 20 | + this.loadFixture(); |
| 21 | + } |
| 22 | + |
| 23 | + async loadFixture() { |
| 24 | + const {fixturePath} = this.props; |
| 25 | + |
| 26 | + try { |
| 27 | + const module = await import(`.${fixturePath}`); |
| 28 | + |
| 29 | + this.setState({Fixture: module.default}); |
| 30 | + } catch (error) { |
| 31 | + console.error(error); |
| 32 | + this.setState({error}); |
| 33 | + } finally { |
| 34 | + this.setState({isLoading: false}); |
| 35 | + } |
60 | 36 | }
|
| 37 | + |
| 38 | + render() { |
| 39 | + const {Fixture, error, isLoading} = this.state; |
| 40 | + |
| 41 | + if (isLoading) { |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + if (error) { |
| 46 | + return <FixtureError error={error} />; |
| 47 | + } |
| 48 | + |
| 49 | + return <Fixture />; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function FixtureError({error}) { |
| 54 | + return ( |
| 55 | + <section> |
| 56 | + <h2>Error loading fixture</h2> |
| 57 | + <p>{error.message}</p> |
| 58 | + </section> |
| 59 | + ); |
61 | 60 | }
|
62 | 61 |
|
63 | 62 | export default FixturesPage;
|
0 commit comments