Skip to content

Commit

Permalink
HashListForm now supports custom input component and default value
Browse files Browse the repository at this point in the history
  • Loading branch information
kreneskyp committed Mar 3, 2024
1 parent 0c8af01 commit d4f46b7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions frontend/components/HashListForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ export const HashListForm = ({
hash_list,
onChange,
onDelete,
defaultValue,
component,
}) => {
const colorMode = useEditorColorMode();
const _default = defaultValue || "";

const handleInputChange = (e, index) => {
const { value } = e.target;
Expand Down Expand Up @@ -46,15 +49,16 @@ export const HashListForm = ({

// Add empty element here so the rendered structure and input focus sticks
// while typing in the new item.
const list_plus_new = list ? [...list, ""] : [""];
const list_plus_new = list ? [...list, _default] : [_default];
const InputComponent = component || Input;

return (
<Box width="100%">
<FormLabel>{label}</FormLabel>
<VStack align="stretch">
{list_plus_new?.map((value, index) => (
<HStack key={index}>
<Input
<HStack key={index} alignItems={"start"}>
<InputComponent
value={value}
onChange={(e) => handleInputChange(e, index)}
placeholder={`Item ${index + 1}`}
Expand All @@ -65,6 +69,7 @@ export const HashListForm = ({
onClick={() => handleRemoveClick(index)}
cursor={index < list?.length ? "pointer" : "default"}
color={index < list?.length ? "inherit" : "transparent"}
style={{ marginTop: "12px" }}
/>
</HStack>
))}
Expand Down
4 changes: 4 additions & 0 deletions frontend/json_form/fields/HashList.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const HashList = ({
config,
onChange,
onDelete,
defaultValue,
component,
}) => {
const hashField = `${name}_hash`;

Expand All @@ -31,6 +33,8 @@ export const HashList = ({
onDelete={onDelete}
label={getLabel(name)}
isRequired
defaultValue={defaultValue}
component={component}
/>
);
};

0 comments on commit d4f46b7

Please sign in to comment.