sliftutils 1.9.2 → 1.9.5

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/index.d.ts CHANGED
@@ -554,6 +554,7 @@ declare module "sliftutils/render-utils/GenericFormat" {
554
554
 
555
555
  declare module "sliftutils/render-utils/Input" {
556
556
  import preact from "preact";
557
+ export declare function roundToDecimals(value: number, decimals: number): number;
557
558
  export type InputProps = (preact.JSX.HTMLAttributes<HTMLInputElement> & {
558
559
  /** ONLY throttles onChangeValue */
559
560
  throttle?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sliftutils",
3
- "version": "1.9.2",
3
+ "version": "1.9.5",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -1,4 +1,5 @@
1
1
  import preact from "preact";
2
+ export declare function roundToDecimals(value: number, decimals: number): number;
2
3
  export type InputProps = (preact.JSX.HTMLAttributes<HTMLInputElement> & {
3
4
  /** ONLY throttles onChangeValue */
4
5
  throttle?: number;
@@ -3,6 +3,22 @@ import { css } from "typesafecss";
3
3
  import { observer } from "./observer";
4
4
  import { throttleFunction } from "socket-function/src/misc";
5
5
 
6
+ const ARROW_KEY_SHIFT_FACTOR = 10;
7
+
8
+ export function roundToDecimals(value: number, decimals: number) {
9
+ return Math.round(value * 10 ** decimals) / 10 ** decimals;
10
+ }
11
+
12
+ function countDecimals(value: number) {
13
+ let text = String(value);
14
+ if (text.includes("e-")) {
15
+ return Number(text.split("e-")[1]);
16
+ }
17
+ let dot = text.indexOf(".");
18
+ if (dot < 0) return 0;
19
+ return text.length - dot - 1;
20
+ }
21
+
6
22
  // TODO: Autogrow mode while typing
7
23
 
8
24
  // NOTE: "value" is optional. If you don't pass "value", we will preserve the value.
@@ -201,13 +217,11 @@ export class Input extends preact.Component<InputProps> {
201
217
  }
202
218
  if (this.elem && props.type === "number") {
203
219
  let delta = 0;
204
- let magnitude = 1;
220
+ let magnitude = Number(props.step) || 1;
205
221
  if (e.shiftKey) {
206
- if (props.integer) {
207
- magnitude = 10;
208
- } else {
209
- magnitude = 0.1;
210
- }
222
+ magnitude *= ARROW_KEY_SHIFT_FACTOR;
223
+ } else if (e.ctrlKey && !props.integer) {
224
+ magnitude /= ARROW_KEY_SHIFT_FACTOR;
211
225
  }
212
226
  if (e.code === "ArrowUp") {
213
227
  delta = magnitude;
@@ -219,9 +233,15 @@ export class Input extends preact.Component<InputProps> {
219
233
  }
220
234
  if (delta !== 0) {
221
235
  e.preventDefault();
222
- let newValue = Math.round(((+this.elem.value || 0) + delta) * 100) / 100;
236
+ let decimals = Math.max(countDecimals(magnitude), countDecimals(+this.elem.value || 0));
237
+ let newValue = roundToDecimals((+this.elem.value || 0) + delta, decimals);
223
238
  e.currentTarget.value = newValue.toString();
224
239
  callback?.(e as unknown as preact.JSX.TargetedInputEvent<HTMLInputElement>);
240
+ if (!hot) {
241
+ props.onChange?.(e as unknown as preact.JSX.TargetedEvent<HTMLInputElement>);
242
+ }
243
+ props.onChangeValue?.(e.currentTarget.value);
244
+ this.lastValue = e.currentTarget.value;
225
245
  }
226
246
  }
227
247
  let { noEnterKeyBlur, onInput, onChange } = props;
@@ -1,5 +1,5 @@
1
1
  import preact from "preact";
2
- import { Input } from "./Input";
2
+ import { Input, roundToDecimals } from "./Input";
3
3
  import { css } from "typesafecss";
4
4
  import { lazy } from "socket-function/src/caching";
5
5
  import { observer } from "./observer";
@@ -68,10 +68,6 @@ export type InputLabelProps = Omit<InputProps, "label" | "title"> & {
68
68
  useDateUI?: boolean;
69
69
  };
70
70
 
71
- function roundToDecimals(value: number, decimals: number) {
72
- return Math.round(value * 10 ** decimals) / 10 ** decimals;
73
- }
74
-
75
71
  export const startGuessDateRange = +new Date(2010, 0, 1).getTime();
76
72
  export const endGuessDateRange = +new Date(2050, 0, 1).getTime();
77
73