Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

5.2 KB
Raw Blame
import { Heading, Input, Textarea } from "@medusajs/ui";
import { useWatch } from "react-hook-form";
import { useTranslation } from "react-i18next";

import { Form } from "../../../../components/common/form";
import { SwitchBox } from "../../../../components/common/switch-box";
import { HandleInput } from "../../../../components/inputs/handle-input";
import { CategoryCombobox } from "../../../products/common/components/category-combobox";
import { useTabbedForm } from "../../../../components/tabbed-form/tabbed-form";
import { defineTabMeta } from "../../../../components/tabbed-form/types";

type AttributeCreateFormValues = {
  name: string;
  handle?: string;
  description?: string;
  is_filterable: boolean;
  is_required: boolean;
  is_variant_axis: boolean;
  is_global: boolean;
  category_ids?: string[];
};

const Root = () => {
  const { t } = useTranslation();
  const form = useTabbedForm<AttributeCreateFormValues>();
  const isGlobal = useWatch({ control: form.control, name: "is_global" });

  return (
    <div
      className="flex flex-col items-center p-16"
      data-testid="attribute-create-details-tab"
    >
      <div className="flex w-full max-w-[720px] flex-col gap-y-8">
        <div>
          <Heading level="h2">{t("attributes.create.tabs.details")}</Heading>
        </div>

        <div className="grid grid-cols-1 gap-4 md:grid-cols-2">
          <Form.Field
            control={form.control}
            name="name"
            render={({ field }) => (
              <Form.Item>
                <Form.Label>{t("fields.title")}</Form.Label>
                <Form.Control>
                  <Input {...field} data-testid="attribute-create-name-input" />
                </Form.Control>
                <Form.ErrorMessage />
              </Form.Item>
            )}
          />
          <Form.Field
            control={form.control}
            name="handle"
            render={({ field }) => (
              <Form.Item>
                <Form.Label optional>
                  {t("attributes.fields.handle")}
                </Form.Label>
                <Form.Control>
                  <HandleInput
                    {...field}
                    data-testid="attribute-create-handle-input"
                  />
                </Form.Control>
                <Form.ErrorMessage />
              </Form.Item>
            )}
          />
        </div>

        <Form.Field
          control={form.control}
          name="description"
          render={({ field }) => (
            <Form.Item>
              <Form.Label optional>
                {t("attributes.fields.description")}
              </Form.Label>
              <Form.Control>
                <Textarea
                  {...field}
                  data-testid="attribute-create-description-input"
                />
              </Form.Control>
              <Form.ErrorMessage />
            </Form.Item>
          )}
        />

        <div className="flex flex-col gap-y-4">
          <SwitchBox
            control={form.control}
            name="is_required"
            label={t("attributes.fields.isRequired")}
            description={t(
              "attributes.fields.isRequiredHint",
              "If checked, a value must be set to this attribute.",
            )}
            data-testid="attribute-create-required-switch"
          />

          <SwitchBox
            control={form.control}
            name="is_filterable"
            label={t("attributes.fields.isFilterable")}
            description={t(
              "attributes.fields.isFilterableHint",
              "If checked, customers will be able to filter products using this attribute.",
            )}
            data-testid="attribute-create-filterable-switch"
          />

          <SwitchBox
            control={form.control}
            name="is_global"
            label={t("attributes.fields.isGlobal")}
            description={t(
              "attributes.fields.isGlobalHint",
              "If checked, this attribute applies to all products across all categories.",
            )}
            onCheckedChange={(checked) => {
              if (checked) {
                form.setValue("category_ids", [], {
                  shouldDirty: true,
                  shouldValidate: true,
                });
              }
            }}
            data-testid="attribute-create-global-switch"
          />
        </div>

        {!isGlobal && (
          <Form.Field
            control={form.control}
            name="category_ids"
            render={({ field }) => (
              <Form.Item>
                <Form.Label>{t("attributes.fields.categories")}</Form.Label>
                <Form.Control>
                  <CategoryCombobox
                    value={field.value ?? []}
                    onChange={field.onChange}
                    data-testid="attribute-create-categories-combobox"
                  />
                </Form.Control>
                <Form.ErrorMessage />
              </Form.Item>
            )}
          />
        )}
      </div>
    </div>
  );
};

Root._tabMeta = defineTabMeta<AttributeCreateFormValues>({
  id: "details",
  labelKey: "attributes.create.tabs.details",
  validationFields: ["name", "category_ids"],
});

export const AttributeCreateDetailsTab = Root;