Skip to content

Commit 043e9d2

Browse files
authored
refactor(hook): migrate to TypeScript (#1872)
* refactor(hook): migrate to TypeScript * chore: changeset * fix(use-toggle-state): do not assign value as type
1 parent 7d56aa9 commit 043e9d2

File tree

15 files changed

+168
-109
lines changed

15 files changed

+168
-109
lines changed

.changeset/lazy-poems-shake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@commercetools-uikit/hooks': patch
3+
---
4+
5+
Migrate `hooks` to TypeScript

packages/hooks/src/use-data-table-sorting-state/use-data-table-sorting-state.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useState, useCallback } from 'react';
2+
import isNil from 'lodash/isNil';
3+
4+
type TSortDefinition = {
5+
key: string;
6+
order: 'desc' | 'asc';
7+
};
8+
9+
type TDataTableSortingState = {
10+
value: TSortDefinition;
11+
onChange: (
12+
key: TSortDefinition['key'],
13+
order: TSortDefinition['order']
14+
) => void;
15+
};
16+
17+
const defaultValues: TSortDefinition = { key: 'createdAt', order: 'desc' };
18+
const applyIf = (
19+
values: Partial<TSortDefinition>,
20+
key: 'order' | 'key'
21+
): Partial<TDataTableSortingState> =>
22+
!isNil(values[key]) ? { [key]: values[key] } : {};
23+
24+
const useDataTableSortingState = (
25+
initialValues: Partial<TSortDefinition> = {}
26+
): TDataTableSortingState => {
27+
const mergedValues: TSortDefinition = {
28+
...defaultValues,
29+
...applyIf(initialValues, 'key'),
30+
...applyIf(initialValues, 'order'),
31+
};
32+
const [sortDefinition, setSortDefinition] = useState<TSortDefinition>(
33+
mergedValues
34+
);
35+
const onTableSortingChange = useCallback<TDataTableSortingState['onChange']>(
36+
(key: TSortDefinition['key'], order: TSortDefinition['order']): void => {
37+
setSortDefinition({
38+
key,
39+
order,
40+
});
41+
},
42+
[]
43+
);
44+
45+
return {
46+
value: sortDefinition,
47+
onChange: onTableSortingChange,
48+
};
49+
};
50+
51+
export default useDataTableSortingState;

packages/hooks/src/use-field-id/use-field-id.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { getFieldId } from '@commercetools-uikit/utils';
3+
4+
type CreateIdFn = () => string;
5+
6+
const useFieldId = (id: string, createIdFn: CreateIdFn): string => {
7+
const [internalId, setId] = React.useState<string>(id);
8+
9+
React.useEffect(() => {
10+
const props = { id };
11+
const state = { id: internalId };
12+
13+
setId(getFieldId(props, state, createIdFn));
14+
}, [id, internalId, setId, createIdFn]);
15+
16+
return internalId;
17+
};
18+
19+
export default useFieldId;

packages/hooks/src/use-pagination-state/use-pagination-state.js

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { useState, useCallback } from 'react';
2+
import isNil from 'lodash/isNil';
3+
4+
type TPaginationDefinition = {
5+
page: number;
6+
perPage: number;
7+
};
8+
type TState = {
9+
value: number;
10+
onChange: (value: number) => void;
11+
};
12+
type TPaginationState = {
13+
[P in keyof TPaginationDefinition]: TState;
14+
};
15+
16+
const applyIf = (
17+
values: Partial<TPaginationDefinition>,
18+
key: 'page' | 'perPage'
19+
): Partial<TPaginationDefinition> =>
20+
!isNil(values[key]) ? { [key]: values[key] } : {};
21+
22+
const defaultValues: TPaginationDefinition = {
23+
page: 1,
24+
perPage: 20,
25+
};
26+
27+
const usePaginationState = (
28+
initialValues: Partial<TPaginationDefinition> = {}
29+
): TPaginationState => {
30+
const mergedValues: TPaginationDefinition = {
31+
...defaultValues,
32+
...applyIf(initialValues, 'page'),
33+
...applyIf(initialValues, 'perPage'),
34+
};
35+
36+
const [page, setPage] = useState<TPaginationDefinition['page']>(
37+
mergedValues.page
38+
);
39+
const [perPage, setPerPage] = useState<TPaginationDefinition['perPage']>(
40+
mergedValues.perPage
41+
);
42+
43+
const onPageChange = useCallback<TState['onChange']>(
44+
(nextPage) => {
45+
setPage(nextPage);
46+
},
47+
[setPage]
48+
);
49+
50+
const onPerPageChange = useCallback<TState['onChange']>(
51+
(nextPerPage) => {
52+
// side-effect:
53+
// GIVEN client updates `perPage`,
54+
// THEN we reset `page` (discards initialValues.page)
55+
setPage(1);
56+
setPerPage(nextPerPage);
57+
},
58+
[setPerPage, setPage]
59+
);
60+
61+
return {
62+
page: {
63+
value: page,
64+
onChange: onPageChange,
65+
},
66+
perPage: {
67+
value: perPage,
68+
onChange: onPerPageChange,
69+
},
70+
};
71+
};
72+
73+
export default usePaginationState;

packages/hooks/src/use-previous/use-previous.js renamed to packages/hooks/src/use-previous/use-previous.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

3-
const usePrevious = (value) => {
4-
const ref = React.useRef();
3+
const usePrevious = <Ref>(value: Ref) => {
4+
const ref = React.useRef<Ref>();
55
React.useEffect(() => {
66
ref.current = value;
77
}, [value]);

packages/hooks/src/use-toggle-state/use-toggle-state.js

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useCallback, useState } from 'react';
2+
3+
const useToggleState = (defaultValue?: boolean) => {
4+
const initialValue = typeof defaultValue === 'boolean' ? defaultValue : true;
5+
6+
const [isToggled, setIsToggled] = useState(initialValue);
7+
const toggle = useCallback<(forceIsToggled?: boolean) => void>(
8+
(forceIsToggled?: boolean) => {
9+
setIsToggled(
10+
typeof forceIsToggled === 'boolean' ? forceIsToggled : !isToggled
11+
);
12+
},
13+
[isToggled, setIsToggled]
14+
);
15+
return [isToggled, toggle];
16+
};
17+
18+
export default useToggleState;

0 commit comments

Comments
 (0)