@xanui/ui 1.1.63 → 1.1.64

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.
@@ -9,41 +9,45 @@ var core = require('@xanui/core');
9
9
 
10
10
  const InputNumber = React.forwardRef((props, ref) => {
11
11
  var _a;
12
- const [_val, setVal] = React.useState((_a = props.value) !== null && _a !== void 0 ? _a : "");
13
12
  const inputRef = React.useRef(null);
14
13
  const mergeRef = core.useMergeRefs(inputRef, ref);
15
- React.useEffect(() => {
16
- if (inputRef.current && (props === null || props === void 0 ? void 0 : props.onChange)) {
17
- let valstr = String(_val);
18
- inputRef.current.value = valstr.includes(".") ? parseFloat(_val) : parseInt(_val);
19
- const syntheticEvent = {
20
- target: inputRef.current,
21
- currentTarget: inputRef.current,
22
- };
23
- props === null || props === void 0 ? void 0 : props.onChange(syntheticEvent);
24
- }
25
- }, [_val]);
26
- const isNumeric = _val === undefined || _val === '' || !isNaN(Number(_val));
14
+ const isNumeric = props.value === undefined || props.value === '' || !isNaN(Number(props.value));
27
15
  const errorProps = {};
28
16
  if (!isNumeric) {
29
17
  errorProps.error = true;
30
18
  errorProps.helperText = "Value must be numeric";
31
19
  }
32
- return (jsxRuntime.jsx(index, Object.assign({}, props, errorProps, { ref: mergeRef, endIcon: jsxRuntime.jsx(UnfoldMore, {}), value: _val !== null && _val !== void 0 ? _val : "", onKeyDown: (e) => {
20
+ return (jsxRuntime.jsx(index, Object.assign({}, props, errorProps, { ref: mergeRef, endIcon: jsxRuntime.jsx(UnfoldMore, {}), value: (_a = props.value) !== null && _a !== void 0 ? _a : "", onKeyDown: (e) => {
33
21
  var _a;
34
22
  (_a = props.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(props, e);
35
23
  if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown')
36
24
  return;
37
25
  e.preventDefault();
38
- const current = parseFloat(_val) || 0;
39
- setVal(e.key === 'ArrowUp' ? current + 1 : current - 1);
26
+ const current = parseFloat(props.value) || 0;
27
+ e.target.value = e.key === 'ArrowUp' ? current + 1 : current - 1;
28
+ (props === null || props === void 0 ? void 0 : props.onChange) && (props === null || props === void 0 ? void 0 : props.onChange(e));
29
+ }, onBlur: (e) => {
30
+ var _a;
31
+ const raw = e.target.value;
32
+ if (raw === "" || raw === ".")
33
+ return;
34
+ const num = parseFloat(raw);
35
+ if (!Number.isNaN(num)) {
36
+ (_a = props === null || props === void 0 ? void 0 : props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, Object.assign(Object.assign({}, e), { target: Object.assign(Object.assign({}, e.target), { value: num }) }));
37
+ }
40
38
  }, onChange: (e) => {
41
- let val = e.target.value.replace(/[^0-9.]/g, '');
42
- const parts = val.split('.');
43
- if (parts.length > 2) {
44
- val = parts[0] + '.' + parts.slice(1).join('');
39
+ var _a, _b;
40
+ let value = e.target.value;
41
+ if (value === "") {
42
+ (_a = props === null || props === void 0 ? void 0 : props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, e);
43
+ return;
45
44
  }
46
- setVal(val === "." ? `0.` : val);
45
+ if (!/^\d*\.?\d*$/.test(value))
46
+ return;
47
+ if (value === ".")
48
+ value = "0.";
49
+ const nextEvent = Object.assign(Object.assign({}, e), { target: Object.assign(Object.assign({}, e.target), { value }) });
50
+ (_b = props === null || props === void 0 ? void 0 : props.onChange) === null || _b === void 0 ? void 0 : _b.call(props, nextEvent);
47
51
  } })));
48
52
  });
49
53
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../src/InputNumber/index.tsx"],"sourcesContent":["\"use client\"\nimport React, { useEffect, useRef, useState } from 'react'\nimport Input, { InputProps } from '../Input'\nimport UnfoldMore from '@xanui/icons/UnfoldMore'\nimport { useMergeRefs } from '@xanui/core'\n\nexport type InputNumberProps = Omit<InputProps, \"value\"> & {\n value?: number; // allow string so typing \".\" works\n}\n\nconst InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {\n const [_val, setVal] = useState<string>(props.value as any ?? \"\")\n const inputRef = useRef<any>(null)\n const mergeRef = useMergeRefs(inputRef, ref)\n useEffect(() => {\n if (inputRef.current && props?.onChange) {\n let valstr = String(_val)\n inputRef.current.value = valstr.includes(\".\") ? parseFloat(_val) : parseInt(_val)\n const syntheticEvent = {\n target: inputRef.current,\n currentTarget: inputRef.current,\n } as unknown as React.ChangeEvent<HTMLInputElement>;\n props?.onChange(syntheticEvent)\n }\n }, [_val])\n\n const isNumeric = _val === undefined || _val === '' || !isNaN(Number(_val));\n const errorProps: Partial<InputProps> = {};\n if (!isNumeric) {\n errorProps.error = true;\n errorProps.helperText = \"Value must be numeric\";\n }\n\n return (\n <Input\n {...props}\n {...errorProps}\n ref={mergeRef}\n endIcon={<UnfoldMore />}\n value={_val ?? \"\" as any}\n onKeyDown={(e: any) => {\n props.onKeyDown?.(e);\n\n if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;\n\n e.preventDefault();\n const current = parseFloat(_val as any) || 0;\n setVal(e.key === 'ArrowUp' ? current + 1 : current - 1 as any);\n }}\n onChange={(e) => {\n let val: any = e.target.value.replace(/[^0-9.]/g, '')\n const parts = val.split('.');\n if (parts.length > 2) {\n val = parts[0] + '.' + parts.slice(1).join('')\n }\n setVal(val === \".\" ? `0.` : val);\n }}\n />\n );\n});\n\nexport default InputNumber;\n"],"names":[],"mappings":";;;;;;;;;AAUA;;AACG;AACA;;;AAGG;AACG;;AAEA;;;;;;AAMN;AAEA;;;AAGG;AACA;;AAGH;;AAQS;;;;;AAMA;AACH;AAEG;;AAEA;AACG;;AAEH;;AAIZ;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../src/InputNumber/index.tsx"],"sourcesContent":["\"use client\"\nimport React, { useRef } from 'react'\nimport Input, { InputProps } from '../Input'\nimport UnfoldMore from '@xanui/icons/UnfoldMore'\nimport { useMergeRefs } from '@xanui/core'\n\nexport type InputNumberProps = Omit<InputProps, \"value\"> & {\n value?: number | \"\";\n}\n\nconst InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {\n const inputRef = useRef<any>(null)\n const mergeRef = useMergeRefs(inputRef, ref)\n const isNumeric = props.value === undefined || props.value === '' || !isNaN(Number(props.value));\n const errorProps: Partial<InputProps> = {};\n if (!isNumeric) {\n errorProps.error = true;\n errorProps.helperText = \"Value must be numeric\";\n }\n\n return (\n <Input\n {...props}\n {...errorProps}\n ref={mergeRef}\n endIcon={<UnfoldMore />}\n value={props.value ?? \"\" as any}\n onKeyDown={(e: any) => {\n props.onKeyDown?.(e);\n if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;\n e.preventDefault();\n const current = parseFloat(props.value as any) || 0;\n e.target.value = e.key === 'ArrowUp' ? current + 1 : current - 1 as any\n props?.onChange && props?.onChange(e)\n\n }}\n onBlur={(e) => {\n const raw = e.target.value\n if (raw === \"\" || raw === \".\") return\n const num = parseFloat(raw)\n if (!Number.isNaN(num)) {\n props?.onChange?.({\n ...e,\n target: { ...e.target, value: num },\n } as any)\n }\n }}\n\n onChange={(e) => {\n let value = e.target.value\n if (value === \"\") {\n props?.onChange?.(e)\n return\n }\n\n if (!/^\\d*\\.?\\d*$/.test(value)) return\n\n if (value === \".\") value = \"0.\"\n const nextEvent = {\n ...e,\n target: {\n ...e.target,\n value,\n },\n }\n\n props?.onChange?.(nextEvent as any)\n }}\n\n />\n );\n});\n\nexport default InputNumber;\n"],"names":[],"mappings":";;;;;;;;;AAUA;;AACG;;;;;AAKG;AACA;;AAGH;;AAQS;;;;;;AAKA;AAEH;;AAEG;AACA;;AACA;;;;AAOH;;AAGG;AACA;;;;AAKA;;;;AAGA;;;AAaZ;;"}
@@ -2,7 +2,7 @@ import React from 'react';
2
2
  import { InputProps } from '../Input/index.js';
3
3
 
4
4
  type InputNumberProps = Omit<InputProps, "value"> & {
5
- value?: number;
5
+ value?: number | "";
6
6
  };
7
7
  declare const InputNumber: React.ForwardRefExoticComponent<Omit<InputNumberProps, "ref"> & React.RefAttributes<HTMLInputElement>>;
8
8
 
@@ -1,47 +1,51 @@
1
1
  "use client";
2
2
  import { jsx } from 'react/jsx-runtime';
3
- import React, { useState, useRef, useEffect } from 'react';
3
+ import React, { useRef } from 'react';
4
4
  import Input from '../Input/index.js';
5
5
  import UnfoldMore from '@xanui/icons/UnfoldMore';
6
6
  import { useMergeRefs } from '@xanui/core';
7
7
 
8
8
  const InputNumber = React.forwardRef((props, ref) => {
9
9
  var _a;
10
- const [_val, setVal] = useState((_a = props.value) !== null && _a !== void 0 ? _a : "");
11
10
  const inputRef = useRef(null);
12
11
  const mergeRef = useMergeRefs(inputRef, ref);
13
- useEffect(() => {
14
- if (inputRef.current && (props === null || props === void 0 ? void 0 : props.onChange)) {
15
- let valstr = String(_val);
16
- inputRef.current.value = valstr.includes(".") ? parseFloat(_val) : parseInt(_val);
17
- const syntheticEvent = {
18
- target: inputRef.current,
19
- currentTarget: inputRef.current,
20
- };
21
- props === null || props === void 0 ? void 0 : props.onChange(syntheticEvent);
22
- }
23
- }, [_val]);
24
- const isNumeric = _val === undefined || _val === '' || !isNaN(Number(_val));
12
+ const isNumeric = props.value === undefined || props.value === '' || !isNaN(Number(props.value));
25
13
  const errorProps = {};
26
14
  if (!isNumeric) {
27
15
  errorProps.error = true;
28
16
  errorProps.helperText = "Value must be numeric";
29
17
  }
30
- return (jsx(Input, Object.assign({}, props, errorProps, { ref: mergeRef, endIcon: jsx(UnfoldMore, {}), value: _val !== null && _val !== void 0 ? _val : "", onKeyDown: (e) => {
18
+ return (jsx(Input, Object.assign({}, props, errorProps, { ref: mergeRef, endIcon: jsx(UnfoldMore, {}), value: (_a = props.value) !== null && _a !== void 0 ? _a : "", onKeyDown: (e) => {
31
19
  var _a;
32
20
  (_a = props.onKeyDown) === null || _a === void 0 ? void 0 : _a.call(props, e);
33
21
  if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown')
34
22
  return;
35
23
  e.preventDefault();
36
- const current = parseFloat(_val) || 0;
37
- setVal(e.key === 'ArrowUp' ? current + 1 : current - 1);
24
+ const current = parseFloat(props.value) || 0;
25
+ e.target.value = e.key === 'ArrowUp' ? current + 1 : current - 1;
26
+ (props === null || props === void 0 ? void 0 : props.onChange) && (props === null || props === void 0 ? void 0 : props.onChange(e));
27
+ }, onBlur: (e) => {
28
+ var _a;
29
+ const raw = e.target.value;
30
+ if (raw === "" || raw === ".")
31
+ return;
32
+ const num = parseFloat(raw);
33
+ if (!Number.isNaN(num)) {
34
+ (_a = props === null || props === void 0 ? void 0 : props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, Object.assign(Object.assign({}, e), { target: Object.assign(Object.assign({}, e.target), { value: num }) }));
35
+ }
38
36
  }, onChange: (e) => {
39
- let val = e.target.value.replace(/[^0-9.]/g, '');
40
- const parts = val.split('.');
41
- if (parts.length > 2) {
42
- val = parts[0] + '.' + parts.slice(1).join('');
37
+ var _a, _b;
38
+ let value = e.target.value;
39
+ if (value === "") {
40
+ (_a = props === null || props === void 0 ? void 0 : props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, e);
41
+ return;
43
42
  }
44
- setVal(val === "." ? `0.` : val);
43
+ if (!/^\d*\.?\d*$/.test(value))
44
+ return;
45
+ if (value === ".")
46
+ value = "0.";
47
+ const nextEvent = Object.assign(Object.assign({}, e), { target: Object.assign(Object.assign({}, e.target), { value }) });
48
+ (_b = props === null || props === void 0 ? void 0 : props.onChange) === null || _b === void 0 ? void 0 : _b.call(props, nextEvent);
45
49
  } })));
46
50
  });
47
51
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/InputNumber/index.tsx"],"sourcesContent":["\"use client\"\nimport React, { useEffect, useRef, useState } from 'react'\nimport Input, { InputProps } from '../Input'\nimport UnfoldMore from '@xanui/icons/UnfoldMore'\nimport { useMergeRefs } from '@xanui/core'\n\nexport type InputNumberProps = Omit<InputProps, \"value\"> & {\n value?: number; // allow string so typing \".\" works\n}\n\nconst InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {\n const [_val, setVal] = useState<string>(props.value as any ?? \"\")\n const inputRef = useRef<any>(null)\n const mergeRef = useMergeRefs(inputRef, ref)\n useEffect(() => {\n if (inputRef.current && props?.onChange) {\n let valstr = String(_val)\n inputRef.current.value = valstr.includes(\".\") ? parseFloat(_val) : parseInt(_val)\n const syntheticEvent = {\n target: inputRef.current,\n currentTarget: inputRef.current,\n } as unknown as React.ChangeEvent<HTMLInputElement>;\n props?.onChange(syntheticEvent)\n }\n }, [_val])\n\n const isNumeric = _val === undefined || _val === '' || !isNaN(Number(_val));\n const errorProps: Partial<InputProps> = {};\n if (!isNumeric) {\n errorProps.error = true;\n errorProps.helperText = \"Value must be numeric\";\n }\n\n return (\n <Input\n {...props}\n {...errorProps}\n ref={mergeRef}\n endIcon={<UnfoldMore />}\n value={_val ?? \"\" as any}\n onKeyDown={(e: any) => {\n props.onKeyDown?.(e);\n\n if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;\n\n e.preventDefault();\n const current = parseFloat(_val as any) || 0;\n setVal(e.key === 'ArrowUp' ? current + 1 : current - 1 as any);\n }}\n onChange={(e) => {\n let val: any = e.target.value.replace(/[^0-9.]/g, '')\n const parts = val.split('.');\n if (parts.length > 2) {\n val = parts[0] + '.' + parts.slice(1).join('')\n }\n setVal(val === \".\" ? `0.` : val);\n }}\n />\n );\n});\n\nexport default InputNumber;\n"],"names":[],"mappings":";;;;;;;AAUA;;AACG;AACA;;;AAGG;AACG;;AAEA;;;;;;AAMN;AAEA;;;AAGG;AACA;;AAGH;;AAQS;;;;;AAMA;AACH;AAEG;;AAEA;AACG;;AAEH;;AAIZ;;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/InputNumber/index.tsx"],"sourcesContent":["\"use client\"\nimport React, { useRef } from 'react'\nimport Input, { InputProps } from '../Input'\nimport UnfoldMore from '@xanui/icons/UnfoldMore'\nimport { useMergeRefs } from '@xanui/core'\n\nexport type InputNumberProps = Omit<InputProps, \"value\"> & {\n value?: number | \"\";\n}\n\nconst InputNumber = React.forwardRef<HTMLInputElement, InputNumberProps>((props, ref) => {\n const inputRef = useRef<any>(null)\n const mergeRef = useMergeRefs(inputRef, ref)\n const isNumeric = props.value === undefined || props.value === '' || !isNaN(Number(props.value));\n const errorProps: Partial<InputProps> = {};\n if (!isNumeric) {\n errorProps.error = true;\n errorProps.helperText = \"Value must be numeric\";\n }\n\n return (\n <Input\n {...props}\n {...errorProps}\n ref={mergeRef}\n endIcon={<UnfoldMore />}\n value={props.value ?? \"\" as any}\n onKeyDown={(e: any) => {\n props.onKeyDown?.(e);\n if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return;\n e.preventDefault();\n const current = parseFloat(props.value as any) || 0;\n e.target.value = e.key === 'ArrowUp' ? current + 1 : current - 1 as any\n props?.onChange && props?.onChange(e)\n\n }}\n onBlur={(e) => {\n const raw = e.target.value\n if (raw === \"\" || raw === \".\") return\n const num = parseFloat(raw)\n if (!Number.isNaN(num)) {\n props?.onChange?.({\n ...e,\n target: { ...e.target, value: num },\n } as any)\n }\n }}\n\n onChange={(e) => {\n let value = e.target.value\n if (value === \"\") {\n props?.onChange?.(e)\n return\n }\n\n if (!/^\\d*\\.?\\d*$/.test(value)) return\n\n if (value === \".\") value = \"0.\"\n const nextEvent = {\n ...e,\n target: {\n ...e.target,\n value,\n },\n }\n\n props?.onChange?.(nextEvent as any)\n }}\n\n />\n );\n});\n\nexport default InputNumber;\n"],"names":[],"mappings":";;;;;;;AAUA;;AACG;;;;;AAKG;AACA;;AAGH;;AAQS;;;;;;AAKA;AAEH;;AAEG;AACA;;AACA;;;;AAOH;;AAGG;AACA;;;;AAKA;;;;AAGA;;;AAaZ;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xanui/ui",
3
- "version": "1.1.63",
3
+ "version": "1.1.64",
4
4
  "description": "Xanui - A React Component Library",
5
5
  "private": false,
6
6
  "dependencies": {