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

refs #38, fixes case where choices is empty but unlisted_choice is enabled #41

Merged
merged 5 commits into from
May 23, 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
19 changes: 19 additions & 0 deletions jupyterhub_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,23 @@
},
},
},
{
"description": "Specify your own docker image (must have python and jupyterhub installed in it)",
"display_name": "Bring your own image",
"profile_options": {
"image": {
"choices": {},
"display_name": "Image",
"unlisted_choice": {
"display_name": "Custom image",
"enabled": True,
"kubespawner_override": {"image": "{value}"},
"validation_message": "Must be a publicly available docker image, of form <image-name>:<tag>",
"validation_regex": "^.+:.+$",
"display_name_in_choices": "Other...",
},
},
},
"slug": "custom",
},
]
21 changes: 21 additions & 0 deletions setupTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,25 @@ window.profileList = [
},
},
},
{
description:
"Specify your own docker image (must have python and jupyterhub installed in it)",
display_name: "Bring your own image",
profile_options: {
image: {
choices: {},
display_name: "Image - No options",
unlisted_choice: {
display_name: "Docker image",
enabled: true,
kubespawner_override: { image: "{value}" },
validation_message:
"Must be a publicly available docker image, of form <image-name>:<tag>",
validation_regex: "^.+:.+$",
display_name_in_choices: "Other...",
},
},
},
slug: "custom",
},
];
9 changes: 9 additions & 0 deletions src/ProfileForm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ test("Multiple profiles renders", async () => {
expect(smallImageField.tabIndex).toEqual(-1);
expect(screen.getByLabelText("Resource Allocation").tabIndex).toEqual(-1);
});

test("select with no options should not render", () => {
render(
<SpawnerFormProvider>
<ProfileForm />
</SpawnerFormProvider>,
);
expect(screen.queryByLabelText("Image - No options")).not.toBeInTheDocument();
});
35 changes: 20 additions & 15 deletions src/ResourceSelect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { SelectField, TextField } from "./components/form/fields";
function ResourceSelect({ id, profile, config, customOptions = [] }) {
const { display_name, unlisted_choice } = config;

const { options, defaultOption } = useSelectOptions(config, customOptions);
const { options, defaultOption, hasDefaultChoices } = useSelectOptions(
config,
customOptions,
);
const { profile: selectedProfile } = useContext(SpawnerFormContext);
const FIELD_ID = `profile-option-${profile}--${id}`;
const FIELD_ID_UNLISTED = `${FIELD_ID}--unlisted-choice`;

const isActive = selectedProfile?.slug === profile;
const [value, setValue] = useState(defaultOption.value);
const [value, setValue] = useState(defaultOption?.value);
const [unlistedChoiceValue, setUnlistedChoiceValue] = useState("");

if (!options.length > 0) {
Expand All @@ -23,20 +26,22 @@ function ResourceSelect({ id, profile, config, customOptions = [] }) {

return (
<>
<SelectField
id={FIELD_ID}
label={display_name}
options={options}
defaultOption={defaultOption}
value={value}
onChange={(e) => setValue(e.value)}
tabIndex={isActive ? "0" : "-1"}
validate={
isActive && {
required: "Select a value.",
{hasDefaultChoices && (
<SelectField
id={FIELD_ID}
label={display_name}
options={options}
defaultOption={defaultOption}
value={value}
onChange={(e) => setValue(e.value)}
tabIndex={isActive ? "0" : "-1"}
validate={
isActive && {
required: "Select a value.",
}
}
}
/>
/>
)}
{value === "unlisted_choice" && (
<TextField
id={FIELD_ID_UNLISTED}
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useSelectOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useMemo } from "react";

function useSelectOptions(config, customOptions = []) {
const { choices, unlisted_choice } = config;

const hasDefaultChoices = Object.keys(choices).length > 0;
const options = useMemo(() => {
const defaultChoices = Object.keys(choices).map((choiceName) => {
return {
Expand All @@ -24,9 +24,14 @@ function useSelectOptions(config, customOptions = []) {
return [...defaultChoices, ...extraChoices, ...customOptions];
}, [choices]);

// The default choice is either the choice marked as default,
// OR the first explicit choice
// OR the first item from options which could be an extra or custom choice
const defaultChoiceName =
Object.keys(choices).find((choiceName) => choices[choiceName].default) ||
Object.keys(choices)[0];
Object.keys(choices).length > 0
? Object.keys(choices)[0]
: options[0].value;

const defaultOption = options.find(
(option) => option.value === defaultChoiceName,
Expand All @@ -35,6 +40,7 @@ function useSelectOptions(config, customOptions = []) {
return {
options,
defaultOption,
hasDefaultChoices,
};
}

Expand Down
Loading