Skip to content

Commit

Permalink
limit input to two digits
Browse files Browse the repository at this point in the history
  • Loading branch information
kimprice committed Sep 9, 2024
1 parent 0473115 commit dbd3d33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 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 @@ -59,10 +60,11 @@ export const Input = (props: InputProps) => {
onOptionSelected,
handleInputRef,
preserveValueOnBlur,
options
options,
filter
} = props;

const [value, setValue] = React.useState(undefined);
const [value, setValue] = React.useState(initialValue || "");
const [expanded, setExpanded] = React.useState(false);

let container: HTMLDivElement;
Expand All @@ -83,7 +85,11 @@ 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) {
const pat = new RegExp(filter);
newValue = newValue.match(pat)?.join("") || "";
}
if (!readOnly && (value !== newValue)) {
setValue(newValue);
}
Expand Down Expand Up @@ -147,7 +153,7 @@ export const Input = (props: InputProps) => {

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

Expand All @@ -174,7 +180,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 dbd3d33

Please sign in to comment.