@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.
- package/dist/components/FileList.d.ts +33 -0
- package/dist/components/FileList.js +1 -1
- package/dist/components/ValueDisplay.d.ts +49 -3
- package/dist/components/ValueDisplay.js +1 -1
- package/dist/components/ValueInput.d.ts +66 -77
- package/dist/components/ValueInput.js +1 -1
- package/dist/hooks/adsHooks.d.ts +49 -0
- package/dist/hooks/adsHooks.js +1 -1
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +1 -0
- package/dist/hooks/useScaledValue.d.ts +57 -0
- package/dist/hooks/useScaledValue.js +1 -0
- package/package.json +2 -2
- package/src/components/FileList.tsx +176 -43
- package/src/components/FileSelect.tsx +1 -1
- package/src/components/ValueDisplay.tsx +72 -17
- package/src/components/ValueInput.tsx +213 -213
- package/src/hooks/adsHooks.tsx +76 -2
- package/src/hooks/index.ts +12 -0
- package/src/hooks/useScaledValue.tsx +89 -0
|
@@ -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
|
+
}
|