ar-design 0.3.0 → 0.3.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.
@@ -64,8 +64,8 @@ const Input = forwardRef(({ variant = "outlined", color = "light", size = "norma
64
64
  (() => {
65
65
  if (attributes.onChange) {
66
66
  // Mevcut değeri alın
67
- const _target = event.target;
68
- const currentValue = upperCase ? convertToUpperCase(_target.value) : _target.value;
67
+ const { value } = event.target;
68
+ const currentValue = upperCase ? convertToUpperCase(value) : value;
69
69
  // Yeni değeri oluşturun ve onChange fonksiyonunu çağırın
70
70
  const newValue = `${addon?.before ?? ""}${currentValue}${addon?.after ?? ""}`;
71
71
  attributes.onChange({
@@ -1,4 +1,4 @@
1
- import { IBorder, IColors, ISize, IUpperCase, IValidation, IVariant } from "../../../libs/types/IGlobalProps";
2
- interface IProps extends IVariant, IColors, IBorder, ISize, IUpperCase, IValidation, Omit<React.InputHTMLAttributes<HTMLInputElement>, "children" | "size" | "color" | "type"> {
1
+ import { IBorder, IColors, IUpperCase, IValidation, IVariant } from "../../../libs/types/IGlobalProps";
2
+ interface IProps extends IVariant, IColors, IBorder, IUpperCase, IValidation, Omit<React.InputHTMLAttributes<HTMLInputElement>, "children" | "size" | "color" | "type"> {
3
3
  }
4
4
  export default IProps;
@@ -1,15 +1,20 @@
1
1
  "use client";
2
- import React, { useMemo, useRef, useState } from "react";
2
+ import React, { useEffect, useMemo, useRef, useState } from "react";
3
3
  import Input from "../input";
4
4
  const InputNumber = ({ ...attributes }) => {
5
5
  // refs
6
6
  const _input = useRef(null);
7
7
  const _caretPosition = useRef(null);
8
+ const _isInputTouch = useRef(false);
8
9
  // states
9
- const [value, setValue] = useState(null);
10
+ const [value, setValue] = useState("");
10
11
  // methods
11
12
  const handleChange = (event) => {
12
13
  let { value } = event.target;
14
+ const caret = _input.current?.selectionStart ?? 0;
15
+ _caretPosition.current = caret;
16
+ if (value.length === 0)
17
+ _isInputTouch.current = false;
13
18
  // Temizle.
14
19
  const cleanedValue = (value = value.replace(/[^0-9,]/g, ""));
15
20
  // Numara olarak çevir.
@@ -18,21 +23,70 @@ const InputNumber = ({ ...attributes }) => {
18
23
  const numberValue = isNaN(parsed) ? 0 : parsed;
19
24
  // Formatla ve Kullanıcı , (virgül) girdiyse kuruş göster.
20
25
  const isDecimals = cleanedValue.includes(",");
21
- const formatted = numberValue === 0 && cleanedValue === "" ? "" : formatter(isDecimals).format(numberValue);
26
+ let formatted = numberValue === 0 && cleanedValue === "" ? "" : formatter(isDecimals).format(numberValue);
27
+ // if (isDecimals) {
28
+ // const [_, digits] = formatted.split(",");
29
+ // if (digits && digits.length > 2) formatted = formatted.slice(0, -1);
30
+ // }
22
31
  setValue(formatted);
32
+ setTimeout(() => {
33
+ requestAnimationFrame(() => {
34
+ if (!_input.current || _caretPosition.current == null)
35
+ return;
36
+ if (isDecimals)
37
+ _isInputTouch.current = false;
38
+ _caretPosition.current += formatted.length > value.length && !_isInputTouch.current ? 1 : 0;
39
+ _input.current?.setSelectionRange(_caretPosition.current, _caretPosition.current);
40
+ });
41
+ }, 0);
42
+ };
43
+ const handleClick = () => {
44
+ const caret = _input.current?.selectionStart ?? 0;
45
+ _caretPosition.current = caret;
46
+ if (_input.current && _input.current.value.length > 3)
47
+ _isInputTouch.current = true;
23
48
  };
24
49
  const handleKeyUp = (event) => {
25
- const caretPosition = event.currentTarget.selectionStart ?? 0;
26
- _caretPosition.current = caretPosition;
50
+ if (!["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"].includes(event.code))
51
+ return;
52
+ if (_input.current && _input.current.value.length > 3)
53
+ _isInputTouch.current = true;
27
54
  };
28
55
  const formatter = useMemo(() => {
29
56
  return (isDecimals) => new Intl.NumberFormat("tr-TR", {
30
- style: "currency",
31
- currency: "TRY",
32
- minimumFractionDigits: isDecimals ? 2 : 0,
33
- maximumFractionDigits: 2,
57
+ style: "decimal",
58
+ minimumFractionDigits: 0,
59
+ maximumFractionDigits: isDecimals ? 2 : 0,
34
60
  });
35
61
  }, []);
36
- return (React.createElement(Input, { ref: _input, ...attributes, value: value ?? "", onChange: handleChange, onKeyUp: handleKeyUp, type: "text", inputMode: "decimal" }));
62
+ // useEffects
63
+ useEffect(() => {
64
+ if (attributes.value !== undefined)
65
+ setValue(attributes.value ?? "");
66
+ }, [attributes.value]);
67
+ return (React.createElement(Input, { ref: _input, ...attributes, value: value ?? attributes.value, onChange: (event) => {
68
+ // Disabled gelmesi durumunda işlem yapmasına izin verme...
69
+ if (attributes.disabled)
70
+ return;
71
+ (() => {
72
+ handleChange(event);
73
+ })();
74
+ (() => {
75
+ if (attributes.onChange) {
76
+ const { value } = event.target;
77
+ const newValue = value.replaceAll(".", "");
78
+ attributes.onChange({
79
+ ...event,
80
+ target: {
81
+ ...event.target,
82
+ id: event.target.id,
83
+ name: event.target.name,
84
+ value: newValue,
85
+ type: event.target.type,
86
+ },
87
+ });
88
+ }
89
+ })();
90
+ }, onClick: handleClick, onKeyUp: handleKeyUp, type: "text", inputMode: "decimal" }));
37
91
  };
38
92
  export default InputNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ar-design",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",