Skip to content

Commit

Permalink
Merge pull request #2481 from NDLANO/refactor/version-form
Browse files Browse the repository at this point in the history
refactor: use form primitives in VersionForm
  • Loading branch information
Jonas-C authored Oct 8, 2024
2 parents dc4415a + e8b307e commit a9fbcb0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
5 changes: 3 additions & 2 deletions src/containers/TaxonomyVersions/components/VersionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import VersionSourceField from "./VersionSourceField";
import { Row } from "../../../components";
import AlertModal from "../../../components/AlertModal";
import Field from "../../../components/Field";
import { FormikForm } from "../../../components/FormikForm";
import validateFormik, { RulesType } from "../../../components/formikValidationSchema";
import SaveButton from "../../../components/SaveButton";
import Fade from "../../../components/Taxonomy/Fade";
Expand Down Expand Up @@ -155,7 +156,7 @@ const VersionForm = ({ version, existingVersions, onClose }: Props) => {
>
{({ isSubmitting, isValid, dirty, handleSubmit }) => {
return (
<>
<FormikForm>
<StyledTitle>{t(`taxonomyVersions.${!version ? "newVersionTitle" : "editVersionTitle"}`)}</StyledTitle>
<VersionNameField />
{!version && <VersionSourceField existingVersions={existingVersions} />}
Expand Down Expand Up @@ -201,7 +202,7 @@ const VersionForm = ({ version, existingVersions, onClose }: Props) => {
]}
onCancel={() => setShowAlertModal(false)}
/>
</>
</FormikForm>
);
}}
</Formik>
Expand Down
54 changes: 24 additions & 30 deletions src/containers/TaxonomyVersions/components/VersionLockedField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
*/

import { useTranslation } from "react-i18next";
import { Label, RadioButtonGroup, RadioButtonItem } from "@ndla/forms";
import { RadioButtonWrapper, FieldsetRow, LeftLegend, StyledFormControl } from "../../../components/Form/styles";
import {
FieldRoot,
RadioGroupItem,
RadioGroupItemControl,
RadioGroupItemHiddenInput,
RadioGroupItemText,
RadioGroupLabel,
RadioGroupRoot,
} from "@ndla/primitives";
import { FormField } from "../../../components/FormField";

const VersionLockedField = () => {
Expand All @@ -26,36 +33,23 @@ const VersionLockedField = () => {
];
return (
<FormField name="locked">
{({ field }) => (
<StyledFormControl>
<RadioButtonGroup
onValueChange={(value: string) =>
field.onChange({
target: {
name: field.name,
value: value,
},
})
}
{({ field, helpers }) => (
<FieldRoot>
<RadioGroupRoot
value={field.value.toString()}
orientation="horizontal"
defaultValue={field.value.toString()}
asChild
onValueChange={(details) => helpers.setValue(details.value)}
>
<FieldsetRow>
<LeftLegend margin="none" textStyle="label-small">
{t("taxonomyVersions.form.locked.subTitle")}
</LeftLegend>
{options.map((option) => (
<RadioButtonWrapper key={option.value}>
<RadioButtonItem id={`locked-${option.value}`} value={option.value} />
<Label htmlFor={`locked-${option.value}`} margin="none" textStyle="label-small">
{option.title}
</Label>
</RadioButtonWrapper>
))}
</FieldsetRow>
</RadioButtonGroup>
</StyledFormControl>
<RadioGroupLabel>{t("taxonomyVersions.form.locked.subTitle")}</RadioGroupLabel>
{options.map((option) => (
<RadioGroupItem key={option.value} value={option.value}>
<RadioGroupItemControl />
<RadioGroupItemText>{option.title}</RadioGroupItemText>
<RadioGroupItemHiddenInput />
</RadioGroupItem>
))}
</RadioGroupRoot>
</FieldRoot>
)}
</FormField>
);
Expand Down
15 changes: 7 additions & 8 deletions src/containers/TaxonomyVersions/components/VersionNameField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
*/

import { useTranslation } from "react-i18next";
import { FieldErrorMessage, InputV3, Label } from "@ndla/forms";
import { FormControl, FormField } from "../../../components/FormField";
import { FieldErrorMessage, FieldInput, FieldLabel, FieldRoot } from "@ndla/primitives";
import { FormField } from "../../../components/FormField";

const VersionNameField = () => {
const { t } = useTranslation();
return (
<FormField name="name">
{({ field, meta }) => (
<FormControl isRequired isInvalid={!!meta.error}>
<Label textStyle="label-small" margin="none">
{t("taxonomyVersions.form.name.label")}
</Label>
<InputV3 {...field} placeholder={t("taxonomyVersions.form.name.placeholder")} />
<FieldRoot required invalid={!!meta.error}>
<FieldLabel>{t("taxonomyVersions.form.name.label")}</FieldLabel>
<FieldErrorMessage>{meta.error}</FieldErrorMessage>
</FormControl>
<FieldInput {...field} placeholder={t("taxonomyVersions.form.name.placeholder")} />
</FieldRoot>
)}
</FormField>
);
Expand Down

0 comments on commit a9fbcb0

Please sign in to comment.