-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Number visual element #1442
Closed
namnguyen20999
wants to merge
8
commits into
develop
from
1429-the-number-visual-element-number-should-have-property-type-step
Closed
Number visual element #1442
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ef9eff6
inital commit
namnguyen20999 a6c8464
adding logic to handle when shift key is pressed
namnguyen20999 3611ef7
step & step_multiplier example
namnguyen20999 b85030e
Isolating complex part + refactoring the viselements.json
namnguyen20999 1a89d39
Merge branch 'develop' into 1429-the-number-visual-element-number-sho…
namnguyen20999 8d3df32
Merge branch 'develop' into 1429-the-number-visual-element-number-sho…
namnguyen20999 3f2a7df
Merge branch '1429-the-number-visual-element-number-should-have-prope…
namnguyen20999 1d6239f
change to MUI icons
namnguyen20999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2021-2024 Avaiga Private Limited | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
# ----------------------------------------------------------------------------------------- | ||
# To execute this script, make sure that the taipy-gui package is installed in your | ||
# Python environment and run: | ||
# python <script> | ||
# ----------------------------------------------------------------------------------------- | ||
from taipy.gui import Gui | ||
|
||
value = 50 | ||
|
||
page = """ | ||
<|{value}|number|step=2|> | ||
""" | ||
|
||
Gui(page).run() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,23 +11,34 @@ | |
* 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"; | ||
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': { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we want to use WebKit or mozilla specific css... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do if this is mandatory for specific browsers. |
||
display: 'none', | ||
}, | ||
'& input[type=number]': { | ||
'-moz-appearance': 'textfield', | ||
}, | ||
}); | ||
|
||
const getActionKeys = (keys?: string): string[] => { | ||
const ak = ( | ||
keys | ||
? keys | ||
.split(";") | ||
.map((v) => v.trim().toLowerCase()) | ||
.filter((v) => AUTHORIZED_KEYS.some((k) => k.toLowerCase() === v)) | ||
.split(";") | ||
.map((v) => v.trim().toLowerCase()) | ||
.filter((v) => AUTHORIZED_KEYS.some((k) => k.toLowerCase() === v)) | ||
: [] | ||
).map((v) => AUTHORIZED_KEYS.find((k) => k.toLowerCase() == v) as string); | ||
return ak.length > 0 ? ak : [AUTHORIZED_KEYS[0]]; | ||
|
@@ -55,6 +66,8 @@ const Input = (props: TaipyInputProps) => { | |
const className = useClassNames(props.libClassName, props.dynamicClassName, props.className); | ||
const active = useDynamicProperty(props.active, props.defaultActive, true); | ||
const hover = useDynamicProperty(props.hoverText, props.defaultHoverText, undefined); | ||
const step = useDynamicProperty(props.step, props.defaultStep, 1); | ||
const stepMultiplier = useDynamicProperty(props.stepMultiplier, props.defaultStepMultiplier, 10); | ||
|
||
const handleInput = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement>) => { | ||
|
@@ -79,6 +92,12 @@ const Input = (props: TaipyInputProps) => { | |
|
||
const handleAction = useCallback( | ||
(evt: KeyboardEvent<HTMLDivElement>) => { | ||
if (evt.shiftKey && evt.key === 'ArrowUp') { | ||
setValue(((Number(evt.currentTarget.querySelector("input")?.value || 0) + (step || 1) * (stepMultiplier || 10) - (step || 1)).toString())); | ||
} | ||
if (evt.shiftKey && evt.key === 'ArrowDown') { | ||
namnguyen20999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setValue(((Number(evt.currentTarget.querySelector("input")?.value || 0) - (step || 1) * (stepMultiplier || 10) + (step || 1)).toString())); | ||
} | ||
if (!evt.shiftKey && !evt.ctrlKey && !evt.altKey && actionKeys.includes(evt.key)) { | ||
namnguyen20999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const val = evt.currentTarget.querySelector("input")?.value; | ||
if (changeDelay > 0 && delayCall.current > 0) { | ||
|
@@ -92,9 +111,36 @@ const Input = (props: TaipyInputProps) => { | |
evt.preventDefault(); | ||
} | ||
}, | ||
[actionKeys, updateVarName, onAction, id, dispatch, onChange, changeDelay, propagate, module] | ||
[actionKeys, step, stepMultiplier, changeDelay, onAction, dispatch, id, module, updateVarName, onChange, propagate] | ||
); | ||
|
||
const roundBasedOnStep = useMemo(() => { | ||
const stepString = (step || 1).toString(); | ||
const decimalPlaces = stepString.includes('.') ? stepString.split('.')[1].length : 0; | ||
const multiplier = Math.pow(10, decimalPlaces); | ||
return (value: number) => Math.round(value * multiplier) / multiplier; | ||
}, [step]); | ||
|
||
const calculateNewValue = useMemo(() => { | ||
return (prevValue: string, step: number, stepMultiplier: number, shiftKey: boolean, increment: boolean) => { | ||
const multiplier = shiftKey ? stepMultiplier : 1; | ||
const change = step * multiplier * (increment ? 1 : -1); | ||
return roundBasedOnStep(Number(prevValue) + change).toString(); | ||
}; | ||
}, [roundBasedOnStep]); | ||
|
||
const handleStepperMouseDown = useCallback((event: React.MouseEvent<HTMLButtonElement>, increment: boolean) => { | ||
setValue(prevValue => calculateNewValue(prevValue, step || 1, stepMultiplier || 10, event.shiftKey, increment)); | ||
}, [step, stepMultiplier, calculateNewValue]); | ||
|
||
const handleUpStepperMouseDown = useCallback((event: React.MouseEvent<HTMLButtonElement>) => { | ||
handleStepperMouseDown(event, true); | ||
}, [handleStepperMouseDown]); | ||
|
||
const handleDownStepperMouseDown = useCallback((event: React.MouseEvent<HTMLButtonElement>) => { | ||
handleStepperMouseDown(event, false); | ||
}, [handleStepperMouseDown]); | ||
|
||
useEffect(() => { | ||
if (props.value !== undefined) { | ||
setValue(props.value); | ||
|
@@ -103,13 +149,34 @@ const Input = (props: TaipyInputProps) => { | |
|
||
return ( | ||
<Tooltip title={hover || ""}> | ||
<TextField | ||
<StyledTextField | ||
margin="dense" | ||
hiddenLabel | ||
value={value ?? ""} | ||
value={value} | ||
namnguyen20999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
className={className} | ||
type={type} | ||
id={id} | ||
inputProps={{ | ||
step: step ? step : 1, | ||
}} | ||
InputProps={{ | ||
FredLL-Avaiga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
endAdornment: ( | ||
<> | ||
<IconButton | ||
size="small" | ||
onMouseDown={handleUpStepperMouseDown} | ||
> | ||
▲ | ||
namnguyen20999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</IconButton> | ||
<IconButton | ||
size="small" | ||
onMouseDown={handleDownStepperMouseDown} | ||
> | ||
▼ | ||
</IconButton> | ||
</> | ||
), | ||
}} | ||
label={props.label} | ||
onChange={handleInput} | ||
disabled={!active} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