-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathXComponents.tsx
More file actions
123 lines (113 loc) · 3.78 KB
/
XComponents.tsx
File metadata and controls
123 lines (113 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import React, { useState } from 'react';
import {
XComponentMapProvider,
createXComponent,
Button,
} from '@deephaven/components';
import SampleSection from './SampleSection';
type FooComponentProps = { value: string };
function FooComponent({ value }: FooComponentProps) {
return (
<Button kind="primary" onClick={() => undefined}>
{value}
</Button>
);
}
FooComponent.displayName = 'FooComponent';
// Create an XComponent from FooComponent to allow for replacement
const XFooComponent = createXComponent(FooComponent);
function NestedFooComponent({ value }: FooComponentProps) {
// We're using the XComponent version so this panel can be replaced if it is mapped from a parent context to a replacement
return <XFooComponent value={`${value}.${value}`} />;
}
function MultiFooComponent({ value }: FooComponentProps) {
// Show multiple instances getting replaced
return (
<div>
<XFooComponent value={value} />
<XFooComponent value={value} />
</div>
);
}
// What we're replacing the XFooComponent with.
function ReverseFooComponent({ value }: FooComponentProps) {
return (
<Button kind="danger" onClick={() => undefined}>
{value.split('').reverse().join('')}
</Button>
);
}
/**
* Some examples showing usage of XComponents.
*/
export function XComponents(): JSX.Element {
const [value, setValue] = useState('hello');
return (
<SampleSection name="xcomponents">
<h2 className="ui-title">XComponents</h2>
<p>
XComponents are a way to replace a component with another component
without needing to pass props all the way down the component tree. This
can be useful in cases where we have a component deep down in the
component tree that we want to replace with a different component, but
don't want to have to provide props at the top level just to hook
into that.
<br />
Below is a component that is simply a button displaying the text
inputted in the input field. We will replace this component with a new
component that reverses the text, straight up, then in a nested
scenario, and then multiple instances.
</p>
<div className="form-group">
<label htmlFor="xcomponentsInput">
Input Value:
<input
type="text"
className="form-control"
id="xcomponentsInput"
value={value}
onChange={e => setValue(e.target.value)}
/>
</label>
</div>
<div className="row">
<div className="col">
<small>Original Component</small>
<div>
<XFooComponent value={value} />
</div>
<small>Replaced with Reverse</small>
<div>
<XComponentMapProvider
value={new Map([[XFooComponent, ReverseFooComponent]])}
>
<XFooComponent value={value} />
</XComponentMapProvider>
</div>
</div>
<div className="col">
<small>Nested component replaced</small>
<div>
<XComponentMapProvider
value={new Map([[XFooComponent, ReverseFooComponent]])}
>
{/* The `FooComponent` that gets replaced is from within the `NestedFooComponent` */}
<NestedFooComponent value={value} />
</XComponentMapProvider>
</div>
</div>
<div className="col">
<small>Multiple Components replaced</small>
<div>
<XComponentMapProvider
value={new Map([[XFooComponent, ReverseFooComponent]])}
>
<MultiFooComponent value={value} />
</XComponentMapProvider>
</div>
</div>
</div>
</SampleSection>
);
}
export default XComponents;