Skip to content

Commit c9dc812

Browse files
authored
Merge pull request #202 from rvsia/cancel-values
fix(renderer): onCancel receives form values
2 parents 13bdaa4 + 05cdaf4 commit c9dc812

File tree

8 files changed

+331
-279
lines changed

8 files changed

+331
-279
lines changed

packages/pf3-component-mapper/src/tests/__snapshots__/tabs.test.js.snap

Lines changed: 272 additions & 260 deletions
Large diffs are not rendered by default.

packages/pf3-component-mapper/src/tests/form-fields/date-time-picker/__snapshots__/year-selector.test.js.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ exports[`<YearSelector /> should render correctly 1`] = `
4242
<span
4343
aria-hidden={true}
4444
className="fa fa-angle-left"
45-
/>
45+
>
46+
<span>
47+
Prev interval
48+
</span>
49+
</span>
4650
</FontAwesome>
4751
</Icon>
4852
</td>
@@ -67,7 +71,11 @@ exports[`<YearSelector /> should render correctly 1`] = `
6771
<span
6872
aria-hidden={true}
6973
className="fa fa-angle-right"
70-
/>
74+
>
75+
<span>
76+
Next interval
77+
</span>
78+
</span>
7179
</FontAwesome>
7280
</Icon>
7381
</td>

packages/pf4-component-mapper/src/tests/__snapshots__/form-fields.test.js.snap

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,8 @@ exports[`FormFields should render Switch correctly 1`] = `
857857
<input
858858
aria-label=""
859859
aria-labelledby="someIdKey-on"
860+
checked={false}
860861
className="pf-c-switch__input"
861-
defaultChecked={false}
862862
disabled={false}
863863
id="someIdKey"
864864
name="Name of the field"
@@ -959,6 +959,7 @@ exports[`FormFields should render TextArea correctly 1`] = `
959959
isRequired={false}
960960
isValid={true}
961961
name="Name of the field"
962+
resizeOrientation="both"
962963
value=""
963964
>
964965
<textarea
@@ -1997,8 +1998,8 @@ exports[`FormFields should render disabled Switch correctly 1`] = `
19971998
<input
19981999
aria-label=""
19992000
aria-labelledby="someIdKey-on"
2001+
checked={false}
20002002
className="pf-c-switch__input"
2001-
defaultChecked={false}
20022003
disabled={true}
20032004
id="someIdKey"
20042005
name="Name of the field"
@@ -2165,8 +2166,8 @@ exports[`FormFields should render disabled Switch correctly 2`] = `
21652166
<input
21662167
aria-label=""
21672168
aria-labelledby="someIdKey-on"
2169+
checked={false}
21682170
className="pf-c-switch__input"
2169-
defaultChecked={false}
21702171
disabled={true}
21712172
id="someIdKey"
21722173
name="Name of the field"
@@ -2270,6 +2271,7 @@ exports[`FormFields should render disabled TextArea correctly 1`] = `
22702271
isRequired={false}
22712272
isValid={true}
22722273
name="Name of the field"
2274+
resizeOrientation="both"
22732275
value=""
22742276
>
22752277
<textarea
@@ -2486,8 +2488,8 @@ exports[`FormFields should render with onText/OffText Switch correctly 1`] = `
24862488
<input
24872489
aria-label=""
24882490
aria-labelledby="someIdKey-on"
2491+
checked={false}
24892492
className="pf-c-switch__input"
2490-
defaultChecked={false}
24912493
disabled={false}
24922494
id="someIdKey"
24932495
name="Name of the field"

