Skip to content

Commit daf2e93

Browse files
authored
Added unit conversion feature in constants block and internal code frequency input (#272)
* added unit conversion feature in all constant and code internal frequency * improved unit conversion feature decimal included in frequency * added comments for unitConversion,handleFunction and regex statement * added negative value support, fixed unwanted comma code
1 parent e099100 commit daf2e93

3 files changed

Lines changed: 75 additions & 4 deletions

File tree

frontend/src/components/blocks/basic/code/code-widget.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { GlobalState } from '../../../../core/store';
88
import BaseBlock, { ContextOption } from '../../common/base-block';
99
import BasePort from '../../common/base-port';
1010
import { CodeBlockModel } from './code-model';
11+
import {unitConversion} from '../../../utils/tooltip/index'
1112
import './styles.scss';
1213

1314

@@ -87,7 +88,7 @@ export class CodeBlockWidget extends React.Component<CodeBlockWidgetProps, CodeB
8788
InputProps={{
8889
endAdornment: <InputAdornment position="start">Hz</InputAdornment>,
8990
}}
90-
type="number"
91+
type="text"
9192
margin="dense"
9293
value={this.state.frequency}
9394
onChange={this.handleFrequencyInput}
@@ -173,8 +174,14 @@ export class CodeBlockWidget extends React.Component<CodeBlockWidgetProps, CodeB
173174
}
174175

175176
handleFrequencyInput = (event: ChangeEvent<HTMLInputElement>) => {
177+
// Convert the input value using a unit conversion function (it returns a string)
178+
const actual_val = parseFloat(unitConversion(event.target.value));
179+
180+
// Update the component state with the raw input value
176181
this.setState({frequency: event.target.value});
177-
this.props.node.data.frequency = event.target.value;
182+
183+
// Update the frequency data in the component's props with the parsed numerical value (actual_val)
184+
this.props.node.data.frequency = actual_val;
178185
}
179186

180187
/**

frontend/src/components/blocks/basic/constant/constant-widget.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { GlobalState } from '../../../../core/store';
66
import BaseBlock, { ContextOption } from '../../common/base-block';
77
import BasePort from '../../common/base-port';
88
import { ConstantBlockModel } from './constant-model';
9+
import {unitConversion} from '../../../utils/tooltip/index'
910
import './styles.scss';
1011

1112
/**
@@ -98,9 +99,15 @@ export class ConstantBlockWidget extends React.Component<ConstantBlockWidgetProp
9899
* @param event Change event from constant input field
99100
*/
100101
handleInput = (event: ChangeEvent<HTMLInputElement>) => {
101-
this.setState({ value: event.target.value });
102+
// Convert the input value using a unit conversion function
103+
const actual_val = unitConversion(event.target.value);
104+
105+
// Update the component state with the raw input value
106+
this.setState({ value: event.target.value});
107+
108+
// Check if the component has associated data in its props
102109
if (this.props.node.data) {
103-
this.props.node.data.value = event.target.value
110+
this.props.node.data.value = actual_val; // Update the value data in the component's props with the converted value
104111
}
105112
}
106113
}

frontend/src/components/utils/tooltip/index.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,63 @@ const useStyles = makeStyles((theme: Theme) => ({
1313
}
1414
}));
1515

16+
// Function for unit conversion in a given input string
17+
export const unitConversion = (input: string): string => {
18+
19+
// Define a multiplier object for unit conversions
20+
const multiplier: { [key: string]: number } = {
21+
'k': 1000,
22+
'M': 1000000,
23+
'm': 0.001,
24+
'K': 1000,
25+
};
26+
27+
const values = input.split(','); // Split input string by commas to handle multiple values
28+
29+
// Map over each value to perform unit conversion
30+
const convertedValues = values.map((value) => {
31+
32+
/*
33+
Regular expression to match and capture components of a value:
34+
- (-?): Captures an optional negative sign.
35+
- (\d*\.?\d+): Captures the numeric part of the value, which may include digits, an optional decimal point,
36+
and
37+
- ([kKmM]?): Captures the unit part of the value, which may be 'k', 'K', 'm', or 'M', and is optional.
38+
- $: Asserts the end of the string, ensuring the match occurs at the end of the value.
39+
The result is stored in the 'match' variable, containing an array with the full matched value,
40+
*/
41+
const match = value.match(/(-?)(\d*\.?\d+)([kKmM]?)$/);
42+
43+
// Check if a match is found
44+
if (match) {
45+
46+
// Extract negative sign, numeric value, and unit from the match
47+
const negativeSign = match[1] || '';
48+
const numericValue = parseFloat(negativeSign + match[2]);
49+
const unit = match[3] || '';
50+
51+
// Check if the unit is present in the multiplier object
52+
if (multiplier.hasOwnProperty(unit)) {
53+
54+
// Perform unit conversion and return the result as a string
55+
const result = numericValue * multiplier[unit];
56+
return result.toString();
57+
58+
} else {
59+
60+
// If unit not found, return the numeric value as a string
61+
return numericValue.toString();
62+
63+
}
64+
}
65+
66+
// Return the original value if it doesn't match the expected format
67+
return value;
68+
});
69+
70+
return convertedValues.join(','); // Join the converted values back into a comma-separated string and return
71+
};
72+
1673
/**
1774
*
1875
* Tooltip with an arrow

0 commit comments

Comments
 (0)