@gridui/number-field 0.0.0 → 1.0.2

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.
package/dist/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // src/number-field/number-field.tsx
2
2
  import { Button } from "@gridui/button";
3
+ import { useFormReset } from "@gridui/form";
3
4
  import { controlVariants } from "@gridui/input";
4
5
  import { cn } from "@gridui/style-utils";
5
6
  import { Minus, Plus } from "lucide-react";
6
- import { useEffect, useImperativeHandle, useRef, useState } from "react";
7
+ import { useImperativeHandle, useRef, useState } from "react";
7
8
  import { jsx, jsxs } from "react/jsx-runtime";
8
9
  var NumberField = (props) => {
9
10
  const {
@@ -34,28 +35,11 @@ var NumberField = (props) => {
34
35
  useImperativeHandle(ref, () => {
35
36
  return input.current;
36
37
  });
37
- useEffect(() => {
38
- const form = input.current?.form;
39
- if (!form || value !== void 0) {
40
- return;
38
+ useFormReset(input, () => {
39
+ if (value === void 0) {
40
+ setLocal(initial.current);
41
41
  }
42
- let mounted = true;
43
- const reset = (event) => {
44
- setTimeout(() => {
45
- if (mounted && !event.defaultPrevented) {
46
- setLocal(initial.current);
47
- }
48
- }, 0);
49
- };
50
- form.addEventListener("reset", reset);
51
- return () => {
52
- mounted = false;
53
- form.removeEventListener("reset", reset);
54
- };
55
- }, [
56
- restProps.form,
57
- value
58
- ]);
42
+ }, restProps.form);
59
43
  const change = (next) => {
60
44
  if (restProps.readOnly || restProps.disabled || input.current?.matches(":disabled")) {
61
45
  return;
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/number-field/number-field.tsx"],
4
- "sourcesContent": ["/**\n * @author WMXPY\n * @package Number Field\n * @namespace NumberField\n * @description Number Field\n */\n\nimport { Button } from \"@gridui/button\";\nimport { controlVariants } from \"@gridui/input\";\nimport { cn } from \"@gridui/style-utils\";\nimport { Minus, Plus } from \"lucide-react\";\nimport { type ComponentPropsWithRef, type FC, useEffect, useImperativeHandle, useRef, useState } from \"react\";\n\nexport type NumberFieldProps = Omit<ComponentPropsWithRef<\"input\">, \"type\" | \"value\" | \"defaultValue\" | \"min\" | \"max\" | \"step\" | \"size\"> & {\n readonly value?: number | null;\n readonly defaultValue?: number | null;\n readonly min?: number;\n readonly max?: number;\n readonly step?: number;\n readonly controlSize?: \"standard\" | \"small\";\n readonly incrementLabel: string;\n readonly decrementLabel: string;\n readonly onValueChange?: (value: number | null) => void;\n};\n\nexport const NumberField: FC<NumberFieldProps> = (props: NumberFieldProps) => {\n const { value, defaultValue = null, min, max, step = 1, controlSize = \"standard\", incrementLabel, decrementLabel,\n onValueChange, onChange, className, ref, ...restProps } = props;\n if (!Number.isFinite(step) || step <= 0 || (min !== undefined && !Number.isFinite(min)) || (max !== undefined && !Number.isFinite(max))\n || (min !== undefined && max !== undefined && min > max)\n || (value !== undefined && value !== null && !Number.isFinite(value))\n || (defaultValue !== null && !Number.isFinite(defaultValue))) {\n throw new RangeError(\"NumberField requires finite values and bounds, an ordered range, and a positive step.\");\n }\n const [\n local,\n setLocal,\n ] = useState<number | null>(defaultValue);\n const input = useRef<HTMLInputElement>(null);\n const initial = useRef(defaultValue);\n const current = value === undefined ? local : value;\n useImperativeHandle(ref, () => {\n return input.current as HTMLInputElement;\n });\n useEffect(() => {\n const form = input.current?.form;\n if (!form || value !== undefined) {\n return;\n }\n let mounted = true;\n const reset = (event: Event) => {\n // Wait until React bubbling handlers can cancel the native reset.\n setTimeout(() => {\n if (mounted && !event.defaultPrevented) {\n setLocal(initial.current);\n }\n }, 0);\n };\n form.addEventListener(\"reset\", reset);\n return () => {\n mounted = false;\n form.removeEventListener(\"reset\", reset);\n };\n }, [\n restProps.form,\n value,\n ]);\n const change = (next: number | null) => {\n if (restProps.readOnly || restProps.disabled || input.current?.matches(\":disabled\")) {\n return;\n }\n if (value === undefined) {\n setLocal(next);\n }\n if (next !== current) {\n onValueChange?.(next);\n }\n };\n const move = (direction: number) => {\n const element = input.current;\n if (!element || restProps.readOnly || element.matches(\":disabled\")) {\n return;\n }\n element.stepUp(direction);\n const next = element.valueAsNumber;\n change(Number.isFinite(next) ? next : null);\n // A controlled consumer can reject the requested value.\n if (value !== undefined) {\n element.value = current === null ? \"\" : String(current);\n }\n element.focus();\n };\n return (<div\n className=\"flex min-w-0 items-stretch gap-gridui-4\"\n dir={restProps.dir}\n >\n <Button\n variant=\"secondary\"\n layout=\"icon\"\n size={controlSize}\n aria-label={decrementLabel}\n disabled={restProps.disabled || restProps.readOnly || (current !== null && min !== undefined && current <= min)}\n onClick={() => {\n move(-1);\n }}\n >\n <Minus\n aria-hidden=\"true\"\n />\n </Button>\n <input\n {...restProps}\n ref={input}\n type=\"number\"\n min={min}\n max={max}\n step={step}\n value={current ?? \"\"}\n className={cn(controlVariants({ size: controlSize }), \"gridui-number-field flex-1\", className)}\n onChange={(event) => {\n onChange?.(event);\n if (!event.defaultPrevented) {\n change(Number.isFinite(event.currentTarget.valueAsNumber) ? event.currentTarget.valueAsNumber : null);\n }\n }}\n />\n <Button\n variant=\"secondary\"\n layout=\"icon\"\n size={controlSize}\n aria-label={incrementLabel}\n disabled={restProps.disabled || restProps.readOnly || (current !== null && max !== undefined && current >= max)}\n onClick={() => {\n move(1);\n }}\n >\n <Plus\n aria-hidden=\"true\"\n />\n </Button>\n </div>);\n};\n"],
5
- "mappings": ";AAOA,SAAS,cAAc;AACvB,SAAS,uBAAuB;AAChC,SAAS,UAAU;AACnB,SAAS,OAAO,YAAY;AAC5B,SAA8C,WAAW,qBAAqB,QAAQ,gBAAgB;AAiF1F,SAcA,KAdA;AAnEL,IAAM,cAAoC,CAAC,UAA4B;AAC1E,QAAM;AAAA,IAAE;AAAA,IAAO,eAAe;AAAA,IAAM;AAAA,IAAK;AAAA,IAAK,OAAO;AAAA,IAAG,cAAc;AAAA,IAAY;AAAA,IAAgB;AAAA,IAC9F;AAAA,IAAe;AAAA,IAAU;AAAA,IAAW;AAAA,IAAK,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YACG,SAAQ;AAAA,YACR,QAAO;AAAA,YACP,MAAM;AAAA,YACN,cAAY;AAAA,YACZ,UAAU,UAAU,YAAY,UAAU,YAAa,YAAY,QAAQ,QAAQ,UAAa,WAAW;AAAA,YAC3G,SAAS,MAAM;AACX,mBAAK,CAAC;AAAA,YACV;AAAA,YAEA;AAAA,cAAC;AAAA;AAAA,gBACG,eAAY;AAAA;AAAA,YAChB;AAAA;AAAA,QACJ;AAAA;AAAA;AAAA,EACJ;AACJ;",
4
+ "sourcesContent": ["/**\n * @author WMXPY\n * @package Number Field\n * @namespace NumberField\n * @description Number Field\n */\n\nimport { Button } from \"@gridui/button\";\nimport { useFormReset } from \"@gridui/form\";\nimport { controlVariants } from \"@gridui/input\";\nimport { cn } from \"@gridui/style-utils\";\nimport { Minus, Plus } from \"lucide-react\";\nimport { type ComponentPropsWithRef, type FC, useImperativeHandle, useRef, useState } from \"react\";\n\nexport type NumberFieldProps = Omit<ComponentPropsWithRef<\"input\">, \"type\" | \"value\" | \"defaultValue\" | \"min\" | \"max\" | \"step\" | \"size\"> & {\n readonly value?: number | null;\n readonly defaultValue?: number | null;\n readonly min?: number;\n readonly max?: number;\n readonly step?: number;\n readonly controlSize?: \"standard\" | \"small\";\n readonly incrementLabel: string;\n readonly decrementLabel: string;\n readonly onValueChange?: (value: number | null) => void;\n};\n\nexport const NumberField: FC<NumberFieldProps> = (props: NumberFieldProps) => {\n const { value, defaultValue = null, min, max, step = 1, controlSize = \"standard\", incrementLabel, decrementLabel,\n onValueChange, onChange, className, ref, ...restProps } = props;\n if (!Number.isFinite(step) || step <= 0 || (min !== undefined && !Number.isFinite(min)) || (max !== undefined && !Number.isFinite(max))\n || (min !== undefined && max !== undefined && min > max)\n || (value !== undefined && value !== null && !Number.isFinite(value))\n || (defaultValue !== null && !Number.isFinite(defaultValue))) {\n throw new RangeError(\"NumberField requires finite values and bounds, an ordered range, and a positive step.\");\n }\n const [\n local,\n setLocal,\n ] = useState<number | null>(defaultValue);\n const input = useRef<HTMLInputElement>(null);\n const initial = useRef(defaultValue);\n const current = value === undefined ? local : value;\n useImperativeHandle(ref, () => {\n return input.current as HTMLInputElement;\n });\n useFormReset(input, () => {\n if (value === undefined) {\n setLocal(initial.current);\n }\n }, restProps.form);\n const change = (next: number | null) => {\n if (restProps.readOnly || restProps.disabled || input.current?.matches(\":disabled\")) {\n return;\n }\n if (value === undefined) {\n setLocal(next);\n }\n if (next !== current) {\n onValueChange?.(next);\n }\n };\n const move = (direction: number) => {\n const element = input.current;\n if (!element || restProps.readOnly || element.matches(\":disabled\")) {\n return;\n }\n element.stepUp(direction);\n const next = element.valueAsNumber;\n change(Number.isFinite(next) ? next : null);\n // A controlled consumer can reject the requested value.\n if (value !== undefined) {\n element.value = current === null ? \"\" : String(current);\n }\n element.focus();\n };\n return (<div\n className=\"flex min-w-0 items-stretch gap-gridui-4\"\n dir={restProps.dir}\n >\n <Button\n variant=\"secondary\"\n layout=\"icon\"\n size={controlSize}\n aria-label={decrementLabel}\n disabled={restProps.disabled || restProps.readOnly || (current !== null && min !== undefined && current <= min)}\n onClick={() => {\n move(-1);\n }}\n >\n <Minus\n aria-hidden=\"true\"\n />\n </Button>\n <input\n {...restProps}\n ref={input}\n type=\"number\"\n min={min}\n max={max}\n step={step}\n value={current ?? \"\"}\n className={cn(controlVariants({ size: controlSize }), \"gridui-number-field flex-1\", className)}\n onChange={(event) => {\n onChange?.(event);\n if (!event.defaultPrevented) {\n change(Number.isFinite(event.currentTarget.valueAsNumber) ? event.currentTarget.valueAsNumber : null);\n }\n }}\n />\n <Button\n variant=\"secondary\"\n layout=\"icon\"\n size={controlSize}\n aria-label={incrementLabel}\n disabled={restProps.disabled || restProps.readOnly || (current !== null && max !== undefined && current >= max)}\n onClick={() => {\n move(1);\n }}\n >\n <Plus\n aria-hidden=\"true\"\n />\n </Button>\n </div>);\n};\n"],
5
+ "mappings": ";AAOA,SAAS,cAAc;AACvB,SAAS,oBAAoB;AAC7B,SAAS,uBAAuB;AACh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,YACR,QAAO;AAAA,YACP,MAAM;AAAA,YACN,cAAY;AAAA,YACZ,UAAU,UAAU,YAAY,UAAU,YAAa,YAAY,QAAQ,QAAQ,UAAa,WAAW;AAAA,YAC3G,SAAS,MAAM;AACX,mBAAK,CAAC;AAAA,YACV;AAAA,YAEA;AAAA,cAAC;AAAA;AAAA,gBACG,eAAY;AAAA;AAAA,YAChB;AAAA;AAAA,QACJ;AAAA;AAAA;AAAA,EACJ;AACJ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gridui/number-field",
3
- "version": "0.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "GridUI React number field",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",
@@ -26,17 +26,18 @@
26
26
  "react-dom": "^19.2.0"
27
27
  },
28
28
  "dependencies": {
29
- "@gridui/button": "^2.1.1",
30
- "@gridui/input": "^0.0.0",
31
- "@gridui/style-utils": "^2.1.2",
32
- "@gridui/theme": "^2.1.2",
33
- "lucide-react": "1.62.0"
29
+ "@gridui/button": "^2.1.2",
30
+ "@gridui/form": "^2.0.1",
31
+ "@gridui/input": "^1.1.1",
32
+ "@gridui/style-utils": "^2.1.3",
33
+ "@gridui/theme": "^2.2.1",
34
+ "lucide-react": "1.63.0"
34
35
  },
35
36
  "devDependencies": {
36
- "@gridui-internal/configuration": "0.0.0",
37
+ "@gridui-internal/configuration": "0.0.0-internal",
37
38
  "@jest/globals": "30.5.2",
38
39
  "@types/jest": "30.0.0",
39
- "@types/node": "26.6.5",
40
+ "@types/node": "26.6.6",
40
41
  "@types/react": "19.3.0",
41
42
  "@types/react-dom": "19.3.0",
42
43
  "jest": "30.5.2",