Skip to content

Commit

Permalink
feat: useTableUtils hook (#1281)
Browse files Browse the repository at this point in the history
resolves #1280
  • Loading branch information
bmingles authored May 9, 2023
1 parent be82b14 commit ce1fe2c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/jsapi-components/src/useTableUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import { ApiContext } from '@deephaven/jsapi-bootstrap';
import dh from '@deephaven/jsapi-shim';
import { TableUtils } from '@deephaven/jsapi-utils';
import { renderHook } from '@testing-library/react-hooks';
import useTableUtils from './useTableUtils';

const wrapper = ({ children }) => (
<ApiContext.Provider value={dh}>{children}</ApiContext.Provider>
);

it('should return a TableUtils instance based on the current dh api context', () => {
const { result } = renderHook(() => useTableUtils(), { wrapper });
expect(result.current.dh).toBe(dh);
expect(result.current).toBeInstanceOf(TableUtils);
});
11 changes: 11 additions & 0 deletions packages/jsapi-components/src/useTableUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useMemo } from 'react';
import { useApi } from '@deephaven/jsapi-bootstrap';
import { TableUtils } from '@deephaven/jsapi-utils';

/**
* Get a `TableUtils` instance using `dh` api from the current context.
*/
export default function useTableUtils(): TableUtils {
const dh = useApi();
return useMemo(() => new TableUtils(dh), [dh]);
}

0 comments on commit ce1fe2c

Please sign in to comment.