react-simplikit 0.0.23 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/hooks/useControlledState/index.ts
21
+ var useControlledState_exports = {};
22
+ __export(useControlledState_exports, {
23
+ useControlledState: () => useControlledState
24
+ });
25
+ module.exports = __toCommonJS(useControlledState_exports);
26
+
27
+ // src/hooks/useControlledState/useControlledState.ts
28
+ var import_react = require("react");
29
+ function useControlledState({
30
+ value: valueProp,
31
+ defaultValue,
32
+ onChange,
33
+ equalityFn = Object.is
34
+ }) {
35
+ const [uncontrolledState, setUncontrolledState] = (0, import_react.useState)(defaultValue);
36
+ const controlled = valueProp !== void 0;
37
+ const value = controlled ? valueProp : uncontrolledState;
38
+ const setValue = (0, import_react.useCallback)(
39
+ (next) => {
40
+ const nextValue = isSetStateAction(next) ? next(value) : next;
41
+ if (equalityFn(value, nextValue) === true) return;
42
+ if (controlled === false) setUncontrolledState(nextValue);
43
+ if (controlled === true && nextValue === void 0) setUncontrolledState(nextValue);
44
+ onChange?.(nextValue);
45
+ },
46
+ [controlled, onChange, equalityFn, value]
47
+ );
48
+ return [value, setValue];
49
+ }
50
+ function isSetStateAction(next) {
51
+ return typeof next === "function";
52
+ }
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ useControlledState
56
+ });
@@ -0,0 +1,51 @@
1
+ import { Dispatch, SetStateAction } from 'react';
2
+
3
+ type ControlledState<T> = {
4
+ value: T;
5
+ defaultValue?: never;
6
+ } | {
7
+ defaultValue: T;
8
+ value?: T;
9
+ };
10
+ type UseControlledStateProps<T> = ControlledState<T> & {
11
+ onChange?: (value: T) => void;
12
+ equalityFn?: (prev: T, next: T) => boolean;
13
+ };
14
+ /**
15
+ * @description
16
+ * `useControlledState` is a hook that allows you to control both controlled and uncontrolled states.
17
+ * If you pass the state to `value`, it will be a controlled state, and if you pass the state to `defaultValue`, it will be an uncontrolled state.
18
+ * If both `value` and `defaultValue` are passed, `value` will take precedence.
19
+ *
20
+ * @param {Object} props
21
+ * @param {T} [props.value] - The value of the state.
22
+ * @param {T} [props.defaultValue] - The default value of the state.
23
+ * @param {(value: T) => void} [props.onChange] - The callback function that is called when the state changes.
24
+ * @param {(prev: T, next: T) => boolean} [props.equalityFn] - The function that is used to compare the previous and next values.
25
+ *
26
+ * @returns {[T, Dispatch<SetStateAction<T>>]} - The state and the setter function.
27
+ *
28
+ * @example
29
+ * type ToggleProps = {
30
+ * value?: boolean;
31
+ * defaultValue?: boolean;
32
+ * onChange?: (value: boolean) => void;
33
+ * }
34
+ *
35
+ * function Toggle({ value, defaultValue, onChange }: ToggleProps) {
36
+ * const [on, setOn] = useControlledState({
37
+ * value,
38
+ * defaultValue: defaultValue ?? false,
39
+ * onChange,
40
+ * });
41
+ *
42
+ * return (
43
+ * <button onClick={() => setOn((prev) => !prev)}>
44
+ * {on ? 'ON' : 'OFF'}
45
+ * </button>
46
+ * )
47
+ * }
48
+ */
49
+ declare function useControlledState<T>({ value: valueProp, defaultValue, onChange, equalityFn, }: UseControlledStateProps<T>): [T, Dispatch<SetStateAction<T>>];
50
+
51
+ export { useControlledState };
@@ -0,0 +1,51 @@
1
+ import { Dispatch, SetStateAction } from 'react';
2
+
3
+ type ControlledState<T> = {
4
+ value: T;
5
+ defaultValue?: never;
6
+ } | {
7
+ defaultValue: T;
8
+ value?: T;
9
+ };
10
+ type UseControlledStateProps<T> = ControlledState<T> & {
11
+ onChange?: (value: T) => void;
12
+ equalityFn?: (prev: T, next: T) => boolean;
13
+ };
14
+ /**
15
+ * @description
16
+ * `useControlledState` is a hook that allows you to control both controlled and uncontrolled states.
17
+ * If you pass the state to `value`, it will be a controlled state, and if you pass the state to `defaultValue`, it will be an uncontrolled state.
18
+ * If both `value` and `defaultValue` are passed, `value` will take precedence.
19
+ *
20
+ * @param {Object} props
21
+ * @param {T} [props.value] - The value of the state.
22
+ * @param {T} [props.defaultValue] - The default value of the state.
23
+ * @param {(value: T) => void} [props.onChange] - The callback function that is called when the state changes.
24
+ * @param {(prev: T, next: T) => boolean} [props.equalityFn] - The function that is used to compare the previous and next values.
25
+ *
26
+ * @returns {[T, Dispatch<SetStateAction<T>>]} - The state and the setter function.
27
+ *
28
+ * @example
29
+ * type ToggleProps = {
30
+ * value?: boolean;
31
+ * defaultValue?: boolean;
32
+ * onChange?: (value: boolean) => void;
33
+ * }
34
+ *
35
+ * function Toggle({ value, defaultValue, onChange }: ToggleProps) {
36
+ * const [on, setOn] = useControlledState({
37
+ * value,
38
+ * defaultValue: defaultValue ?? false,
39
+ * onChange,
40
+ * });
41
+ *
42
+ * return (
43
+ * <button onClick={() => setOn((prev) => !prev)}>
44
+ * {on ? 'ON' : 'OFF'}
45
+ * </button>
46
+ * )
47
+ * }
48
+ */
49
+ declare function useControlledState<T>({ value: valueProp, defaultValue, onChange, equalityFn, }: UseControlledStateProps<T>): [T, Dispatch<SetStateAction<T>>];
50
+
51
+ export { useControlledState };
@@ -0,0 +1,29 @@
1
+ // src/hooks/useControlledState/useControlledState.ts
2
+ import { useCallback, useState } from "react";
3
+ function useControlledState({
4
+ value: valueProp,
5
+ defaultValue,
6
+ onChange,
7
+ equalityFn = Object.is
8
+ }) {
9
+ const [uncontrolledState, setUncontrolledState] = useState(defaultValue);
10
+ const controlled = valueProp !== void 0;
11
+ const value = controlled ? valueProp : uncontrolledState;
12
+ const setValue = useCallback(
13
+ (next) => {
14
+ const nextValue = isSetStateAction(next) ? next(value) : next;
15
+ if (equalityFn(value, nextValue) === true) return;
16
+ if (controlled === false) setUncontrolledState(nextValue);
17
+ if (controlled === true && nextValue === void 0) setUncontrolledState(nextValue);
18
+ onChange?.(nextValue);
19
+ },
20
+ [controlled, onChange, equalityFn, value]
21
+ );
22
+ return [value, setValue];
23
+ }
24
+ function isSetStateAction(next) {
25
+ return typeof next === "function";
26
+ }
27
+ export {
28
+ useControlledState
29
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-simplikit",
3
- "version": "0.0.23",
3
+ "version": "0.0.24",
4
4
  "main": "./dist/index.cjs",
5
5
  "type": "module",
6
6
  "sideEffects": false,