1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-19 05:19:39 +02:00
portainer/app/react/portainer/custom-templates/components/CustomTemplatesVariablesDefinitionField/CustomTemplatesVariablesDefinitionField.tsx
Chaim Lev-Ari 82b848af0c
refactor(azure): migrate module to react [EE-2782] (#6689)
* refactor(azure): migrate module to react [EE-2782]

* fix(azure): remove optional chain

* feat(azure): apply new icons in dashboard

* feat(azure): apply new icons in dashboard

* feat(ui): allow single string for breadcrumbs

* refactor(azure/containers): use Table.content

* feat(azure/containers): implement new ui [EE-3538]

* fix(azure/containers): use correct icon

* chore(tests): mock svg as component

* fix(azure): fix tests

Co-authored-by: matias.spinarolli <matias.spinarolli@portainer.io>
2022-07-26 16:44:08 -03:00

108 lines
2.8 KiB
TypeScript

import { FormikErrors } from 'formik';
import { FormError } from '@@/form-components/FormError';
import { Input } from '@@/form-components/Input';
import { InputList } from '@@/form-components/InputList';
import { ItemProps } from '@@/form-components/InputList/InputList';
export interface VariableDefinition {
name: string;
label: string;
defaultValue: string;
description: string;
}
interface Props {
value: VariableDefinition[];
onChange: (value: VariableDefinition[]) => void;
errors?: FormikErrors<VariableDefinition>[];
isVariablesNamesFromParent?: boolean;
}
export function CustomTemplatesVariablesDefinitionField({
onChange,
value,
errors,
isVariablesNamesFromParent,
}: Props) {
return (
<InputList
label="Variables Definition"
onChange={onChange}
value={value}
renderItem={(item, onChange, error) => (
<Item
item={item}
onChange={onChange}
error={error}
isNameReadonly={isVariablesNamesFromParent}
/>
)}
itemBuilder={() => ({
label: '',
name: '',
defaultValue: '',
description: '',
})}
errors={errors}
textTip="List should map the mustache variables in the template file, if default value is empty, the variable will be required."
isAddButtonHidden={isVariablesNamesFromParent}
/>
);
}
interface DefinitionItemProps extends ItemProps<VariableDefinition> {
isNameReadonly?: boolean;
}
function Item({ item, onChange, error, isNameReadonly }: DefinitionItemProps) {
const errorObj = typeof error === 'object' ? error : {};
return (
<div className="flex gap-2">
<div>
<Input
value={item.name}
name="name"
onChange={handleChange}
placeholder="Name (e.g var_name)"
readOnly={isNameReadonly}
/>
{errorObj?.name && <FormError>{errorObj.name}</FormError>}
</div>
<div>
<Input
value={item.label}
onChange={handleChange}
placeholder="Label"
name="label"
/>
{errorObj?.label && <FormError>{errorObj.label}</FormError>}
</div>
<div>
<Input
name="description"
value={item.description}
onChange={handleChange}
placeholder="Description"
/>
{errorObj?.description && <FormError>{errorObj.description}</FormError>}
</div>
<div>
<Input
value={item.defaultValue}
onChange={handleChange}
placeholder="Default Value"
name="defaultValue"
/>
{errorObj?.defaultValue && (
<FormError>{errorObj.defaultValue}</FormError>
)}
</div>
</div>
);
function handleChange(e: React.ChangeEvent<HTMLInputElement>) {
onChange({ ...item, [e.target.name]: e.target.value });
}
}