-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDynamicActionContextDemo.tsx
More file actions
130 lines (120 loc) · 3.78 KB
/
Copy pathDynamicActionContextDemo.tsx
File metadata and controls
130 lines (120 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
124
125
126
127
128
129
130
import { Component, createMemo, createSignal, createUniqueId, For, Show } from 'solid-js';
import { KbdShortcut, createSyncActionsContext } from '../../../../../lib';
import { ownContactId, contacts, contactActionId } from './data';
import { InputEventHandler, ContactItemProps, ReceiverContactDetailsProps } from './types';
import demoStyles from '../demoUtils.module.css';
import utilStyles from '../../../../utils.module.css';
import styles from './DynamicActionContextDemo.module.css';
const ContactItem: Component<ContactItemProps> = (p) => {
const inputId = createUniqueId();
return (
<li
class={styles.contactItem}
classList={{
[styles.active]: p.isActive,
}}
>
<label
class={styles.contactLabel}
htmlFor={inputId}
>
{p.contactData.label}
</label>
<input
id={inputId}
class={styles.contactInput}
type="radio"
name="contact-option"
checked={p.isActive}
value={p.contactId}
onInput={p.onInput}
/>
</li>
);
};
const ReceiverContactDetails: Component<ReceiverContactDetailsProps> = (p) => {
createSyncActionsContext(contactActionId, () => {
return {
receiverContactId: p.contactId(),
};
});
return (
<div>
<h2 class={utilStyles.stripSpace}>Receiver Details</h2>
<p>{p.contactData().details}</p>
</div>
);
};
export const DynamicActionContextDemo: Component = () => {
const [activeContactId, setActiveContactId] = createSignal(ownContactId);
const activeContactData = createMemo(() => {
const activeContactIdValue = activeContactId();
const activeContactData = contacts[activeContactIdValue];
return activeContactData;
});
const handleInput: InputEventHandler = (event) => {
const newValue = event.currentTarget.value;
setActiveContactId(newValue);
};
function renderOwnDetails() {
return (
<div>
<h2 class={utilStyles.stripSpace}>Personal Details</h2>
<p>{activeContactData().details}</p>
</div>
);
}
return (
<section class={demoStyles.demoSection}>
<div>
<h3>Dynamically set action context</h3>
<p>
When you select Andrew or Tobey, the <strong>ReceiverContactDetails</strong> is rendered.
</p>
<p>
While this component is rendered, the profile details are shared using dynamic action
context.
</p>
<p>
Try the <strong>Send Message to Contact</strong> action once after selecting each profile.
</p>
<p>
If you have Andrew's or Tobey's profile already opened, their contact id will already be
taken and you'll skip one step.
</p>
</div>
<div class={demoStyles.demoInteraction}>
<div class={styles.contactsWrapper}>
<aside>
<ul class={styles.contactList}>
<For each={Object.entries(contacts)}>
{([contactId, contact]) => (
<ContactItem
contactId={contactId}
isActive={contactId === activeContactId()}
contactData={contact}
onInput={handleInput}
/>
)}
</For>
</ul>
</aside>
<main class={styles.contactDetails}>
<Show
when={activeContactId() !== ownContactId}
fallback={renderOwnDetails()}
>
<ReceiverContactDetails
contactId={activeContactId}
contactData={activeContactData}
/>
</Show>
</main>
</div>
<p class={demoStyles.demoInteractionDesc}>
Try pressing <KbdShortcut shortcut="m" />
</p>
</div>
</section>
);
};