-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.stories.js
210 lines (183 loc) · 4.69 KB
/
index.stories.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, {useMemo, useState, useRef} from 'react';
import uuid from 'uuid';
import MessageComposer from './index.js';
import './styles.scss';
const users = [
{
id: 'all',
displayName: 'All',
objectType: 'groupMention',
},
{
id: 'here',
displayName: 'Here',
objectType: 'groupMention',
},
{
id: 'moderators',
displayName: 'Moderators',
objectType: 'groupMention',
},
{
displayName: 'Philip Fry',
},
{
displayName: 'Turanga Leela',
},
{
displayName: 'Hubert Farnsworth',
},
{
displayName: 'Zapp Brannigan',
},
{
displayName: 'John Zoidberg',
},
{
displayName: 'Amy Wang',
},
{
displayName: 'Bender Rodriguez',
},
{
displayName: 'Hermes Conrad',
},
{
displayName: 'Kif Kroker',
},
{
displayName: 'Barbados Slim',
},
{
displayName: 'Bill McNeal',
},
];
const sanitizeUser = (user) => {
if (!user.id) {
// eslint-disable-next-line no-param-reassign
user.id = uuid.v4();
}
if (!user.objectType) {
// eslint-disable-next-line no-param-reassign
user.objectType = 'person';
}
};
for (const user of users) {
sanitizeUser(user);
if (user.items) {
for (const person of user.items) {
sanitizeUser(person);
}
}
}
// insert presence items
let items = [users[3], users[4], users[5]];
users[1].items = JSON.stringify(items);
// insert moderators
items = [users[5], users[6], users[7]];
users[2].items = JSON.stringify(items);
const spaces = [];
const setValue = (v, num) => {
spaces[num] = v;
};
export default {
component: MessageComposer,
title: 'MessageComposer',
};
const Example = () => {
const [message, setMessage] = useState('');
const [number, setNumber] = useState('1');
const [failSend, setFailSend] = useState(false);
const participantsRef = useRef();
const toggleFailSend = () => {
setFailSend(!failSend);
};
const show = (num) => {
setMessage('');
setNumber(num);
};
const other = number === '1' ? '2' : '1';
const draft = {
id: number,
value: spaces[number],
save: setValue,
};
const emitter = useRef();
const setEmitter = (e) => {
emitter.current = e;
};
const focus = (e) => {
e.preventDefault();
emitter.current.emit('FOCUS');
};
const insertText = (t) => (e) => {
e.preventDefault();
emitter.current.emit('INSERT_TEXT', t);
};
const send = (e) => {
e.preventDefault();
emitter.current.emit('SEND');
};
const clear = (e) => {
e.preventDefault();
emitter.current.emit('CLEAR');
};
const onSend = (val) => {
setMessage(val);
return !failSend;
};
const notifyKeyDown = (event) => {
// eslint-disable-next-line no-console
console.log('Key pressed', event);
};
const [disabled, setDisabled] = useState(false);
const toggleDisabled = () => {
setDisabled(!disabled);
};
const [isMarkdownDisabled, setMarkdownDisabled] = useState(false);
const toggleMarkdownDisabled = () => {
setMarkdownDisabled(!isMarkdownDisabled);
};
const [placeholder, setPlaceholder] = useState('Write your message in this space.');
const changePlaceholder = () => {
setPlaceholder('This is a new placeholder');
};
const markdown = useMemo(
() => ({
disabled: isMarkdownDisabled,
}),
[isMarkdownDisabled]
);
participantsRef.current = users;
const mentions = {participants: participantsRef};
return (
<div className="container">
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
<div className="content" onClick={focus} />
<div className="mc">
<MessageComposer
disabled={disabled}
draft={draft}
markdown={markdown}
mentions={mentions}
send={onSend}
notifyKeyDown={notifyKeyDown}
placeholder={placeholder}
setEmitter={setEmitter}
/>
<br />
<div>Sending: {JSON.stringify(message)}</div>
<button onClick={() => show(other)}>Show Space {other}</button>
<button onClick={insertText('🎉')}>Insert Emoji</button>
<button onClick={insertText('@')}>@Mention</button>
<button onClick={toggleDisabled}>{disabled ? 'enable' : 'disable'}</button>
<button onClick={toggleMarkdownDisabled}>{isMarkdownDisabled ? 'enable markdown' : 'disable markdown'}</button>
<button onClick={toggleFailSend}>{failSend ? 'send message successfully' : 'make send message fail'}</button>
<button onClick={send}>SEND</button>
<button onClick={clear}>CLEAR</button>
<button onClick={changePlaceholder}>Change placeholder</button>
</div>
</div>
);
};
export const Quill = () => Example();