@adcops/autocore-react 3.0.19 → 3.0.21

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.
@@ -0,0 +1,89 @@
1
+ /*
2
+ * Copyright (C) 2024 Automated Design Corp.. All Rights Reserved.
3
+ * Created Date: 2024-04-30 11:41:59
4
+ * -----
5
+ * Last Modified: 2024-04-30 21:33:42
6
+ * -----
7
+ *
8
+ */
9
+
10
+
11
+
12
+ import { useCallback, useState } from 'react';
13
+
14
+
15
+ export const kMillimeters2Inches: number = 1 / 25.4;
16
+
17
+
18
+ type UseScaledValueReturn = [number, (newValue: number) => void];
19
+
20
+ /**
21
+ * A custom React hook for converting values between different scales,
22
+ * with support for dynamically updating the scale and offset. This hook manages values
23
+ * in a base unit and allows for easy conversion to a display unit, updating
24
+ * reactively to changes in the conversion factors.
25
+ *
26
+ * @param initialValue - The initial value in the base unit.
27
+ * @param scale - The dynamic scale factor used for conversion to the display unit.
28
+ * @param offset - An optional offset applied after scaling (default is 0).
29
+ * @returns A tuple containing the display value and a setter function for updating
30
+ * the value in the display unit.
31
+ *
32
+ * @example
33
+ * Here's how to use `useScaledValue` in a component that fetches a scale factor from a backend
34
+ * and allows the user to input values in a converted scale, which are then handled in a base unit:
35
+ *
36
+ * ```tsx
37
+ * import React, { useState, useEffect } from 'react';
38
+ * import { useScaledValue } from './hooks/useScaledValue';
39
+ *
40
+ * const MeasurementInput: React.FC = () => {
41
+ * const [units, setUnits] = useState<number>(1); // Start with a default scale of 1
42
+ * const [xPosition, setXPosition] = useScaledValue(0, units);
43
+ *
44
+ * useEffect(() => {
45
+ * async function fetchScale() {
46
+ * try {
47
+ * // Simulate fetching scale factor from a backend
48
+ * const res = await invoke("GNV", "read_value", { group: "ux", key: "kSelectedUnits" });
49
+ * setUnits(res.data); // Update scale based on backend response
50
+ * } catch (error) {
51
+ * console.error('Failed to fetch units', error);
52
+ * }
53
+ * }
54
+ * fetchScale();
55
+ * }, []);
56
+ *
57
+ * return (
58
+ * <input
59
+ * type="number"
60
+ * value={xPosition.toFixed(2)} // Display the value formatted to 2 decimal places
61
+ * onChange={(e) => setXPosition(parseFloat(e.target.value))}
62
+ * />
63
+ * );
64
+ * };
65
+ *
66
+ * export default MeasurementInput;
67
+ * ```
68
+ *
69
+ * In this example, `useScaledValue` is used to manage a measurement input in a dynamic unit system.
70
+ * The component fetches the conversion scale from the backend upon component mounting and updates the
71
+ * input display accordingly. The user's input is converted back to the base unit before being processed.
72
+ */
73
+ export function useScaledValue(initialValue: number, scale: number, offset: number = 0): UseScaledValueReturn {
74
+
75
+ const [displayValue, setDisplayValue] = useState<number>(initialValue * scale + offset);
76
+
77
+ const handleSetDisplayValue = useCallback((newValue: number) => {
78
+
79
+ // In autocore-react, we multiple to scale incoming values,
80
+ // divide to scale outgoing values.
81
+ // This is an INCOMING value, so we multiply.
82
+
83
+ const newDisplayValue = newValue * scale + offset;
84
+ setDisplayValue(newDisplayValue);
85
+
86
+ }, [scale, offset]);
87
+
88
+ return [displayValue, handleSetDisplayValue];
89
+ }