Skip to content

Commit 690e0d0

Browse files
authored
Merge pull request #172 from rvsia/add_label_to_form
fix(renderer): add label prop to form
2 parents bfdf7f4 + b44d669 commit 690e0d0

File tree

5 files changed

+52
-167
lines changed

5 files changed

+52
-167
lines changed

packages/react-form-renderer/src/form-renderer/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const FormRenderer = ({
5757
return <SchemaErrorComponent name={ schemaError.name } message={ schemaError.message } />;
5858
}
5959

60+
const label = inputSchema.schema.title || inputSchema.schema.label;
61+
6062
return (
6163
<Form
6264
onSubmit={ onSubmit }
@@ -90,7 +92,7 @@ const FormRenderer = ({
9092
<RendererContext.Consumer>
9193
{ ({ layoutMapper: { FormWrapper }}) => (
9294
<FormWrapper>
93-
{ inputSchema.schema.title && renderTitle(inputSchema.schema.title) }
95+
{ label && renderTitle(label) }
9496
{ inputSchema.schema.description && renderDescription(inputSchema.schema.description) }
9597
{ renderForm(inputSchema.schema.fields) }
9698
{ onStateUpdate && <FormSpy onChange={ onStateUpdate } /> }

packages/react-form-renderer/src/tests/form-renderer/__snapshots__/form-information.test.js.snap

Lines changed: 2 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -157,163 +157,6 @@ exports[`<FormControls /> should render with description 1`] = `
157157
</ContextWrapper>
158158
`;
159159

160-
exports[`<FormControls /> should render with title 1`] = `
161-
<ContextWrapper>
162-
<FormRenderer
163-
buttonClassName=""
164-
buttonsLabels={Object {}}
165-
clearOnUnmount={false}
166-
disableSubmit={Array []}
167-
formFieldsMapper={Object {}}
168-
formType="pf3"
169-
initialValues={Object {}}
170-
layoutMapper={
171-
Object {
172-
"Button": [Function],
173-
"ButtonGroup": [Function],
174-
"Description": [Function],
175-
"FormWrapper": [Function],
176-
"Title": [Function],
177-
}
178-
}
179-
onSubmit={[MockFunction]}
180-
resetAble={false}
181-
schema={
182-
Object {
183-
"fields": Array [],
184-
"title": "Title",
185-
}
186-
}
187-
schemaType="default"
188-
showFormControls={true}
189-
uiSchema={Object {}}
190-
>
191-
<ReactFinalForm
192-
decorators={
193-
Array [
194-
[Function],
195-
]
196-
}
197-
initialValues={Object {}}
198-
mutators={
199-
Object {
200-
"concat": [Function],
201-
"insert": [Function],
202-
"move": [Function],
203-
"pop": [Function],
204-
"push": [Function],
205-
"remove": [Function],
206-
"removeBatch": [Function],
207-
"shift": [Function],
208-
"swap": [Function],
209-
"unshift": [Function],
210-
"update": [Function],
211-
}
212-
}
213-
onSubmit={[MockFunction]}
214-
render={[Function]}
215-
subscription={
216-
Object {
217-
"pristine": true,
218-
"submitting": true,
219-
"valid": true,
220-
}
221-
}
222-
>
223-
<Component>
224-
<form>
225-
<Component>
226-
<div>
227-
Title
228-
</div>
229-
</Component>
230-
<FormControls
231-
buttonClassName=""
232-
buttonOrder={
233-
Array [
234-
"submit",
235-
"reset",
236-
"cancel",
237-
]
238-
}
239-
canReset={false}
240-
canSubmit={false}
241-
cancelLabel="Cancel"
242-
handleSubmit={[Function]}
243-
resetLabel="Reset"
244-
submitLabel="Submit"
245-
>
246-
<ReactFinalForm(FormSpy)>
247-
<FormSpy
248-
reactFinalForm={
249-
Object {
250-
"batch": [Function],
251-
"blur": [Function],
252-
"change": [Function],
253-
"destroyOnUnregister": false,
254-
"focus": [Function],
255-
"getFieldState": [Function],
256-
"getRegisteredFields": [Function],
257-
"getState": [Function],
258-
"initialize": [Function],
259-
"isValidationPaused": [Function],
260-
"mutators": Object {
261-
"concat": [Function],
262-
"insert": [Function],
263-
"move": [Function],
264-
"pop": [Function],
265-
"push": [Function],
266-
"remove": [Function],
267-
"removeBatch": [Function],
268-
"shift": [Function],
269-
"swap": [Function],
270-
"unshift": [Function],
271-
"update": [Function],
272-
},
273-
"pauseValidation": [Function],
274-
"registerField": [Function],
275-
"reset": [Function],
276-
"resetFieldState": [Function],
277-
"resumeValidation": [Function],
278-
"setConfig": [Function],
279-
"submit": [Function],
280-
"subscribe": [Function],
281-
}
282-
}
283-
>
284-
<Component
285-
className=""
286-
>
287-
<div
288-
className=""
289-
>
290-
<Component
291-
key="form-submit"
292-
label="Submit"
293-
onClick={[Function]}
294-
type="button"
295-
variant="primary"
296-
>
297-
<button
298-
onClick={[Function]}
299-
type="button"
300-
variant="primary"
301-
>
302-
Submit
303-
</button>
304-
</Component>
305-
</div>
306-
</Component>
307-
</FormSpy>
308-
</ReactFinalForm(FormSpy)>
309-
</FormControls>
310-
</form>
311-
</Component>
312-
</ReactFinalForm>
313-
</FormRenderer>
314-
</ContextWrapper>
315-
`;
316-
317160
exports[`<FormControls /> should render with title and description 1`] = `
318161
<ContextWrapper>
319162
<FormRenderer
@@ -381,9 +224,9 @@ exports[`<FormControls /> should render with title and description 1`] = `
381224
<Component>
382225
<form>
383226
<Component>
384-
<div>
227+
<h1>
385228
Title
386-
</div>
229+
</h1>
387230
</Component>
388231
<Component>
389232
<div>

packages/react-form-renderer/src/tests/form-renderer/form-information.test.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('<FormControls />', () => {
2121
layoutMapper = {
2222
[layoutComponents.BUTTON]: ({ label, ...props }) => <button { ...props }>{ label }</button>,
2323
[layoutComponents.BUTTON_GROUP]: ({ children, ...props }) => <div { ...props }>{ children }</div>,
24-
[layoutComponents.TITLE]: ({ children }) => <div>{ children }</div>,
24+
[layoutComponents.TITLE]: ({ children }) => <h1>{ children }</h1>,
2525
[layoutComponents.DESCRIPTION]: ({ children }) => <div>{ children }</div>,
2626
[layoutComponents.FORM_WRAPPER]: ({ children }) => <form>{ children }</form>,
2727
};
@@ -40,13 +40,24 @@ describe('<FormControls />', () => {
4040
};
4141
});
4242

43+
const TITLE = 'tiiiitle';
44+
4345
it('should render with title', () => {
4446
const wrapper = mount(
4547
<ContextWrapper>
46-
<FormRenderer { ...initialProps } schema={{ ...schema, title: 'Title' }}/>
48+
<FormRenderer { ...initialProps } schema={{ ...schema, title: TITLE }}/>
4749
</ContextWrapper>
4850
);
49-
expect(toJson(wrapper)).toMatchSnapshot();
51+
expect(wrapper.find('h1').text().includes(TITLE)).toEqual(true);
52+
});
53+
54+
it('should render with label', () => {
55+
const wrapper = mount(
56+
<ContextWrapper>
57+
<FormRenderer { ...initialProps } schema={{ ...schema, label: TITLE }}/>
58+
</ContextWrapper>
59+
);
60+
expect(wrapper.find('h1').text().includes(TITLE)).toEqual(true);
5061
});
5162

5263
it('should render with description', () => {

packages/react-renderer-demo/src/docs-components/component-mapping.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ Check the example below to see a simple implementation of layout components.
5858
|----|----|-----------|-----|------------|
5959
|children|node|Wrapper around form buttons||No|
6060

61-
#### FormGroup
62-
|Prop|Type|Description|Value|Customizable|
63-
|----|----|-----------|-----|------------|
64-
|children|node|Form field group||Based on form schema|
61+
#### Title
62+
|Prop|Type|Description|Customizable|
63+
|----|----|-----------|------------|
64+
|children|node|Form title|no|
65+
66+
#### Description
67+
|Prop|Type|Description|Customizable|
68+
|----|----|-----------|------------|
69+
|children|node|Form description|no|
6570

6671
## Creating formFieldsMapper
6772

packages/react-renderer-demo/src/docs-components/renderer-api.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,27 @@ Form Renderer provides a lot of customization via props.
3434
|subscription|object|You can pass your own [subscription](https://final-form.org/docs/react-final-form/types/FormProps#subscription), which will be added to default settings.|`{ pristine: true, submitting: true, valid: true }`|
3535
|uiSchema|object|Use when you need to use mozilla schema.|{ }|
3636
|<NavLink to="/renderer/validators">validate</NavLink>|func|A function which receives all form values and returns an object with errors.||
37+
38+
### Schema
39+
40+
The root object of the schema represents the <NavLink to="/renderer/component-mapping#formwrapper">Form</NavLink> component, which accepts only these three props:
41+
42+
|Prop|Type|Description|
43+
|----|:--:|----------:|
44+
|label, title|node|<NavLink to="/renderer/component-mapping#title">Title</NavLink> of the form. Optional.|
45+
|description|node|<NavLink to="/renderer/component-mapping#description">Description</NavLink> of the form. Optional.|
46+
|fields|array of objects|<NavLink to="/renderer/component-api">Components</NavLink> of the form. Required!|
47+
48+
#### Example
49+
50+
```javascript
51+
schema = {
52+
title: 'Your name',
53+
description: 'Add your name',
54+
fields: [{
55+
name: 'userName',
56+
label: 'Your name is',
57+
component: componentTypes.TEXT_FIELD,
58+
}]
59+
};
60+
```

0 commit comments

Comments
 (0)