Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

1.6 KB
Raw Blame
import { useField } from "formik";
import type { HTMLAttributes } from "react";
import { t } from "ttag";

import { useUniqueId } from "metabase/common/hooks/use-unique-id";
import { FormField } from "metabase/forms";
import { SegmentedControl } from "metabase/ui";

import { OFFICIAL_COLLECTION, REGULAR_COLLECTION } from "../constants";

interface Props extends HTMLAttributes<HTMLDivElement> {
  name: string;
  title?: string;
}

const getOptions = () => [
  {
    label: REGULAR_COLLECTION.name,
    value: REGULAR_COLLECTION.type,
    icon: REGULAR_COLLECTION.icon,
  },
  {
    label: OFFICIAL_COLLECTION.name,
    value: OFFICIAL_COLLECTION.type,
    icon: OFFICIAL_COLLECTION.icon,
  },
];

export function FormCollectionAuthorityLevel({
  className,
  style,
  name = "authority_level",
  title = t`Collection type`,
}: Props) {
  const id = useUniqueId();
  const [{ value }, { error, touched }, { setValue }] = useField(name);

  const handleChange = (val: string) => {
    setValue(val === "null" ? null : val);
  };

  return (
    <FormField
      className={className}
      style={style}
      title={title}
      htmlFor={id}
      infoTooltip={t`The contents of Official collections will get a badge by their name and will be more likely to show up in search results.`}
      error={touched ? error : undefined}
    >
      <SegmentedControl
        w="fit-content"
        value={String(value)}
        onChange={handleChange}
        data={getOptions().map((option) => ({
          value: String(option.value),
          label: option.label,
          icon: option.icon,
        }))}
      />
    </FormField>
  );
}