aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Buttons/ButtonSubmit/ButtonSubmit.tsx
blob: 4725cad2552a2529e50eabcf597fe2f0a24a409e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { ReactNode } from 'react';
import styles from '../Buttons.module.scss';

type Modifier = 'search' | 'submit';

const ButtonSubmit = ({
  children,
  modifier = 'submit',
}: {
  children: ReactNode;
  modifier?: Modifier;
}) => {
  const withModifier = modifier === 'search' ? styles.search : styles.primary;

  return (
    <button type="submit" className={`${styles.btn} ${withModifier}`}>
      {children}
    </button>
  );
};

export default ButtonSubmit;
Long */
import { type ForwardRefRenderFunction, forwardRef } from 'react';
import type { HeadingsTreeNode } from '../../../../utils/hooks';
import {
  LinksWidget,
  type LinksWidgetItemData,
  type LinksWidgetProps,
} from '../links-widget';
import styles from './toc-widget.module.scss';

const getLinksItemFrom = (tree: HeadingsTreeNode[]): LinksWidgetItemData[] =>
  tree.map((node) => {
    return {
      child: node.children.length ? getLinksItemFrom(node.children) : undefined,
      id: node.id,
      label: node.label,
      url: `#${node.id}`,
    };
  });

export type TocWidgetProps = Omit<LinksWidgetProps, 'isOrdered' | 'items'> & {
  /**
   * The headings tree.
   */
  tree: HeadingsTreeNode[];
};

const TocWidgetWithRef: ForwardRefRenderFunction<
  HTMLDivElement,
  TocWidgetProps
> = ({ className = '', tree, ...props }, ref) => {
  const wrapperClass = `${styles.toc} ${className}`;

  return (
    <LinksWidget
      {...props}
      className={wrapperClass}
      isOrdered
      items={getLinksItemFrom(tree)}
      ref={ref}
    />
  );
};

export const TocWidget = forwardRef(TocWidgetWithRef);