diff --git a/react-common/components/controls/Input.tsx b/react-common/components/controls/Input.tsx index 77cc14b95eba..12a560320e99 100644 --- a/react-common/components/controls/Input.tsx +++ b/react-common/components/controls/Input.tsx @@ -22,6 +22,7 @@ export interface InputProps extends ControlProps { handleInputRef?: React.RefObject | ((ref: HTMLInputElement) => void); preserveValueOnBlur?: boolean; options?: pxt.Map; + filter?: string; onChange?: (newValue: string) => void; onEnterKey?: (value: string) => void; @@ -62,8 +63,9 @@ export const Input = (props: InputProps) => { options } = props; - const [value, setValue] = React.useState(undefined); + const [value, setValue] = React.useState(initialValue || ""); const [expanded, setExpanded] = React.useState(false); + const [filter] = React.useState(props.filter ? new RegExp(props.filter) : undefined); let container: HTMLDivElement; @@ -83,7 +85,10 @@ export const Input = (props: InputProps) => { } const changeHandler = (e: React.ChangeEvent) => { - const newValue = (e.target as any).value; + let newValue = (e.target as any).value; + if (newValue && filter) { + newValue = newValue.match(filter)?.join("") || ""; + } if (!readOnly && (value !== newValue)) { setValue(newValue); } @@ -147,7 +152,7 @@ export const Input = (props: InputProps) => { const value = options[option]; setValue(value); - if (onOptionSelected) { + if (onOptionSelected) { onOptionSelected(value); } @@ -174,7 +179,7 @@ export const Input = (props: InputProps) => { aria-hidden={ariaHidden} type={type || "text"} placeholder={placeholder} - value={value !== undefined ? value : (initialValue || "")} + value={value} readOnly={!!readOnly} onClick={clickHandler} onChange={changeHandler} diff --git a/teachertool/src/components/CriteriaInstanceDisplay.tsx b/teachertool/src/components/CriteriaInstanceDisplay.tsx index 5029c3134fa9..50e8f30089e3 100644 --- a/teachertool/src/components/CriteriaInstanceDisplay.tsx +++ b/teachertool/src/components/CriteriaInstanceDisplay.tsx @@ -34,7 +34,7 @@ const InlineInputSegment: React.FC = ({ setParameterValue(instance.instanceId, param.name, newValue); } - const tooltip = isEmpty ? `"${param.name}: ${Strings.ValueRequired}` : param.name; + const tooltip = isEmpty ? `${param.name}: ${Strings.ValueRequired}` : param.name; return (
= ({ placeholder={numeric ? "0" : param.name} title={tooltip} autoComplete={false} - type={numeric ? "number" : "text"} + filter={numeric ? "[0-9]{1,2}" : undefined} />
);