Skip to content

Commit

Permalink
Clean up form wrapper, focus
Browse files Browse the repository at this point in the history
  • Loading branch information
Etheryte committed Oct 18, 2023
1 parent f4cbfc1 commit 32fb2fe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
2 changes: 1 addition & 1 deletion web/html/src/components/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Combobox extends React.Component<ComboboxProps, ComboboxState> {
options={options}
styles={colourStyles}
menuPortalTarget={document.body}
formatCreateLabel={(label: string) => `Create "${label}"`}
formatCreateLabel={(label: string) => t("Create {label}", { label })}
getNewOptionData={this.props.getNewOptionData}
{...testAttributes}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export const renderer = (id: string, props: Props) => {
let actionChains = [] as any[];
let startsChecked = false;
try {
actionChains = JSON.parse(props.actionChains || "");
actionChains = JSON.parse(
(props.actionChains || "")
// TODO: Fix this in JSP
.replace(/&quot;/g, '"')
);
startsChecked = JSON.parse(props.startsChecked || "");
} catch (error) {
Loggerhead.error(error);
Expand Down
64 changes: 23 additions & 41 deletions web/html/src/manager/schedule-options/action-chain-picker.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { useEffect, useState } from "react";
import { useRef, useState } from "react";

import { Combobox } from "components/combobox";
import { Form } from "components/input";

import Network from "utils/network";

type ActionChain = {
id: string;
label: string;
entrycount: number;
text: string;
};

type Props = {
Expand All @@ -17,30 +13,14 @@ type Props = {
};

export const ActionChainPicker = (props: Props) => {
const newActionChain: ActionChain = { label: t("new action chain"), id: "new action chain", entrycount: 0 };
const [selectedId, setSelectedId] = useState(newActionChain.id);
const [actionChains, setActionChains] = useState<ActionChain[]>([newActionChain]);

useEffect(() => {
Network.apiGet<ActionChain[]>("actionchain/listChains").then((items) => {
console.log(items);
setActionChains([newActionChain, ...items]);
});
}, []);

useEffect(() => {
console.log(props.actionChains, actionChains, JSON.stringify(props.actionChains) === JSON.stringify(actionChains));
}, [actionChains]);

const comboboxItems = actionChains.map((item) => ({
id: item.label,
text: item.label,
}));
const radioRef = useRef<HTMLInputElement | null>(null);
const [selectedId, setSelectedId] = useState(props.actionChains[0]?.id || "");

return (
<div className="form-group">
<div className="col-sm-3 control-label">
<input
ref={radioRef}
type="radio"
name="schedule_type"
value="action_chain"
Expand All @@ -50,22 +30,24 @@ export const ActionChainPicker = (props: Props) => {
<label htmlFor="schedule-by-action-chain">{t("Add to:")}</label>
</div>
<div className="col-sm-6">
{/* TODO: Remove this <Form> wrapper once https://github.com/SUSE/spacewalk/issues/14250 is implemented */}
<Form>
<Combobox
id="action-chain"
name="action_chain"
options={comboboxItems}
selectedId={selectedId}
getNewOptionData={(input, label) => {
const sanitized = input.replace(/[',]/g, "");
const maxLength = 256;
const cut = sanitized.substring(0, maxLength);
return { id: cut, value: cut, label };
}}
onSelect={(item) => setSelectedId(item.id)}
/>
</Form>
<Combobox
id="action-chain"
name="action_chain"
options={props.actionChains}
selectedId={selectedId}
getNewOptionData={(input, label) => {
const sanitized = input.replace(/[',]/g, "");
const maxLength = 256;
const cut = sanitized.substring(0, maxLength);
return { id: cut, value: cut, label };
}}
onSelect={(item) => setSelectedId(item.id)}
onFocus={() => {
if (radioRef.current) {
radioRef.current.checked = true;
}
}}
/>
</div>
</div>
);
Expand Down

0 comments on commit 32fb2fe

Please sign in to comment.