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

62 lines
1.3 KiB
TypeScript
Raw Normal View History

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';
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;
};
2023-04-28 07:54:37 -05:00
const getAllDocs = (docs: Doc[]) => {
let result: Doc[] = [];
docs.forEach(doc => {
result.push(doc);
if (doc.children && doc.children.length > 0) {
result = result.concat(getAllDocs(doc.children));
}
});
return result;
};
const docs = getAllDocs(version.docs);
return (
<select
onChange={handleSelectChange}
title="navigation dropdown"
className={styles.dropdownSelect}
2023-04-27 16:03:27 -05:00
value={section}
>
2023-04-28 07:54:37 -05:00
{docs.map(doc => {
if (
doc.copy === undefined ||
doc.example === undefined
) {
return null;
}
return (
2023-04-28 07:54:37 -05:00
<option
className={styles.selectItem}
label={doc.title}
value={toKebabCase(doc.title)}
key={doc.title}
2023-04-28 07:54:37 -05:00
>
{doc.title}
</option>
);
})}
</select>
);
}