px-react-ui-components 1.0.27 → 1.0.29

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.
@@ -88,6 +88,9 @@ function MyInput({
88
88
  const [isError, setIsError] = (0, _react.useState)(false);
89
89
  const [isTyping, setIsTyping] = (0, _react.useState)(false);
90
90
  const typingTimeoutRef = (0, _react.useRef)(null);
91
+
92
+ // MONEY input için display value (formatlanmış hali)
93
+ const [displayValue, setDisplayValue] = (0, _react.useState)(null);
91
94
  const toBase64 = file => new Promise((resolve, reject) => {
92
95
  const reader = new FileReader();
93
96
  reader.readAsDataURL(file);
@@ -105,52 +108,34 @@ function MyInput({
105
108
  return `${size.toFixed(2)} ${units[unitIndex]}`;
106
109
  };
107
110
  const moneyFormat = _value => {
108
- let money = '';
109
- let inputValue = _value;
110
- if (inputValue == null || inputValue == undefined || inputValue == '') return money;
111
+ if (_value == null || _value == undefined || _value == '') return '';
111
112
  try {
112
- inputValue = inputValue.toString();
113
- // Tüm virgülleri kaldır ve son girilen karakteri kontrol et
114
- money = inputValue.replace(/,/g, '');
115
- const lastChar = inputValue.slice(-1);
116
- if (lastChar === ',') {
117
- money = money + '.';
118
- }
113
+ let inputValue = _value.toString();
119
114
 
120
- // Sadece sayılar ve tek bir nokta kalacak şekilde temizle
121
- money = money.replace(/[^0-9.]/g, '');
115
+ // Sadece sayılar ve nokta
116
+ inputValue = inputValue.replace(/[^0-9.]/g, '');
122
117
 
123
118
  // Birden fazla nokta varsa ilkini koru
124
- const parts = money.split('.');
119
+ const parts = inputValue.split('.');
125
120
  if (parts.length > 2) {
126
- money = parts[0] + '.' + parts[1];
121
+ inputValue = parts[0] + '.' + parts.slice(1).join('');
127
122
  }
128
123
 
129
- // Noktadan sonra en fazla 2 basamak
124
+ // Noktadan sonra maksimum decimal count
130
125
  if (parts.length === 2 && parts[1].length > decimalCount) {
131
- money = parts[0] + '.' + parts[1].substring(0, decimalCount);
126
+ inputValue = parts[0] + '.' + parts[1].substring(0, decimalCount);
132
127
  }
133
128
 
134
- // Binlik ayracı için formatlama
135
- if (money) {
136
- const numParts = money.split('.');
137
- if (numParts[0]) {
138
- // Sayıyı önce tam sayıya çevir, sonra binlik ayracı ekle
139
- numParts[0] = parseInt(numParts[0], 10).toLocaleString('en-US');
140
- }
141
- money = numParts.join('.');
142
- }
143
- const numParts = money.split('.');
144
- if (numParts[0]) {
145
- numParts[0] = numParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
129
+ // Binlik ayıracı ekle (sadece tam sayı kısmına)
130
+ const finalParts = inputValue.split('.');
131
+ if (finalParts[0]) {
132
+ finalParts[0] = finalParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
146
133
  }
147
- money = numParts.join('.');
134
+ return finalParts.join('.');
148
135
  } catch (error) {
149
- money = '';
150
- console.log(inputValue);
151
- console.error(error);
136
+ console.error('Money format error:', error);
137
+ return '';
152
138
  }
153
- return money;
154
139
  };
155
140
  const onMyBlur = e => {
156
141
  setIsError(false);
@@ -170,48 +155,110 @@ function MyInput({
170
155
  break;
171
156
  }
172
157
  }
173
- e.target.value = myValue;
174
- e.value = myValue;
158
+
159
+ // MONEY input için formatlanmış değeri kullan
160
+ if (type === MyInputType.MONEY) {
161
+ // Çıkışta decimal kısmını düzenle (sadece görsel)
162
+ let formattedValue = myValue;
163
+ if (formattedValue && formattedValue !== '') {
164
+ const numValue = parseFloat(formattedValue);
165
+ if (!isNaN(numValue)) {
166
+ formattedValue = numValue.toFixed(decimalCount);
167
+ // Sadece display value'yu güncelle, myValue'yu değiştirme
168
+ setDisplayValue(formattedValue);
169
+ }
170
+ }
171
+ const finalFormattedValue = moneyFormat(formattedValue);
172
+ e.target.value = finalFormattedValue;
173
+ e.value = finalFormattedValue;
174
+ } else {
175
+ e.target.value = myValue;
176
+ e.value = myValue;
177
+ }
175
178
  if (onBlur != null) onBlur(e);
176
179
  };
177
180
  const onMyFocus = e => {
178
- e.target.value = myValue;
179
- e.value = myValue;
181
+ // MONEY input için formatlanmış değeri kullan
182
+ if (type === MyInputType.MONEY) {
183
+ // Focus'ta displayValue'yu temizle, orijinal değere dön
184
+ setDisplayValue(null);
185
+ e.target.value = moneyFormat(myValue);
186
+ e.value = moneyFormat(myValue);
187
+ } else {
188
+ e.target.value = myValue;
189
+ e.value = myValue;
190
+ }
180
191
  if (onFocus != null) onFocus(e);
181
192
  };
182
193
  const onMyKeyDown = e => {
183
- e.target.value = myValue;
184
- e.value = myValue;
194
+ // MONEY input için formatlanmış değeri kullan
195
+ if (type === MyInputType.MONEY) {
196
+ e.target.value = moneyFormat(myValue);
197
+ e.value = moneyFormat(myValue);
198
+ } else {
199
+ e.target.value = myValue;
200
+ e.value = myValue;
201
+ }
185
202
  if (onKeyDown != null) onKeyDown(e);
186
203
  };
187
204
  const onMyKeyUp = e => {
188
- e.target.value = myValue;
189
- e.value = myValue;
205
+ // MONEY input için formatlanmış değeri kullan
206
+ if (type === MyInputType.MONEY) {
207
+ e.target.value = moneyFormat(myValue);
208
+ e.value = moneyFormat(myValue);
209
+ } else {
210
+ e.target.value = myValue;
211
+ e.value = myValue;
212
+ }
190
213
  if (onKeyUp != null) onKeyUp(e);
191
214
  };
192
215
  const onMyKeyPress = e => {
193
- e.target.value = myValue;
194
- e.value = myValue;
216
+ // MONEY input için formatlanmış değeri kullan
217
+ if (type === MyInputType.MONEY) {
218
+ e.target.value = moneyFormat(myValue);
219
+ e.value = moneyFormat(myValue);
220
+ } else {
221
+ e.target.value = myValue;
222
+ e.value = myValue;
223
+ }
195
224
  if (onKeyPress != null) onKeyPress(e);
196
225
  };
197
226
  const onMyMouseDown = e => {
198
- e.target.value = myValue;
199
- e.value = myValue;
227
+ // MONEY input için formatlanmış değeri kullan
228
+ if (type === MyInputType.MONEY) {
229
+ e.target.value = moneyFormat(myValue);
230
+ e.value = moneyFormat(myValue);
231
+ } else {
232
+ e.target.value = myValue;
233
+ e.value = myValue;
234
+ }
200
235
  if (onMouseDown != null) onMouseDown(e);
201
236
  };
202
237
  const onMyMouseUp = e => {
203
- e.target.value = myValue;
204
- e.value = myValue;
238
+ // MONEY input için formatlanmış değeri kullan
239
+ if (type === MyInputType.MONEY) {
240
+ e.target.value = moneyFormat(myValue);
241
+ e.value = moneyFormat(myValue);
242
+ } else {
243
+ e.target.value = myValue;
244
+ e.value = myValue;
245
+ }
205
246
  if (onMouseUp != null) onMouseUp(e);
206
247
  };
207
248
  const onMyMouseEnter = e => {
208
- e.target.value = myValue;
209
- e.value = myValue;
249
+ // MONEY input için displayValue'yu koru, sadece diğer tipler için değer ata
250
+ if (type !== MyInputType.MONEY) {
251
+ e.target.value = myValue;
252
+ e.value = myValue;
253
+ }
210
254
  if (onMouseEnter != null) onMouseEnter(e);
211
255
  };
212
256
  const onMyMouseLeave = e => {
213
- e.target.value = myValue;
214
- e.value = myValue;
257
+ // MONEY input için displayValue'yu koru, sadece diğer tipler için değer ata
258
+ if (type !== MyInputType.MONEY) {
259
+ e.target.value = myValue;
260
+ e.value = myValue;
261
+ }
215
262
  if (onMouseLeave != null) onMouseLeave(e);
216
263
  };
217
264
  const onRemoveImageClick = e => {
@@ -246,9 +293,6 @@ function MyInput({
246
293
  typingTimeoutRef.current = setTimeout(() => {
247
294
  setIsTyping(false);
248
295
  if (onChange) {
249
- if (type === MyInputType.MONEY && newValue) {
250
- newValue = parseFloat(newValue.replace(/,/g, '')).toFixed(decimalCount);
251
- }
252
296
  onChange({
253
297
  value: newValue,
254
298
  target: {
@@ -261,6 +305,72 @@ function MyInput({
261
305
  // State'i güncelle
262
306
  setMyValue(newValue);
263
307
  }, [myValue, onChange]);
308
+
309
+ // MONEY input için özel change handler
310
+ const handleMoneyChange = (0, _react.useCallback)(e => {
311
+ const input = e.target;
312
+ const cursorPos = input.selectionStart;
313
+ const oldValue = input.value;
314
+ let newValue = e.target.value;
315
+
316
+ // Yazarken displayValue'yu temizle
317
+ setDisplayValue(null);
318
+
319
+ // Virgülleri kaldır
320
+ newValue = newValue.replace(/,/g, '');
321
+
322
+ // Sadece sayılar ve tek nokta
323
+ newValue = newValue.replace(/[^0-9.]/g, '');
324
+
325
+ // Birden fazla nokta varsa sadece ilkini koru
326
+ const parts = newValue.split('.');
327
+ if (parts.length > 2) {
328
+ newValue = parts[0] + '.' + parts.slice(1).join('');
329
+ }
330
+
331
+ // Noktadan sonra maksimum decimal count
332
+ if (parts.length === 2 && parts[1].length > decimalCount) {
333
+ newValue = parts[0] + '.' + parts[1].substring(0, decimalCount);
334
+ }
335
+
336
+ // State'i güncelle
337
+ setMyValue(newValue);
338
+
339
+ // Cursor pozisyonunu koru
340
+ setTimeout(() => {
341
+ if (input && input.setSelectionRange) {
342
+ // Yeni formatlanmış değerdeki virgül sayısını hesapla
343
+ const newFormattedValue = moneyFormat(newValue);
344
+ const oldCommaCount = (oldValue.match(/,/g) || []).length;
345
+ const newCommaCount = (newFormattedValue.match(/,/g) || []).length;
346
+ const commaChange = newCommaCount - oldCommaCount;
347
+ let newCursorPos = cursorPos + commaChange;
348
+ newCursorPos = Math.max(0, Math.min(newCursorPos, newFormattedValue.length));
349
+ input.setSelectionRange(newCursorPos, newCursorPos);
350
+ }
351
+ }, 0);
352
+
353
+ // Typing durumunu güncelle
354
+ setIsTyping(true);
355
+
356
+ // Önceki timeout'u temizle
357
+ if (typingTimeoutRef.current) {
358
+ clearTimeout(typingTimeoutRef.current);
359
+ }
360
+
361
+ // Yeni timeout ayarla
362
+ typingTimeoutRef.current = setTimeout(() => {
363
+ setIsTyping(false);
364
+ if (onChange) {
365
+ onChange({
366
+ value: newValue,
367
+ target: {
368
+ value: newValue
369
+ }
370
+ });
371
+ }
372
+ }, 300);
373
+ }, [onChange, decimalCount]);
264
374
  const onMyChange = async e => {
265
375
  if (type === MyInputType.FILE || type === MyInputType.IMAGE) {
266
376
  let files = [];
@@ -504,8 +614,8 @@ function MyInput({
504
614
  ref: ref,
505
615
  id: id,
506
616
  type: "text",
507
- value: moneyFormat(myValue),
508
- onChange: handleChange,
617
+ value: moneyFormat(displayValue || myValue),
618
+ onChange: handleMoneyChange,
509
619
  placeholder: placeholder,
510
620
  autoComplete: "off",
511
621
  style: style,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "px-react-ui-components",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "type": "commonjs",