@yamada-ui/use-controllable-state
Advanced tools
Comparing version 0.0.0-dev-20231104154042 to 0.0.0-dev-20231107035012
import { Dispatch, SetStateAction } from 'react'; | ||
type UseControllableStateProps<T extends any> = { | ||
type UseControllableStateProps<T> = { | ||
value?: T; | ||
@@ -9,4 +9,4 @@ defaultValue?: T | (() => T); | ||
}; | ||
declare const useControllableState: <T extends unknown>(props: UseControllableStateProps<T>) => [T, Dispatch<SetStateAction<T>>]; | ||
declare const useControllableState: <T>({ value, ...rest }: UseControllableStateProps<T>) => [T, Dispatch<SetStateAction<T>>]; | ||
export { UseControllableStateProps, useControllableState }; |
@@ -29,15 +29,17 @@ "use client" | ||
var import_react = require("react"); | ||
var useControllableState = (props) => { | ||
var useControllableState = ({ | ||
value, | ||
...rest | ||
}) => { | ||
var _a; | ||
props.onUpdate = (_a = props.onUpdate) != null ? _a : (prev, next) => prev !== next; | ||
const onChange = (0, import_utils.useCallbackRef)(props.onChange); | ||
const onUpdate = (0, import_utils.useCallbackRef)(props.onUpdate); | ||
const [defaultValue, setDefaultValue] = (0, import_react.useState)(props.defaultValue); | ||
const controlledRef = (0, import_react.useRef)(props.value !== void 0); | ||
const controlled = controlledRef.current; | ||
const value = controlled ? props.value : defaultValue; | ||
rest.onUpdate = (_a = rest.onUpdate) != null ? _a : (prev, next) => prev !== next; | ||
const onChange = (0, import_utils.useCallbackRef)(rest.onChange); | ||
const onUpdate = (0, import_utils.useCallbackRef)(rest.onUpdate); | ||
const [defaultValue, setDefaultValue] = (0, import_react.useState)(rest.defaultValue); | ||
const controlled = value !== void 0; | ||
const resolvedValue = controlled ? value : defaultValue; | ||
const setValue = (0, import_utils.useCallbackRef)( | ||
(next) => { | ||
const nextValue = (0, import_utils.runIfFunc)(next, value); | ||
if (!onUpdate(value, nextValue)) | ||
const nextValue = (0, import_utils.runIfFunc)(next, resolvedValue); | ||
if (!onUpdate(resolvedValue, nextValue)) | ||
return; | ||
@@ -48,5 +50,5 @@ if (!controlled) | ||
}, | ||
[controlled, value, onChange, onUpdate] | ||
[controlled, resolvedValue, onChange, onUpdate] | ||
); | ||
return [value, setValue]; | ||
return [resolvedValue, setValue]; | ||
}; | ||
@@ -53,0 +55,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "@yamada-ui/use-controllable-state", | ||
"version": "0.0.0-dev-20231104154042", | ||
"version": "0.0.0-dev-20231107035012", | ||
"description": "Yamada UI useControllableState custom hook", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@yamada-ui/utils": "0.0.0-dev-20231104154042" | ||
"@yamada-ui/utils": "0.3.3" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
95
6994
+ Added@yamada-ui/utils@0.3.3(transitive)
- Removed@yamada-ui/utils@0.0.0-dev-20231104154042(transitive)
Updated@yamada-ui/utils@0.3.3