Skip to content

Commit 6b539b9

Browse files
committed
fix(renderer): onCancel receives form values
1 parent 13bdaa4 commit 6b539b9

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

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)