forked from facebook/fbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.react.js
361 lines (344 loc) · 12.1 KB
/
Example.react.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
*
* @format
* @noflow
* @fbt {"project": "fbt-demo-project"}
* @emails oncall+i18n_fbt_js
*/
/* eslint-disable fb-www/flow-exact-by-default-object-types */
import './css/Example.css';
import classNames from 'classnames';
import fbt, {GenderConst, IntlVariations, init} from 'fbt';
import * as React from 'react';
// eslint-disable-next-line fb-www/no-module-aliasing
const ExampleEnum = require('./Example$FbtEnum');
const viewerContext = {
GENDER: IntlVariations.GENDER_UNKNOWN,
locale: 'en_US',
};
init({
translations: require('../translatedFbts.json'),
hooks: {
getViewerContext: () => viewerContext,
},
});
const LOCALES = Object.freeze({
en_US: Object.freeze({
bcp47: 'en-US',
displayName: 'English (US)\u200e',
englishName: 'English (US)',
rtl: false,
}),
fb_HX: Object.freeze({
bcp47: 'fb-HX',
displayName: 'l33t 5p34k',
englishName: 'FB H4x0r',
rtl: false,
}),
es_LA: Object.freeze({
bcp47: 'es-419',
displayName: 'Espa\u00F1ol',
englishName: 'Spanish',
rtl: false,
}),
ar_AR: Object.freeze({
bcp47: 'ar',
displayName: '\u0627\u0644\u0639\u0631\u0628\u064A\u0629',
englishName: 'Arabic',
rtl: true,
}),
he_IL: Object.freeze({
bcp47: 'he',
displayName: '\u05E2\u05D1\u05E8\u05D9\u05EA',
englishName: 'Hebrew',
rtl: true,
}),
ja_JP: Object.freeze({
bcp47: 'ja',
displayName: '\u65E5\u672C\u8A9E',
englishName: 'Japanese',
rtl: false,
}),
ru_RU: Object.freeze({
bcp47: 'ru',
displayName: 'Русский',
englishName: 'Russian',
rtl: false,
}),
});
type Locale = $Keys<typeof LOCALES>;
type Variation = $Values<typeof IntlVariations>;
type SharedObj = $Keys<typeof ExampleEnum>;
type PronounGender = $Keys<typeof GenderConst>;
type Props = $ReadOnly<{||}>;
type State = {|
locale: Locale,
vcGender: Variation,
ex1Name: string,
ex1Gender: Variation,
ex1Count: int,
ex2Name: string,
ex2Object: SharedObj,
ex2Pronoun: PronounGender,
|};
export default class Example extends React.Component<Props, State> {
state = {
locale: 'en_US',
ex1Name: 'Someone',
ex1Gender: IntlVariations.GENDER_UNKNOWN,
ex1Count: 1,
ex2Name: 'Someone',
ex2Object: 'LINK',
ex2Pronoun: GenderConst.UNKNOWN_SINGULAR,
};
setLocale(locale: Locale) {
viewerContext.locale = locale;
this.setState({locale});
const html = document.getElementsByTagName('html')[0];
if (html != null) {
html.lang = LOCALES[locale].bcp47;
}
document.body.className = LOCALES[locale].rtl ? 'rtl' : 'ltr';
}
onSubmit(event: SyntheticInputEvent<>) {
event.stopPropagation();
event.preventDefault();
}
render() {
const {locale} = this.state;
return (
<div>
<div className="example">
<div className="warning">
<fbt desc="title">Your FBT Demo</fbt>
</div>
<h1>
<fbt desc="header">Construct sentences</fbt>
</h1>
<h2>
{/* For fbt common strings, the description will be sourced from an external manifest.
See `--fbt-common-path` option from `fbt-collect` and common_strings.json */}
<fbt common={true}>Use the form below to see FBT in action.</fbt>
</h2>
<form action="" method="get" onSubmit={this.onSubmit}>
<fieldset>
<span className="example_row">
<span className="example_input--30">
<select
className="neatoSelect"
onChange={(event: SyntheticUIEvent<>) => {
const vcGender = parseInt(event.target.value, 10);
viewerContext.GENDER = vcGender;
this.forceUpdate();
}}>
<option value={IntlVariations.GENDER_UNKNOWN}>
<fbt desc="Gender Select label">Your Gender:</fbt>
</option>
<option value={IntlVariations.GENDER_UNKNOWN}>
<fbt desc="Unknown gender">Unknown</fbt>
</option>
<option value={IntlVariations.GENDER_MALE}>
<fbt desc="Male gender">Male</fbt>
</option>
<option value={IntlVariations.GENDER_FEMALE}>
<fbt desc="Female gender">Female</fbt>
</option>
</select>
</span>
</span>
</fieldset>
<fieldset>
<span className={classNames('example_row', 'example_row--multi')}>
<span
className={classNames('example_input', 'example_input--40')}>
<input
name="name"
onChange={(event: SyntheticUIEvent<>) => {
this.setState({ex1Name: event.target.value});
}}
placeholder={fbt('name', 'name field')}
type="text"
/>
</span>
<span
className={classNames('example_input', 'example_input--30')}>
<input
name="count"
onChange={(event: SyntheticUIEvent<>) => {
const val = parseInt(event.target.value, 10);
this.setState({ex1Count: isNaN(val) ? 1 : val});
}}
placeholder={fbt('count', 'count field')}
type="number"
/>
</span>
<span className="example_row">
<select
className="neatoSelect"
onChange={(event: SyntheticUIEvent<>) => {
this.setState({
ex1Gender: parseInt(event.target.value, 10),
});
}}>
<option value={IntlVariations.GENDER_UNKNOWN}>
<fbt desc="Gender Select label">Gender:</fbt>
</option>
<option value={IntlVariations.GENDER_UNKNOWN}>
<fbt desc="Unknown gender">Unknown</fbt>
</option>
<option value={IntlVariations.GENDER_MALE}>
<fbt desc="Male gender">Male</fbt>
</option>
<option value={IntlVariations.GENDER_FEMALE}>
<fbt desc="Female gender">Female</fbt>
</option>
</select>
</span>
</span>
</fieldset>
<fieldset>
<span className="sentence example_row">
<fbt desc="example 1">
<fbt:param gender={this.state.ex1Gender} name="name">
<b className="padRight">{this.state.ex1Name}</b>
</fbt:param>
has shared
<a className="neatoLink" href="#">
<fbt:plural
count={this.state.ex1Count}
many="photos"
showCount="ifMany">
a photo
</fbt:plural>
</a>
with you
</fbt>
</span>
</fieldset>
<fieldset>
<span className={classNames('example_row', 'example_row--multi')}>
<span
className={classNames('example_input', 'example_input--40')}>
<input
name="ex2Name"
onChange={(event: SyntheticUIEvent<>) => {
this.setState({ex2Name: event.target.value});
}}
placeholder={fbt('name', 'name field')}
type="text"
/>
</span>
<span
className={classNames('example_input', 'example_input--20')}>
<select
className="neatoSelect"
onChange={(event: SyntheticUIEvent<>) => {
this.setState({ex2Object: event.target.value});
}}>
{Object.keys(ExampleEnum).map(k => (
<option key={k} value={k}>
{ExampleEnum[k]}
</option>
))}
</select>
</span>
<span
className={classNames('example_row', 'example_input--20')}>
<select
className="neatoSelect"
onChange={(event: SyntheticUIEvent<>) => {
this.setState({
ex2Pronoun: parseInt(event.target.value, 10),
});
}}>
<option value={GenderConst.UNKNOWN_PLURAL}>
<fbt desc="Gender Select label">Gender:</fbt>
</option>
<option value={GenderConst.NOT_A_PERSON}>
<fbt desc="Gender Select label">Not a person</fbt>
</option>
<option value={GenderConst.UNKNOWN_PLURAL}>
<fbt desc="Gender Select label">Unknown (Plural)</fbt>
</option>
<option value={GenderConst.UNKNOWN_SINGULAR}>
<fbt desc="Gender Select label">Unknown (singular)</fbt>
</option>
<option value={GenderConst.MALE_SINGULAR}>
<fbt desc="Gender Select label">Male (singular)</fbt>
</option>
<option value={GenderConst.FEMALE_SINGULAR}>
<fbt desc="Gender Select label">Female (singular)</fbt>
</option>
</select>
</span>
</span>
</fieldset>
<fieldset>
<span className="sentence example_row">
<fbt desc="Example enum & pronoun">
<fbt:param name="name">
<b className="padRight">
<a href="#">{this.state.ex2Name}</a>
</b>
</fbt:param>
has a
<fbt:enum
enum-range={ExampleEnum}
value={this.state.ex2Object}
/>
to share!{' '}
<b className="pad">
<a href="#">View</a>
</b>{' '}
<fbt:pronoun
gender={this.state.ex2Pronoun}
human={false}
type="possessive"
/>{' '}
<fbt:enum
enum-range={ExampleEnum}
value={this.state.ex2Object}
/>.
</fbt>
</span>
</fieldset>
<fieldset>
<span className="example_row">
<button
className="bottom"
onClick={e => {
window.open('https://github.com/facebook/fbt', '_blank');
}}
type="submit">
{fbt('Try it out!', 'Sign up button')}
</button>
</span>
</fieldset>
</form>
</div>
<ul className="languages">
{Object.keys(LOCALES).map(loc => (
<li key={loc} value={loc}>
{locale === loc ? (
LOCALES[loc].displayName
) : (
<a
href={`#${loc}`}
onClick={(event: SyntheticUIEvent<>) => {
event.preventDefault();
this.setLocale(loc);
}}>
{LOCALES[loc].displayName}
</a>
)}
</li>
))}
</ul>
<p className="copyright">{`Facebook \u00A9 2018`}</p>
</div>
);
}
}