Files
onspring-api-docs/src/app/components/SectionDropdown.tsx
T

66 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Fragment } from 'react';
2023-04-27 16:03:27 -05:00
import { useSectionContext } from '../context/section';
import { Doc, DocsStructure } from '../types/types';
2023-04-27 16:03:27 -05:00
import { toKebabCase } from '../utils/stringUtils';
import styles from './SectionDropdown.module.css';
function SectionDropdownOption({ doc }: { doc: Doc }) {
return (
<Fragment>
{doc.copy && doc.example && (
<option
className={styles.selectItem}
label={doc.title}
2023-04-27 16:03:27 -05:00
value={toKebabCase(doc.title)}
>
{doc.title}
</option>
)}
{doc.children && doc.children.length > 0 && (
<optgroup label={doc.title}>
{doc.children.map(child => (
<option
2023-04-27 16:03:27 -05:00
value={toKebabCase(child.title)}
className={styles.selectItem}
label={child.title}
key={child.title}
>
{child.title}
</option>
))}
</optgroup>
)}
</Fragment>
);
}
export default function SectionDropdown({
version,
}: {
version: DocsStructure;
}) {
2023-04-27 16:03:27 -05:00
const { section, setSection } = useSectionContext();
const handleSelectChange = (e: any) => {
2023-04-27 16:03:27 -05:00
setSection(e.target.value);
location.hash = e.target.value;
};
return (
<select
onChange={handleSelectChange}
title="navigation dropdown"
className={styles.dropdownSelect}
2023-04-27 16:03:27 -05:00
value={section}
>
{version.docs.map(doc => {
return (
<SectionDropdownOption
doc={doc}
key={doc.title}
/>
);
})}
</select>
);
}