-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(hook): migrate to TypeScript (#1872)
* refactor(hook): migrate to TypeScript * chore: changeset * fix(use-toggle-state): do not assign value as type
- Loading branch information
Showing
15 changed files
with
168 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-uikit/hooks': patch | ||
--- | ||
|
||
Migrate `hooks` to TypeScript |
File renamed without changes.
25 changes: 0 additions & 25 deletions
25
packages/hooks/src/use-data-table-sorting-state/use-data-table-sorting-state.js
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
packages/hooks/src/use-data-table-sorting-state/use-data-table-sorting-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useState, useCallback } from 'react'; | ||
import isNil from 'lodash/isNil'; | ||
|
||
type TSortDefinition = { | ||
key: string; | ||
order: 'desc' | 'asc'; | ||
}; | ||
|
||
type TDataTableSortingState = { | ||
value: TSortDefinition; | ||
onChange: ( | ||
key: TSortDefinition['key'], | ||
order: TSortDefinition['order'] | ||
) => void; | ||
}; | ||
|
||
const defaultValues: TSortDefinition = { key: 'createdAt', order: 'desc' }; | ||
const applyIf = ( | ||
values: Partial<TSortDefinition>, | ||
key: 'order' | 'key' | ||
): Partial<TDataTableSortingState> => | ||
!isNil(values[key]) ? { [key]: values[key] } : {}; | ||
|
||
const useDataTableSortingState = ( | ||
initialValues: Partial<TSortDefinition> = {} | ||
): TDataTableSortingState => { | ||
const mergedValues: TSortDefinition = { | ||
...defaultValues, | ||
...applyIf(initialValues, 'key'), | ||
...applyIf(initialValues, 'order'), | ||
}; | ||
const [sortDefinition, setSortDefinition] = useState<TSortDefinition>( | ||
mergedValues | ||
); | ||
const onTableSortingChange = useCallback<TDataTableSortingState['onChange']>( | ||
(key: TSortDefinition['key'], order: TSortDefinition['order']): void => { | ||
setSortDefinition({ | ||
key, | ||
order, | ||
}); | ||
}, | ||
[] | ||
); | ||
|
||
return { | ||
value: sortDefinition, | ||
onChange: onTableSortingChange, | ||
}; | ||
}; | ||
|
||
export default useDataTableSortingState; |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import { getFieldId } from '@commercetools-uikit/utils'; | ||
|
||
type CreateIdFn = () => string; | ||
|
||
const useFieldId = (id: string, createIdFn: CreateIdFn): string => { | ||
const [internalId, setId] = React.useState<string>(id); | ||
|
||
React.useEffect(() => { | ||
const props = { id }; | ||
const state = { id: internalId }; | ||
|
||
setId(getFieldId(props, state, createIdFn)); | ||
}, [id, internalId, setId, createIdFn]); | ||
|
||
return internalId; | ||
}; | ||
|
||
export default useFieldId; |
File renamed without changes.
50 changes: 0 additions & 50 deletions
50
packages/hooks/src/use-pagination-state/use-pagination-state.js
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
packages/hooks/src/use-pagination-state/use-pagination-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useState, useCallback } from 'react'; | ||
import isNil from 'lodash/isNil'; | ||
|
||
type TPaginationDefinition = { | ||
page: number; | ||
perPage: number; | ||
}; | ||
type TState = { | ||
value: number; | ||
onChange: (value: number) => void; | ||
}; | ||
type TPaginationState = { | ||
[P in keyof TPaginationDefinition]: TState; | ||
}; | ||
|
||
const applyIf = ( | ||
values: Partial<TPaginationDefinition>, | ||
key: 'page' | 'perPage' | ||
): Partial<TPaginationDefinition> => | ||
!isNil(values[key]) ? { [key]: values[key] } : {}; | ||
|
||
const defaultValues: TPaginationDefinition = { | ||
page: 1, | ||
perPage: 20, | ||
}; | ||
|
||
const usePaginationState = ( | ||
initialValues: Partial<TPaginationDefinition> = {} | ||
): TPaginationState => { | ||
const mergedValues: TPaginationDefinition = { | ||
...defaultValues, | ||
...applyIf(initialValues, 'page'), | ||
...applyIf(initialValues, 'perPage'), | ||
}; | ||
|
||
const [page, setPage] = useState<TPaginationDefinition['page']>( | ||
mergedValues.page | ||
); | ||
const [perPage, setPerPage] = useState<TPaginationDefinition['perPage']>( | ||
mergedValues.perPage | ||
); | ||
|
||
const onPageChange = useCallback<TState['onChange']>( | ||
(nextPage) => { | ||
setPage(nextPage); | ||
}, | ||
[setPage] | ||
); | ||
|
||
const onPerPageChange = useCallback<TState['onChange']>( | ||
(nextPerPage) => { | ||
// side-effect: | ||
// GIVEN client updates `perPage`, | ||
// THEN we reset `page` (discards initialValues.page) | ||
setPage(1); | ||
setPerPage(nextPerPage); | ||
}, | ||
[setPerPage, setPage] | ||
); | ||
|
||
return { | ||
page: { | ||
value: page, | ||
onChange: onPageChange, | ||
}, | ||
perPage: { | ||
value: perPage, | ||
onChange: onPerPageChange, | ||
}, | ||
}; | ||
}; | ||
|
||
export default usePaginationState; |
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...es/hooks/src/use-previous/use-previous.js → ...es/hooks/src/use-previous/use-previous.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useCallback, useState } from 'react'; | ||
|
||
const useToggleState = (defaultValue?: boolean) => { | ||
const initialValue = typeof defaultValue === 'boolean' ? defaultValue : true; | ||
|
||
const [isToggled, setIsToggled] = useState(initialValue); | ||
const toggle = useCallback<(forceIsToggled?: boolean) => void>( | ||
(forceIsToggled?: boolean) => { | ||
setIsToggled( | ||
typeof forceIsToggled === 'boolean' ? forceIsToggled : !isToggled | ||
); | ||
}, | ||
[isToggled, setIsToggled] | ||
); | ||
return [isToggled, toggle]; | ||
}; | ||
|
||
export default useToggleState; |
043e9d2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: