Skip to content

Commit 68fd15d

Browse files
committed
examples: Add enumI18n example
1 parent 003498c commit 68fd15d

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
The MIT License
3+
4+
Copyright (c) 2017-2019 EclipseSource Munich
5+
https://github.com/eclipsesource/jsonforms
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
import { registerExamples } from '../register';
26+
import { Translator } from '@jsonforms/core';
27+
import get from 'lodash/get';
28+
29+
export const schema = {
30+
type: 'object',
31+
properties: {
32+
country: {
33+
type: 'string',
34+
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other'],
35+
},
36+
countryNoAutocomplete: {
37+
type: 'string',
38+
enum: ['DE', 'IT', 'JP', 'US', 'RU', 'Other'],
39+
},
40+
status: {
41+
type: 'string',
42+
oneOf: [
43+
{ const: 'pending', title: 'Pending' },
44+
{ const: 'approved', title: 'Approved' },
45+
{ const: 'rejected', title: 'Rejected' },
46+
],
47+
},
48+
},
49+
};
50+
51+
export const uischema = {
52+
type: 'VerticalLayout',
53+
elements: [
54+
{
55+
type: 'Group',
56+
label: 'Enum with i18n (Autocomplete)',
57+
elements: [
58+
{
59+
type: 'Control',
60+
scope: '#/properties/country',
61+
label: 'Country (with autocomplete)',
62+
},
63+
],
64+
},
65+
{
66+
type: 'Group',
67+
label: 'Enum with i18n (Dropdown)',
68+
elements: [
69+
{
70+
type: 'Control',
71+
scope: '#/properties/countryNoAutocomplete',
72+
label: 'Country (dropdown)',
73+
options: {
74+
autocomplete: false,
75+
},
76+
},
77+
],
78+
},
79+
{
80+
type: 'Group',
81+
label: 'OneOf Enum with i18n',
82+
elements: [
83+
{
84+
type: 'Control',
85+
scope: '#/properties/status',
86+
label: 'Status',
87+
},
88+
],
89+
},
90+
],
91+
};
92+
93+
export const data = {
94+
country: 'DE',
95+
};
96+
97+
export const translations: Record<string, string> = {
98+
// Translations for country enum values
99+
// Key format: <property>.<enumValue>
100+
'country.DE': 'Germany',
101+
'country.IT': 'Italy',
102+
'country.JP': 'Japan',
103+
'country.US': 'United States',
104+
'country.RU': 'Russia',
105+
'country.Other': 'Other Country',
106+
// Same translations for the non-autocomplete version
107+
'countryNoAutocomplete.DE': 'Germany',
108+
'countryNoAutocomplete.IT': 'Italy',
109+
'countryNoAutocomplete.JP': 'Japan',
110+
'countryNoAutocomplete.US': 'United States',
111+
'countryNoAutocomplete.RU': 'Russia',
112+
'countryNoAutocomplete.Other': 'Other Country',
113+
// Translations for status oneOf enum
114+
'status.pending': 'Awaiting Review',
115+
'status.approved': 'Approved',
116+
'status.rejected': 'Declined',
117+
};
118+
119+
export const translate: Translator = (
120+
key: string,
121+
defaultMessage: string | undefined
122+
) => {
123+
return get(translations, key) ?? defaultMessage;
124+
};
125+
126+
registerExamples([
127+
{
128+
name: 'enum-i18n',
129+
label: 'Enums (i18n)',
130+
data,
131+
schema,
132+
uischema,
133+
i18n: {
134+
translate: translate,
135+
locale: 'en',
136+
},
137+
},
138+
]);

packages/examples/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import * as onChange from './examples/onChange';
6767
import * as enumExample from './examples/enum';
6868
import * as radioGroupExample from './examples/radioGroup';
6969
import * as multiEnum from './examples/enum-multi';
70+
import * as enumI18n from './examples/enumI18n';
7071
import * as enumInArray from './examples/enumInArray';
7172
import * as readonly from './examples/readonly';
7273
import * as listWithDetailPrimitives from './examples/list-with-detail-primitives';
@@ -134,6 +135,7 @@ export {
134135
radioGroupExample,
135136
multiEnum,
136137
multiEnumWithLabelAndDesc,
138+
enumI18n,
137139
enumInArray,
138140
readonly,
139141
listWithDetailPrimitives,

0 commit comments

Comments
 (0)