packages/pf4-component-mapper/src/tests/__snapshots__/tabs.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`Tabs component should render tabs correctly 1`] = `
55
activeKey={0}
66
onSelect={[Function]}
77
>
8-
<ForwardRef
8+
<Tab
99
eventKey={0}
1010
key="cosiName"
1111
title="cosiTitle"
@@ -17,8 +17,8 @@ exports[`Tabs component should render tabs correctly 1`] = `
1717
Here would be form
1818
</div>
1919
</div>
20-
</ForwardRef>
21-
<ForwardRef
20+
</Tab>
21+
<Tab
2222
eventKey={1}
2323
key="cosiName2"
2424
title="cosiTitle2"
@@ -30,6 +30,6 @@ exports[`Tabs component should render tabs correctly 1`] = `
3030
Here would be form
3131
</div>
3232
</div>
33-
</ForwardRef>
33+
</Tab>
3434
</Component>
3535
`;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const FormControls = ({
3838
: (
3939
<RendererContext.Consumer>
4040
{ ({ layoutMapper: { Button, ButtonGroup }}) => {
41-
const { submitting, pristine, validating, form: { submit, reset }} = formSpyProps;
41+
const { submitting, pristine, validating, form: { submit, reset }, values } = formSpyProps;
4242
const buttons = {
4343
submit: (
4444
<Button
@@ -64,7 +64,7 @@ const FormControls = ({
6464
label={ resetLabel }
6565
/>
6666
) : null,
67-
cancel: onCancel ? <Button key="form-cancel" type="button" onClick={ onCancel } label={ cancelLabel } /> : null,
67+
cancel: onCancel ? <Button key="form-cancel" type="button" onClick={ () => onCancel(values) } label={ cancelLabel } /> : null,
6868
};
6969
return (
7070
<ButtonGroup className={ buttonClassName }>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ exports[`<FormControls /> should add missing buttons if not defined in button or
7878
<Component
7979
key="form-cancel"
8080
label="Cancel"
81-
onClick={[MockFunction]}
81+
onClick={[Function]}
8282
type="button"
8383
>
8484
<button
85-
onClick={[MockFunction]}
85+
onClick={[Function]}
8686
type="button"
8787
>
8888
Cancel
@@ -184,11 +184,11 @@ exports[`<FormControls /> should render all controls and with default labels 1`]
184184
<Component
185185
key="form-cancel"
186186
label="Cancel"
187-
onClick={[MockFunction]}
187+
onClick={[Function]}
188188
type="button"
189189
>
190190
<button
191-
onClick={[MockFunction]}
191+
onClick={[Function]}
192192
type="button"
193193
>
194194
Cancel
@@ -257,11 +257,11 @@ exports[`<FormControls /> should render buttons in correct order 1`] = `
257257
<Component
258258
key="form-cancel"
259259
label="Cancel"
260-
onClick={[MockFunction]}
260+
onClick={[Function]}
261261
type="button"
262262
>
263263
<button
264-
onClick={[MockFunction]}
264+
onClick={[Function]}
265265
type="button"
266266
>
267267
Cancel

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('<FormControls />', () => {
1010
let initialProps;
1111
let ContextWrapper;
1212
let initialLayout;
13+
1314
beforeEach(() => {
1415
initialLayout = {
1516
[layoutComponents.BUTTON]: ({ label, ...props }) => <button { ...props }>{ label }</button>,
@@ -36,6 +37,12 @@ describe('<FormControls />', () => {
3637
};
3738
});
3839

40+
afterEach(() => {
41+
initialProps.onCancel.mockReset();
42+
initialProps.onSubmit.mockReset();
43+
initialProps.onReset.mockReset();
44+
});
45+
3946
it('should render all controls and with default labels', () => {
4047
const wrapper = mount(
4148
<ContextWrapper>
@@ -78,4 +85,27 @@ describe('<FormControls />', () => {
7885
);
7986
expect(toJson(wrapper)).toMatchSnapshot();
8087
});
88+
89+
it('should call cancel with form values', () => {
90+
const wrapper = mount(
91+
<ContextWrapper>
92+
<FormControls { ...initialProps } />
93+
</ContextWrapper>
94+
);
95+
96+
const CANCEL_INDEX = 2;
97+
const FIELD_NAME = 'xxx';
98+
const FIELD_VALUE = 'yyy';
99+
100+
const expectedValues = {
101+
[FIELD_NAME]: FIELD_VALUE,
102+
};
103+
104+
wrapper.find(Form).instance().form.change(FIELD_NAME, FIELD_VALUE);
105+
wrapper.update();
106+
107+
wrapper.find('button').at(CANCEL_INDEX).simulate('click');
108+
109+
expect(initialProps.onCancel).toHaveBeenCalledWith(expectedValues);
110+
});
81111
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Form Renderer provides a lot of customization via props.
2525
|<NavLink to="/renderer/unmounting">clearOnUnmount</NavLink>|bool|Will clear values of unmounted components. You can also set this to specific component in the form schema.|false|
2626
|canReset|bool|Show/hide reset button.|false|
2727
|onReset|func|A reset callback. You don't need to manually clear the form values!||
28-
|onCancel|func|A cancel callback.||
28+
|onCancel|func|A cancel callback, which receives `values` as the first argument.||
2929
|onStateUpdate|func|A function which will be called with every form update, i.e. `({ values }) => setValues(values)`||
3030
|disableSubmit|array of strings|You can specify a form attributes (see [here](https://final-form.org/docs/final-form/types/FormState)) which will make the submit button disabled. |[ ]|
3131
|initialValues|object|An object of fields names as keys and values as their values.||

0 commit comments

Comments
 (0)