Skip to content

Commit

Permalink
Merge pull request #46 from Ashish-simpleCoder/feature/develop-hooks
Browse files Browse the repository at this point in the history
release minor version
  • Loading branch information
Ashish-simpleCoder authored Jul 4, 2024
2 parents 804f553 + 840c88b commit 6984db6
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 172 deletions.
10 changes: 10 additions & 0 deletions .changeset/beige-ladybugs-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'classic-react-hooks': minor
---

Docs and some refactor for logic

- [c1db97d] docs: remove doc template files
- [d3cdff6] feat: create new copy-to-clipboard function in use-copy-to-clipboard
- [44179bd] test: add test case to handle defaultValue in catch block in use-local-storage
- [d8d5b41] fix: parse logic in use-local-storage
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
with:
node-version: ${{ matrix.node }}

- uses: pnpm/action-setup@v2
with:
version: 8
- uses: pnpm/action-setup@v4
# with:
# version: 8
- uses: actions/setup-node@v3
with:
node-version: 16.x
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
permissions: write-all
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: pnpm/action-setup@v4
# with:
# version: 8
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: pnpm/action-setup@v4
# with:
# version: 8
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- uses: pnpm/action-setup@v4
# with:
# version: 8
- run: pnpm install --no-frozen-lockfile
- run: pnpm build && pnpm test
55 changes: 0 additions & 55 deletions apps/doc/api-examples.md

This file was deleted.

7 changes: 4 additions & 3 deletions apps/doc/hooks/use-counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ outline: deep

### Parameters

| Parameter | Type | Required | Default Value | Description |
| --------- | :----: | :------: | :-----------: | --------------------------------------------------------------------------- |
| key | string || "" | Based on the key, it generates `type-safe` object with `prefixed` proprety. |
| Parameter | Type | Required | Default Value | Description |
| ------------ | :----: | :------: | :-----------: | --------------------------------------------------------------------------- |
| key | string || "" | Based on the key, it generates `type-safe` object with `prefixed` proprety. |
| initialValue | number || 0 | Initial value of the counter. |

### Returns

Expand Down
85 changes: 0 additions & 85 deletions apps/doc/markdown-examples.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'

export { useEventListener as useEventListener } from './lib/use-event-listener'
export { default as useCopyToClipboard } from './lib/use-copy-to-clipboard'
export { default as useCopyToClipboard, copyToClipboardFn } from './lib/use-copy-to-clipboard'
export { default as useLocalStorage } from './lib/use-local-storage'
export { default as useOutsideClick } from './lib/use-outside-click'
export { default as useDebouncedFn, debouncedFnWrapper } from './lib/use-debounced-fn'
Expand Down
34 changes: 19 additions & 15 deletions src/lib/use-copy-to-clipboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ export default function useCopyToClipboard(props?: { onSuccess?: OnSuccess; onEr
const propsRef = useSyncedRef(props)

const copyToClipboard = useRef(async (data: string, onSuccess?: OnSuccess, onError?: OnError) => {
try {
if (navigator.clipboard) {
navigator.clipboard
.writeText(data)
.then(() => (onSuccess || propsRef.current?.onSuccess)?.())
.catch((error) => (onError || propsRef.current?.onError)?.(error))
} else {
const error: Partial<Error> = {
message: 'Cliboard not available',
}
;(onError || propsRef.current?.onError)?.(error as Error)
}
} catch (error) {
;(onError || propsRef.current?.onError)?.(error as Error)
}
copyToClipboardFn(data, onSuccess || propsRef.current?.onSuccess, onError || propsRef.current?.onError)
})

return copyToClipboard.current
}

export async function copyToClipboardFn(data: string, onSuccess?: OnSuccess, onError?: OnError) {
try {
if (navigator.clipboard) {
navigator.clipboard
.writeText(data)
.then(() => onSuccess?.())
.catch((error) => onError?.(error))
} else {
const error: Partial<Error> = {
message: 'Cliboard not available',
}
onError?.(error as Error)
}
} catch (error) {
onError?.(error as Error)
}
}
12 changes: 12 additions & 0 deletions src/lib/use-local-storage/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe('use-local-storage', () => {
expect(typeof result.current[1]).toBe('function')
})

it('should be able handle error when item is undefined in local-storage', () => {
localStorage.setItem('key', '') // when getting localStorage.getItem(key) => '', it results into undefined
const { result } = renderHook(() => useLocalStorage('key'))
expect(result.current[0]).toBeUndefined()
})

it('should be able handle error when with default value param', () => {
localStorage.setItem('key', '')
const { result } = renderHook(() => useLocalStorage('key', {}))
expect(result.current[0]).toStrictEqual({})
})

it('should set the default value', () => {
const { result } = renderHook(() => useLocalStorage('user', { name: 'Saitama' }))

Expand Down
2 changes: 1 addition & 1 deletion src/lib/use-local-storage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function useLocalStorage<State>(key: string, defaultValue?: State
if (defaultValue && item == null) {
return defaultValue
}
const parsed_value = item == null ? '' : JSON.parse(item ?? '')
const parsed_value = item == null ? '' : JSON.parse(item)
return parsed_value
} catch (err) {
if (defaultValue) {
Expand Down

0 comments on commit 6984db6

Please sign in to comment.