Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit aceab9c

Browse files
feat(usesubmittrigger): add useSubmitTrigger (#19)
Add a new trigger called useSubmitTrigger that is responsible for providing submit events for forms
1 parent ad617d6 commit aceab9c

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Collector is a library of React components and hooks that facilitates contextual
2121
- [TrackingView](#trackingview)
2222
- [TrackingElement](#trackingelement)
2323
- [useClickTrigger](#useclicktrigger)
24+
- [useSubmitTrigger](#usesubmittrigger)
2425
- [usePageViewTrigger](#usepageviewtrigger)
2526
- [Code of Conduct (CoC)](#code-of-conduct-coc)
2627
- [Maintainers](#maintainers)
@@ -263,6 +264,41 @@ function Button({ onClick, 'tracking-label': label, children }) {
263264
}
264265
```
265266

267+
### useSubmitTrigger
268+
269+
`useSubmitTrigger` provides you a dispatch function for any kind of form submission event.
270+
271+
The dispatch function accepts the following interface:
272+
273+
```jsx
274+
interface Options {
275+
component?: string;
276+
label?: string;
277+
customParameters?: {
278+
[key: string]: any
279+
};
280+
event: 'submit'; // Added internally by the hook
281+
timestamp: number; // Added internally when the dispatch function is called
282+
}
283+
```
284+
285+
```jsx
286+
import React from 'react';
287+
import { useSubmitTrigger } from '@sumup/collector';
288+
289+
function Form({ children }) {
290+
const dispatch = useSubmitTrigger();
291+
292+
const submitHandler = (e) => {
293+
e.preventDefault();
294+
295+
dispatch({ component: 'form' });
296+
};
297+
298+
return <form onSubmit={handler}>{children}</form>;
299+
}
300+
```
301+
266302
### usePageViewTrigger
267303

268304
`usePageViewTrigger()` lets you dispatch a [page view](#page-view) event.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Copyright 2020, SumUp Ltd.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import useSubmitTrigger from './useSubmitTrigger';
17+
18+
export default useSubmitTrigger;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2019, SumUp Ltd.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import * as React from 'react';
17+
import { render, fireEvent } from '@testing-library/react';
18+
19+
import { Events } from '../../types';
20+
import TrackingRoot from '../../components/TrackingRoot';
21+
22+
import useSubmitTrigger from './useSubmitTrigger';
23+
24+
const DispatchForm = () => {
25+
const dispatch = useSubmitTrigger();
26+
27+
return (
28+
<form
29+
data-testid="dispatch-form"
30+
onSubmit={(e) => {
31+
e.preventDefault();
32+
33+
dispatch({
34+
component: 'form'
35+
});
36+
}}
37+
>
38+
Dispatch button
39+
</form>
40+
);
41+
};
42+
43+
describe('useSubmitTrigger', () => {
44+
it('should provide a dispatch function that contains the submit event', () => {
45+
const dispatch = jest.fn();
46+
const app = 'test-app-hook';
47+
const form = 'dispatch-form';
48+
const component = 'form';
49+
50+
const expected = {
51+
app,
52+
view: undefined,
53+
elementTree: [],
54+
event: Events.submit,
55+
component,
56+
id: undefined,
57+
timestamp: expect.any(Number)
58+
};
59+
60+
const { getByTestId } = render(
61+
<TrackingRoot name={app} onDispatch={dispatch}>
62+
<DispatchForm />
63+
</TrackingRoot>
64+
);
65+
66+
fireEvent.submit(getByTestId(form));
67+
68+
expect(dispatch).toHaveBeenCalledWith(expected);
69+
});
70+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Copyright 2019, SumUp Ltd.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
import useBaseTrigger from '../useBaseTrigger';
17+
import { Events } from '../../types';
18+
19+
const useSubmitTrigger = () => useBaseTrigger(Events.submit);
20+
21+
export default useSubmitTrigger;

0 commit comments

Comments
 (0)