react-ui89 0.19.1 → 0.19.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/esm/index.js CHANGED
@@ -303,14 +303,20 @@ function throttledTimeout() {
303
303
  Timeout.set(id, () => callback(), delay);
304
304
  }
305
305
  },
306
+ /**
307
+ * If there is a call that has been scheduled, remove it from the queue.
308
+ */
309
+ abort() {
310
+ Timeout.clear(id, true);
311
+ },
306
312
  };
307
313
  }
308
314
 
309
315
  const THROTTLE_DELAY = 200;
310
316
  function Ui89InputText({ value, placeholder, autoTrim = true, onChange, onTyping, onFocus, onBlur, }) {
311
317
  const inputRef = useRef(null);
312
- const [isTyping, setIsTyping] = useState(false);
313
318
  const [intermediateValue, setIntermediateValue] = useState(value);
319
+ const [isTyping, setIsTyping] = useState(false);
314
320
  const onChangeThrottled = useMemo(() => throttledTimeout(), []);
315
321
  useEffect(() => {
316
322
  if (inputRef.current === null) {
@@ -326,6 +332,8 @@ function Ui89InputText({ value, placeholder, autoTrim = true, onChange, onTyping
326
332
  }
327
333
  };
328
334
  inputRef.current.onblur = () => {
335
+ onChangeThrottled.abort();
336
+ update();
329
337
  setIsTyping(false);
330
338
  if (onTyping) {
331
339
  onTyping(false);
@@ -336,31 +344,40 @@ function Ui89InputText({ value, placeholder, autoTrim = true, onChange, onTyping
336
344
  };
337
345
  }, [inputRef.current, onBlur, onFocus]);
338
346
  useEffect(() => {
339
- if (isTyping === null) {
340
- return;
341
- }
342
347
  if (!isTyping) {
343
- // Lets show the user what is actually the value.
344
- setIntermediateValue(value);
348
+ // Lets show the user what the actual value is.
349
+ onChangeThrottled.call(THROTTLE_DELAY, () => {
350
+ setIntermediateValue(value);
351
+ });
345
352
  }
346
- }, [isTyping, value]);
353
+ }, [value]);
347
354
  function implOnChange(e) {
355
+ setIntermediateValue(e.target.value);
356
+ onChangeThrottled.call(THROTTLE_DELAY, update);
357
+ }
358
+ function update() {
348
359
  if (!onChange) {
349
360
  return;
350
361
  }
351
- let newVal = e.target.value;
352
- setIntermediateValue(newVal);
362
+ if (inputRef.current === null) {
363
+ return;
364
+ }
365
+ // We get the current value straight from the element because this function
366
+ // may have been queued.
367
+ let newVal = inputRef.current.value;
353
368
  if (autoTrim) {
354
369
  // Must trim after setting intermediate value. Do not want to disturb
355
370
  // user.
356
371
  newVal = newVal.replace(/\s+/g, " ").trim();
357
372
  }
358
- onChangeThrottled.call(THROTTLE_DELAY, () => {
359
- onChange(newVal);
360
- });
373
+ if (newVal === value) {
374
+ // Same value.
375
+ return;
376
+ }
377
+ onChange(newVal);
361
378
  }
362
379
  return (React__default.createElement("div", null,
363
- React__default.createElement("input", { ref: inputRef, className: `${inputBoxStyles.inputBox} ${typoStyles.special}`, type: "text", value: intermediateValue, onChange: implOnChange, placeholder: placeholder })));
380
+ React__default.createElement("input", { ref: inputRef, value: intermediateValue, className: `${inputBoxStyles.inputBox} ${typoStyles.special}`, type: "text", onChange: implOnChange, placeholder: placeholder })));
364
381
  }
365
382
 
366
383
  function stringRemoveAllWhitespace(str) {
@@ -390,10 +407,8 @@ function Ui89InputTextNumber({
390
407
  * The value that is emitted when the input is emptied.
391
408
  */
392
409
  emptyValue = null, value, min, max, onChange, precision, }) {
410
+ const [isTyping, setIsTyping] = useState(false);
393
411
  const [intermediateValue, setIntermediateValue] = useState(displayText(value, emptyValue));
394
- useEffect(() => {
395
- setIntermediateValue(displayText(value, emptyValue));
396
- }, [value]);
397
412
  function implOnChange(value) {
398
413
  if (onChange === undefined) {
399
414
  return;
@@ -421,11 +436,12 @@ emptyValue = null, value, min, max, onChange, precision, }) {
421
436
  }
422
437
  onChange(value);
423
438
  }
424
- function implOnBlur() {
425
- // Correct display value.
426
- setIntermediateValue(displayText(value, emptyValue));
427
- }
428
- return (React__default.createElement(Ui89InputText, { value: intermediateValue, onChange: implOnChange, onBlur: implOnBlur }));
439
+ useEffect(() => {
440
+ if (!isTyping) {
441
+ setIntermediateValue(displayText(value, emptyValue));
442
+ }
443
+ }, [isTyping, value]);
444
+ return (React__default.createElement(Ui89InputText, { value: intermediateValue, onChange: implOnChange, onTyping: setIsTyping }));
429
445
  }
430
446
 
431
447
  var styles$a = {"menuBar":"Ui89MenuBar-module_menuBar__IlnEP","menuBarItem":"Ui89MenuBar-module_menuBarItem__UVFg2"};