Skip to content
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

Teacher tool: Restrict numerical inputs to no more than two digits #10168

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading