Skip to content

Commit

Permalink
Teacher tool: Restrict numerical inputs to no more than two digits (#…
Browse files Browse the repository at this point in the history
…10168)

* limit input to two digits

* move reg exp to state variable

* use 'filter' instead of 'pattern'
  • Loading branch information
kimprice authored Sep 11, 2024
1 parent e0901ae commit 26db124
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions react-common/components/controls/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface InputProps extends ControlProps {
handleInputRef?: React.RefObject<HTMLInputElement> | ((ref: HTMLInputElement) => void);
preserveValueOnBlur?: boolean;
options?: pxt.Map<string>;
filter?: string;

onChange?: (newValue: string) => void;
onEnterKey?: (value: string) => void;
Expand Down Expand Up @@ -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;

Expand All @@ -83,7 +85,10 @@ export const Input = (props: InputProps) => {
}

const changeHandler = (e: React.ChangeEvent<any>) => {
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);
}
Expand Down Expand Up @@ -147,7 +152,7 @@ export const Input = (props: InputProps) => {

const value = options[option];
setValue(value);
if (onOptionSelected) {
if (onOptionSelected) {
onOptionSelected(value);
}

Expand All @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions teachertool/src/components/CriteriaInstanceDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const InlineInputSegment: React.FC<InlineInputSegmentProps> = ({
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 (
<div title={tooltip} className={css["inline-input-wrapper"]}>
<Input
Expand All @@ -51,7 +51,7 @@ const InlineInputSegment: React.FC<InlineInputSegmentProps> = ({
placeholder={numeric ? "0" : param.name}
title={tooltip}
autoComplete={false}
type={numeric ? "number" : "text"}
filter={numeric ? "[0-9]{1,2}" : undefined}
/>
</div>
);
Expand Down

0 comments on commit 26db124

Please sign in to comment.