Skip to content

Commit

Permalink
adding logic to handle when shift key is pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
namnguyen20999 committed Jun 22, 2024
1 parent ef9eff6 commit a6c8464
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
57 changes: 54 additions & 3 deletions frontend/taipy-gui/src/components/Taipy/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,27 @@
* specific language governing permissions and limitations under the License.
*/

import React, {useState, useEffect, useCallback, useRef, KeyboardEvent} from "react";
import React, {useState, useEffect, useCallback, useRef, KeyboardEvent, useMemo} from "react";
import TextField from "@mui/material/TextField";
import Tooltip from "@mui/material/Tooltip";
import {styled} from '@mui/material/styles';
import IconButton from "@mui/material/IconButton";

import {createSendActionNameAction, createSendUpdateAction} from "../../context/taipyReducers";
import {TaipyInputProps} from "./utils";
import {useClassNames, useDispatch, useDynamicProperty, useModule} from "../../utils/hooks";

const AUTHORIZED_KEYS = ["Enter", "Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"];

const StyledTextField = styled(TextField)({
'& input[type=number]::-webkit-outer-spin-button, & input[type=number]::-webkit-inner-spin-button': {
display: 'none',
},
'& input[type=number]': {
'-moz-appearance': 'textfield',
},
});

const getActionKeys = (keys?: string): string[] => {
const ak = (
keys
Expand Down Expand Up @@ -101,6 +112,29 @@ const Input = (props: TaipyInputProps) => {
[actionKeys, props.step, props.stepMultiplier, changeDelay, onAction, dispatch, id, module, updateVarName, onChange, propagate]
);

const roundBasedOnStep = useMemo(() => {
const stepString = (props.step || 1).toString();
const decimalPlaces = stepString.includes('.') ? stepString.split('.')[1].length : 0;

Check warning on line 117 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
const multiplier = Math.pow(10, decimalPlaces);
return (value: number) => Math.round(value * multiplier) / multiplier;

Check warning on line 119 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 119 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
}, [props.step]);

const handleUpStepperMouseDown = useCallback((event: React.MouseEvent<HTMLButtonElement>) => {

Check warning on line 122 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
if (event.shiftKey) {
setValue(prevValue => roundBasedOnStep(Number(prevValue) + (props.step || 1) * (props.stepMultiplier || 10)).toString());

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 124 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
} else {
setValue(prevValue => roundBasedOnStep(Number(prevValue) + (props.step || 1)).toString())

Check warning on line 126 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 126 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 126 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 126 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 126 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
}

Check warning on line 127 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 127 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 127 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}, [props.step, props.stepMultiplier, roundBasedOnStep])

const handleDownStepperMouseDown = useCallback((event: React.MouseEvent<HTMLButtonElement>) => {

Check warning on line 130 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
if (event.shiftKey) {
setValue(prevValue => roundBasedOnStep(Number(prevValue) - (props.step || 1) * (props.stepMultiplier || 10)).toString());

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 132 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
} else {
setValue(prevValue => roundBasedOnStep(Number(prevValue) - (props.step || 1)).toString())

Check warning on line 134 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 134 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 134 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 134 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 134 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
}

Check warning on line 135 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 135 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 135 in frontend/taipy-gui/src/components/Taipy/Input.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
}, [props.step, props.stepMultiplier, roundBasedOnStep])

useEffect(() => {
if (props.value !== undefined) {
setValue(props.value);
Expand All @@ -109,7 +143,7 @@ const Input = (props: TaipyInputProps) => {

return (
<Tooltip title={hover || ""}>
<TextField
<StyledTextField
margin="dense"
hiddenLabel
value={value}
Expand All @@ -119,9 +153,26 @@ const Input = (props: TaipyInputProps) => {
inputProps={{
step: props.step ? props.step : 1,
}}
InputProps={{
endAdornment: (
<>
<IconButton
size="small"
onMouseDown={handleUpStepperMouseDown}
>
</IconButton>
<IconButton
size="small"
onMouseDown={handleDownStepperMouseDown}
>
</IconButton>
</>
),
}}
label={props.label}
onChange={handleInput}
onClick={handleClick}
disabled={!active}
onKeyDown={handleAction}
multiline={multiline}
Expand Down
12 changes: 12 additions & 0 deletions taipy/gui/viselements.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@
"type": "str",
"default_value": "None",
"doc": "The label associated with the input."
},
{
"name": "step",
"type": "int|float",
"default_value": "1",
"doc": "The amount that the value changes on each increment or decrement."
},
{
"name": "step_multiplier",
"type": "int|float",
"default_value": "10",
"doc": "The amount that the value changes on each increment or decrement when the shift key is pressed."
}
]
}
Expand Down

0 comments on commit a6c8464

Please sign in to comment.