Skip to content

Commit

Permalink
Merge pull request #98 from trinhthinh388/feat/update-use-toggle-api
Browse files Browse the repository at this point in the history
feat: updated useTogle APi
  • Loading branch information
trinhthinh388 authored Apr 24, 2024
2 parents ff9430c + 576984a commit 1713e96
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rimraf .turbo && rimraf node_modules && rimraf .next"
},
"dependencies": {
"@react-awesome/components": "1.0.19",
"@react-awesome/components": "1.0.20",
"classnames": "^2.5.1",
"lodash": "^4.17.21",
"lucide-react": "^0.315.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/docs/src/pages/docs/use-toggle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { Container } from '../../components/Container'
import { useToggle } from '@react-awesome/components'

export const Example = () => {
const { toggle, isOn } = useToggle(true)
const [isOn, toggle] = useToggle(true)
return (

<>
Expand All @@ -47,7 +47,7 @@ return (
</div>
<label
htmlFor="switch"
class="mt-px mb-0 ml-3 font-light cursor-pointer select-none"
className="mt-px mb-0 ml-3 font-light cursor-pointer select-none"
>
Click to toggle
</label>
Expand All @@ -63,7 +63,7 @@ return (
import { useToggle } from '@react-awesome/use-toggle'

const Example = () => {
const { toggle, isOn } = useToggle(state)
const [isOn, toggle] = useToggle(state)

return <input checked={isOn} type="checkbox" onClick={toggle} />
}
Expand Down
7 changes: 7 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @react-awesome/components

## 1.0.20

### Patch Changes

- Updated dependencies
- @react-awesome/use-toggle@1.0.0

## 1.0.19

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/components",
"version": "1.0.19",
"version": "1.0.20",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -44,7 +44,7 @@
"@react-awesome/use-preserve-input-caret-position": "0.0.3",
"@react-awesome/use-selection-range": "0.0.3",
"@react-awesome/use-previous": "0.0.3",
"@react-awesome/use-toggle": "0.0.1",
"@react-awesome/use-toggle": "1.0.0",
"@react-awesome/use-breakpoint": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions packages/use-toggle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @react-awesome/use-previous

## 1.0.0

### Major Changes

- Return as array of values instead of fixed named object key

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/use-toggle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/use-toggle",
"version": "0.0.1",
"version": "1.0.0",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
16 changes: 8 additions & 8 deletions packages/use-toggle/src/useToggle/useToggle.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ import { useToggle } from './useToggle'

describe('useToggle', () => {
it('Should toggle the value', () => {
const { result, rerender } = renderHook(() => useToggle(true))
const { result } = renderHook(() => useToggle(true))

expect(result.current.isOn).toBe(true)
expect(result.current[1]).toBeInstanceOf(Function)

expect(result.current[0]).toBe(true)

act(() => {
result.current.toggle()
rerender()
result.current[1]()
})

expect(result.current.isOn).toBe(false)
expect(result.current[0]).toBe(false)

act(() => {
result.current.toggle(true)
rerender()
result.current[1](true)
})

expect(result.current.isOn).toBe(true)
expect(result.current[0]).toBe(true)
})
})
11 changes: 5 additions & 6 deletions packages/use-toggle/src/useToggle/useToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React, { useCallback, useState } from 'react'
import { useCallback, useMemo, useState } from 'react'

Check warning on line 1 in packages/use-toggle/src/useToggle/useToggle.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Definition for rule '@typescript-eslint/no-unused-vars' was not found

Check warning on line 1 in packages/use-toggle/src/useToggle/useToggle.tsx

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

Definition for rule '@typescript-eslint/no-unused-vars' was not found

export const useToggle = (initialValue: boolean) => {
export const useToggle = (
initialValue: boolean,
): [boolean, (value?: boolean) => void] => {
const [value, setValue] = useState(initialValue)

const toggle = useCallback(
Expand All @@ -9,8 +11,5 @@ export const useToggle = (initialValue: boolean) => {
[],
)

return {
toggle,
isOn: value,
}
return useMemo(() => [value, toggle], [toggle, value])
}

0 comments on commit 1713e96

Please sign in to comment.