Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/tiny-react-hooks/src/useDisclosure/useDisclosure.demo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import { useDisclosure } from './useDisclosure';

export default function Component() {
export function Component() {
const {
isOpen,
onClose,
onOpen,
onToggle,
} = useDisclosure();
// const {
// isOpen,
// onClose,
// onOpen,
// onToggle,
// } = useDisclosure(false);
// const {
// isOpen,
// onClose,
// onOpen,
// onToggle,
// } = useDisclosure(() => false);

return (
<>
Expand All @@ -18,4 +30,4 @@ export default function Component() {
<button onClick={onToggle}>Toggle</button>
</>
)
}
}
47 changes: 36 additions & 11 deletions packages/tiny-react-hooks/src/useDisclosure/useDisclosure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ describe('useDisclosure()', () => {
expect(typeof result.current.onToggle).toBe('function');
});

describe('with no default value', () => {
it('should return isOpen with false when nothing is passed as argument', () => {
const { result } = renderHook(() => useDisclosure());
expect(result.current.isOpen).toBe(false);
});
});

describe('with default value', () => {
describe('with correct default value as boolean', () => {
describe('should work with default value', () => {
describe('with correct default value', () => {
describe('with default value is a boolean', () => {
it('should return isOpen with true', () => {
const { result } = renderHook(() => useDisclosure(true));
expect(result.current.isOpen).toBe(true);
Expand All @@ -24,20 +30,39 @@ describe('useDisclosure()', () => {
const { result } = renderHook(() => useDisclosure(false));
expect(result.current.isOpen).toBe(false);
});
it('should return isOpen with false when nothing is passed as argument', () => {
const { result } = renderHook(() => useDisclosure());
});

describe('with default value is function', () => {
it('should return isOpen with true', () => {
const { result } = renderHook(() => useDisclosure(() => true));
expect(result.current.isOpen).toBe(true);
});
it('should return isOpen with false', () => {
const { result } = renderHook(() => useDisclosure(() => false));
expect(result.current.isOpen).toBe(false);
});
});
});
describe('with incorrect default value type', () => {
it('should throw an error', () => {
const nonBoolean = '' as never;
vi.spyOn(console, 'error').mockImplementation(() => vi.fn());
expect(() => {
renderHook(() => useDisclosure(nonBoolean));
}).toThrowError('defaultValue must be a boolean value');
vi.resetAllMocks();
describe('with default value is a boolean', () => {
it('should throw an error', () => {
const nonBoolean = '' as never;
vi.spyOn(console, 'error').mockImplementation(() => vi.fn());
expect(() => {
renderHook(() => useDisclosure(nonBoolean));
}).toThrowError('defaultValue must be a boolean value');
vi.resetAllMocks();
});
});
describe('with default value is a function', () => {
it('should throw an error', () => {
const nonBoolean = () => '' as never;
vi.spyOn(console, 'error').mockImplementation(() => vi.fn());
expect(() => {
renderHook(() => useDisclosure(nonBoolean));
}).toThrowError('defaultValue must be a boolean value');
vi.resetAllMocks();
});
});
});
});
Expand Down
13 changes: 8 additions & 5 deletions packages/tiny-react-hooks/src/useDisclosure/useDisclosure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState } from 'react';
import { useCallback, useRef, useState } from 'react';

type UseDisclosureReturn = {
isOpen: boolean;
Expand All @@ -7,9 +7,11 @@ type UseDisclosureReturn = {
onToggle: () => void;
}

type DisclosureDefaultValue = boolean | (() => boolean);

/**
* Custom hook that handles boolean state with useful utility functions.
* @param {boolean} [defaultValue] - The initial value for the boolean state (default is `false`).
* @param {DisclosureDefaultValue} [defaultValue] - The initial value or produce default value function for the boolean state (default is `false`).
* @returns {UseDisclosureReturn} An object containing the boolean state value and utility functions to manipulate the state.
* @throws Will throw an error if `defaultValue` is an invalid boolean value.
* @public
Expand All @@ -18,9 +20,10 @@ type UseDisclosureReturn = {
* const { isOpen, onOpen, onClose, onToggle } = UseDisclosureReturn(true);
* ```
*/
export function useDisclosure(defaultValue = false): UseDisclosureReturn {
if (typeof defaultValue !== 'boolean') throw new Error('defaultValue must be a boolean value');
const [isOpen, setOpen] = useState<boolean>(defaultValue);
export function useDisclosure(init: DisclosureDefaultValue = false): UseDisclosureReturn {
const initialValue = useRef<boolean>(typeof init === 'function' ? init() : init);
if (typeof initialValue.current !== 'boolean') throw new Error('defaultValue must be a boolean value');
const [isOpen, setOpen] = useState<boolean>(initialValue.current);

const onOpen = useCallback(() => {
setOpen(true);
Expand Down