Skip to content

Commit 8c9e091

Browse files
author
Bradley Marques
authored
Allow users to override default labels using mods (#63)
Allow users to override default labels using mods
1 parent 789f0ce commit 8c9e091

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

docs/Mods.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Mods provide for the customization of the Form Builder component, such as the de
44

55
## Type Definition
66

7-
Flow type defintions are available via [flow-typed](https://github.com/flow-typed/flow-typed).
7+
Flow type definitions are available via [flow-typed](https://github.com/flow-typed/flow-typed).
88

99
The type definition for Mods is as follows:
1010

@@ -27,6 +27,10 @@ declare type Mods = {|
2727
labels?: {|
2828
formNameLabel?: string,
2929
formDescriptionLabel?: string,
30+
objectNameLabel?: string,
31+
displayNameLabel?: string,
32+
descriptionLabel?: string,
33+
inputTypeLabel?: string,
3034
|},
3135
showFormHead?: boolean,
3236
|};
@@ -60,7 +64,7 @@ declare type FormInput = {|
6064
|};
6165
```
6266

63-
The `displayName` is the full name of the desired form input. For example, **Short Answer** is the `displayName` for the `shortAnswer` form input.
67+
The `displayName` is the full name of the desired form input. For example, **Short Answer** is the `displayName` for the `shortAnswer` form input.
6468

6569
`matchIf` is an array of scenarios that will cause the `FormBuilder` to recognize a piece of Data and UI schema as this custom input type. For more information, see the *Matching Algorithm* section on this page.
6670

@@ -167,4 +171,8 @@ By passing in alternative text to the `tooltipDescriptions` object of the `mods`
167171
The text for the labels of a few of the inputs in the Form Builder can similarly be customized by specifying a `labels` object of `mods`.
168172

169173
- `formNameLabel` - The label for the input for the name/title of the entire form.
170-
- `formDescriptionLabel` - The lable for the input for the description of the entire form.
174+
- `formDescriptionLabel` - The label for the input for the description of the entire form.
175+
- `objectNameLabel` - The label for the "Object Name" field.
176+
- `displayNameLabel` - The label for the "Display Name" field.
177+
- `descriptionLabel` - The label for the "Description" field.
178+
- `inputTypeLabel` - The label for the "Input Type" field.

flow-libdef/@ginkgo-bioworks/react-json-schema-form-builder_v1.x.x/flow_v0.92.x-/react-json-schema-form-builder_v1.x.x.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ declare module '@ginkgo-bioworks/react-json-schema-form-builder' {
7373
labels?: {|
7474
formNameLabel?: string,
7575
formDescriptionLabel?: string,
76+
objectNameLabel?: string,
77+
displayNameLabel?: string,
78+
descriptionLabel?: string,
79+
inputTypeLabel?: string,
7680
|},
7781
showFormHead?: boolean,
7882
|};

src/formBuilder/Card.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ const props = {
3333
allFormInputs: DEFAULT_FORM_INPUTS,
3434
};
3535

36+
const mods = {
37+
labels: {
38+
objectNameLabel: 'Custom Object Name',
39+
displayNameLabel: 'Custom Display Name',
40+
descriptionLabel: 'Custom Description',
41+
inputTypeLabel: 'Custom Input Type',
42+
},
43+
};
44+
45+
const getHeadingText = (wrapper, index) => {
46+
return wrapper.find('.card-container').first().find('h5').at(index).html();
47+
};
48+
3649
describe('Card', () => {
3750
it('renders without error', () => {
3851
const div = document.createElement('div');
@@ -143,4 +156,42 @@ describe('Card', () => {
143156
]);
144157
mockEvent.mockClear();
145158
});
159+
160+
it('renders with default labels if no mods are passed', () => {
161+
const div = document.createElement('div');
162+
document.body.appendChild(div);
163+
const wrapper = mount(<Card {...props} />, { attachTo: div });
164+
165+
const objectNameLabel = getHeadingText(wrapper, 0);
166+
expect(objectNameLabel).toContain('Object Name');
167+
168+
const displayNameLabel = getHeadingText(wrapper, 1);
169+
expect(displayNameLabel).toContain('Display Name');
170+
171+
const descriptionLabel = getHeadingText(wrapper, 2);
172+
expect(descriptionLabel).toContain('Description');
173+
174+
const inputTypeLabel = getHeadingText(wrapper, 3);
175+
expect(inputTypeLabel).toContain('Input Type');
176+
});
177+
178+
it('renders with passed labels', () => {
179+
const div = document.createElement('div');
180+
document.body.appendChild(div);
181+
182+
const propsWithMods = { ...props, mods: mods };
183+
const wrapper = mount(<Card {...propsWithMods} />, { attachTo: div });
184+
185+
const objectNameLabel = getHeadingText(wrapper, 0);
186+
expect(objectNameLabel).toContain('Custom Object Name');
187+
188+
const displayNameLabel = getHeadingText(wrapper, 1);
189+
expect(displayNameLabel).toContain('Custom Display Name');
190+
191+
const descriptionLabel = getHeadingText(wrapper, 2);
192+
expect(descriptionLabel).toContain('Custom Description');
193+
194+
const inputTypeLabel = getHeadingText(wrapper, 3);
195+
expect(inputTypeLabel).toContain('Custom Input Type');
196+
});
146197
});

src/formBuilder/CardGeneralParameterInputs.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,22 @@ export default function CardGeneralParameterInputs({
3232
);
3333
const categoryMap = categoryToNameMap(parameters.category, allFormInputs);
3434

35+
const fetchLabel = (labelName: string, defaultLabel: string): string => {
36+
return mods && mods.labels && typeof mods.labels[labelName] === 'string'
37+
? mods.labels[labelName]
38+
: defaultLabel;
39+
};
40+
41+
const objectNameLabel = fetchLabel('objectNameLabel', 'Object Name');
42+
const displayNameLabel = fetchLabel('displayNameLabel', 'Display Name');
43+
const descriptionLabel = fetchLabel('descriptionLabel', 'Description');
44+
const inputTypeLabel = fetchLabel('inputTypeLabel', 'Input Type');
45+
3546
return (
3647
<div>
3748
<div className='card-entry'>
3849
<h5>
39-
Object Name{' '}
50+
{`${objectNameLabel} `}
4051
<Tooltip
4152
text={
4253
mods &&
@@ -72,7 +83,7 @@ export default function CardGeneralParameterInputs({
7283
}`}
7384
>
7485
<h5>
75-
Display Name{' '}
86+
{`${displayNameLabel} `}
7687
<Tooltip
7788
text={
7889
mods &&
@@ -101,7 +112,7 @@ export default function CardGeneralParameterInputs({
101112
</div>
102113
<div className={`card-entry ${parameters.$ref ? 'disabled-input' : ''}`}>
103114
<h5>
104-
Description{' '}
115+
{`${descriptionLabel} `}
105116
<Tooltip
106117
text={
107118
mods &&
@@ -130,7 +141,7 @@ export default function CardGeneralParameterInputs({
130141
</div>
131142
<div className='card-entry'>
132143
<h5>
133-
Input Type{' '}
144+
{`${inputTypeLabel} `}
134145
<Tooltip
135146
text={
136147
mods &&

src/formBuilder/types.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export type Mods = {
120120
labels?: {
121121
formNameLabel?: string,
122122
formDescriptionLabel?: string,
123+
objectNameLabel?: string,
124+
displayNameLabel?: string,
125+
descriptionLabel?: string,
126+
inputTypeLabel?: string,
123127
},
124128
showFormHead?: boolean,
125129
};

0 commit comments

Comments
 (0)