Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(site): set min and max attributes for workspace number parameters #15182

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

Merged
merged 7 commits into from
Nov 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ export const NumberType: Story = {
},
};

export const NumberTypeWithIncreasingMonotonicity: Story = {
args: {
value: 4,
id: "number_parameter",
parameter: createTemplateVersionParameter({
name: "number_parameter",
type: "number",
description: "Numeric parameter",
default_value: "",
validation_min: 0,
validation_max: 10,
validation_monotonic: "increasing",
}),
},
};

export const NumberTypeWithDecreasingMonotonicity: Story = {
args: {
value: 4,
id: "number_parameter",
parameter: createTemplateVersionParameter({
name: "number_parameter",
type: "number",
description: "Numeric parameter",
default_value: "",
validation_min: 0,
validation_max: 10,
validation_monotonic: "decreasing",
}),
},
};

export const BooleanType: Story = {
args: {
value: "false",
Expand Down
28 changes: 28 additions & 0 deletions site/src/components/RichParameterInput/RichParameterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ErrorOutline from "@mui/icons-material/ErrorOutline";
import Button from "@mui/material/Button";
import FormControlLabel from "@mui/material/FormControlLabel";
import FormHelperText from "@mui/material/FormHelperText";
import type { InputBaseComponentProps } from "@mui/material/InputBase";
import Radio from "@mui/material/Radio";
import RadioGroup from "@mui/material/RadioGroup";
import TextField, { type TextFieldProps } from "@mui/material/TextField";
Expand Down Expand Up @@ -217,6 +218,7 @@ export const RichParameterInput: FC<RichParameterInputProps> = ({
onChange={onChange}
size={size}
parameter={parameter}
parameterAutofill={parameterAutofill}
/>
{!parameter.ephemeral &&
autofillSource === "user_history" &&
Expand Down Expand Up @@ -250,6 +252,7 @@ const RichParameterField: FC<RichParameterInputProps> = ({
disabled,
onChange,
parameter,
parameterAutofill,
value,
size,
...props
Expand Down Expand Up @@ -375,6 +378,30 @@ const RichParameterField: FC<RichParameterInputProps> = ({
);
}

let inputProps: InputBaseComponentProps = {};
if (parameter.type === "number") {
switch (parameter.validation_monotonic) {
case "increasing":
inputProps = {
max: parameter.validation_max,
min: parameterAutofill?.value,
};
break;
case "decreasing":
inputProps = {
max: parameterAutofill?.value,
min: parameter.validation_min,
};
break;
default:
inputProps = {
max: parameter.validation_max,
min: parameter.validation_min,
};
break;
}
}

// A text field can technically handle all cases!
// As other cases become more prominent (like filtering for numbers),
// we should break this out into more finely scoped input fields.
Expand All @@ -389,6 +416,7 @@ const RichParameterField: FC<RichParameterInputProps> = ({
required={parameter.required}
placeholder={parameter.default_value}
value={value}
inputProps={inputProps}
onChange={(event) => {
onChange(event.target.value);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ export const WorkspaceParametersForm: FC<WorkspaceParameterFormProps> = ({
);
}}
parameter={parameter}
parameterAutofill={autofillParams?.find(
({ name }) => name === parameter.name,
)}
/>
) : null,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test("Submit the workspace settings page successfully", async () => {
{ exact: false },
);
await user.clear(parameter2);
await user.type(parameter2, "1");
await user.type(parameter2, "3");
await user.click(
within(form).getByRole("button", { name: "Submit and restart" }),
);
Expand All @@ -70,7 +70,7 @@ test("Submit the workspace settings page successfully", async () => {
transition: "start",
rich_parameter_values: [
{ name: MockTemplateVersionParameter1.name, value: "new-value" },
{ name: MockTemplateVersionParameter2.name, value: "1" },
{ name: MockTemplateVersionParameter2.name, value: "3" },
],
});
});
Expand Down
Loading