Skip to content

Commit

Permalink
define values on useTrack fn
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan committed Mar 30, 2024
1 parent a3dcd61 commit bf708a8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/react-on/src/lib/handlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ export function createTrackingHandlerProvider<TBase extends ReactOnBase = ReactO
return <TrackingHandler {...props} onHandle={onHandle} />;
}

function useTrack() {
function useTrack({ values }: { values?: Partial<TValues> } = {}) {
const handler = useContext(handlerCtx).handle;
const ctxValue = useContext(ctx);

return useStableCallback<TTrackFn>((options) => {
if (ctxValue.enabled) {
handler({ ...options, values: { ...ctxValue.values, ...options?.values } as TValues });
handler({ ...options, values: { ...ctxValue.values, ...values, ...options?.values } as TValues });
}
});
}
Expand Down
7 changes: 2 additions & 5 deletions packages/react-on/src/lib/track-callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ export function createTrackCallback<TBase extends ReactOnBase = ReactOn>(useTrac
function TrackCallback<
ComponentType = DOMAttributes<any>,
CallbackName extends PropertyKey = CallbackNames<ComponentType> | (string & {}),
>({ children, callback, name, disabled, ...props }: TProps & { callback: CallbackName }) {
>({ children, callback, name, disabled, values, ...props }: TProps & { callback: CallbackName }) {
children = Children.only(children) as ReactElement;

if (!children) throw new Error('Children passed to track directive must be an element with ref');
if (!callback) throw new Error('Callback name must be provided');

const resolvedName = name ?? String(callback);
const track = useTrack();

const trackFn = useStableCallback((...args: any[]) =>
track({ values: props.values, args: [resolvedName, ...args] }),
);
const trackFn = useStableCallback((...args: any[]) => track({ values, args: [resolvedName, ...args] }));

const originalCallback = children.props[callback];
const handle = useCallback(
Expand Down
3 changes: 2 additions & 1 deletion packages/react-on/src/lib/track-event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function createTrackEvent<TBase extends ReactOnBase = ReactOn>(useTrack:

const resolvedName = name ?? event;
const track = useTrack();
const trackFn = useStableCallback((...args: any[]) => track({ values: values, args: [resolvedName, ...args] }));
const trackFn = useStableCallback((...args: any[]) => track({ values, args: [resolvedName, ...args] }));

const handle = useCallback(
(ev: HTMLElementEventMap[EventName], ...args: any[]) => {
trackFn(ev, ...args);
Expand Down

0 comments on commit bf708a8

Please sign in to comment.