feat(examples): title set based on example

This commit is contained in:
Alireza Sheikholmolouki
2022-10-04 16:07:00 +02:00
parent 0afaf550a4
commit a743b2ef36
+55 -6
View File
@@ -1,4 +1,4 @@
import { ChangeEvent } from 'react'; import { useEffect, useState } from 'react';
import { BrowserRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom'; import { BrowserRouter, Route, Routes, useLocation, useNavigate } from 'react-router-dom';
import Basic from '../examples/Basic'; import Basic from '../examples/Basic';
@@ -36,159 +36,208 @@ import Validation from '../examples/Validation';
import UseKeyPress from '../examples/UseKeyPress'; import UseKeyPress from '../examples/UseKeyPress';
import EdgeRouting from '../examples/EdgeRouting'; import EdgeRouting from '../examples/EdgeRouting';
const routes = [ interface IRoute {
name: string;
path: string;
component: React.ComponentType;
}
const routes: IRoute[] = [
{ {
name: 'Basic',
path: '/', path: '/',
component: Basic, component: Basic,
}, },
{ {
name: 'Backgrounds',
path: '/backgrounds', path: '/backgrounds',
component: Backgrounds, component: Backgrounds,
}, },
{ {
name: 'Controlled/Uncontrolled',
path: '/controlled-uncontrolled', path: '/controlled-uncontrolled',
component: ControlledUncontrolled, component: ControlledUncontrolled,
}, },
{ {
name: 'Custom Connection Line',
path: '/custom-connectionline', path: '/custom-connectionline',
component: CustomConnectionLine, component: CustomConnectionLine,
}, },
{ {
name: 'Custom Node',
path: '/custom-node', path: '/custom-node',
component: CustomNode, component: CustomNode,
}, },
{ {
name: 'Default Nodes',
path: '/default-nodes', path: '/default-nodes',
component: DefaultNodes, component: DefaultNodes,
}, },
{ {
name: 'Drag Handle',
path: '/draghandle', path: '/draghandle',
component: DragHandle, component: DragHandle,
}, },
{ {
name: 'Drag and Drop',
path: '/dragndrop', path: '/dragndrop',
component: DragNDrop, component: DragNDrop,
}, },
{ {
name: 'Edges',
path: '/edges', path: '/edges',
component: Edges, component: Edges,
}, },
{ {
name: 'Edge Types',
path: '/edge-types', path: '/edge-types',
component: EdgeTypes, component: EdgeTypes,
}, },
{ {
name: 'Edge Routing',
path: '/edge-routing', path: '/edge-routing',
component: EdgeRouting, component: EdgeRouting,
}, },
{ {
name: 'Empty',
path: '/empty', path: '/empty',
component: Empty, component: Empty,
}, },
{ {
name: 'Floating Edges',
path: '/floating-edges', path: '/floating-edges',
component: FloatingEdges, component: FloatingEdges,
}, },
{ {
name: 'Hidden',
path: '/hidden', path: '/hidden',
component: Hidden, component: Hidden,
}, },
{ {
name: 'Interaction',
path: '/interaction', path: '/interaction',
component: Interaction, component: Interaction,
}, },
{ {
name: 'Layouting',
path: '/layouting', path: '/layouting',
component: Layouting, component: Layouting,
}, },
{ {
name: 'Multi Flows',
path: '/multiflows', path: '/multiflows',
component: MultiFlows, component: MultiFlows,
}, },
{ {
name: 'Nested Nodes',
path: '/nested-nodes', path: '/nested-nodes',
component: NestedNodes, component: NestedNodes,
}, },
{ {
name: 'Node Type Change',
path: '/nodetype-change', path: '/nodetype-change',
component: NodeTypeChange, component: NodeTypeChange,
}, },
{ {
name: 'nodeTypes Object Change',
path: '/nodetypesobject-change', path: '/nodetypesobject-change',
component: NodeTypesObjectChange, component: NodeTypesObjectChange,
}, },
{ {
name: 'Overview',
path: '/overview', path: '/overview',
component: Overview, component: Overview,
}, },
{ {
name: 'Provider',
path: '/provider', path: '/provider',
component: Provider, component: Provider,
}, },
{ {
name: 'Save/Restore',
path: '/save-restore', path: '/save-restore',
component: SaveRestore, component: SaveRestore,
}, },
{ {
name: 'Stress',
path: '/stress', path: '/stress',
component: Stress, component: Stress,
}, },
{ {
name: 'Subflow',
path: '/subflow', path: '/subflow',
component: Subflow, component: Subflow,
}, },
{ {
name: 'Switch Flow',
path: '/switch', path: '/switch',
component: SwitchFlow, component: SwitchFlow,
}, },
{ {
name: 'Touch Device',
path: '/touch-device', path: '/touch-device',
component: TouchDevice, component: TouchDevice,
}, },
{ {
name: 'Undirectional',
path: '/undirectional', path: '/undirectional',
component: Undirectional, component: Undirectional,
}, },
{ {
name: 'Updatable Edge',
path: '/updatable-edge', path: '/updatable-edge',
component: UpdatableEdge, component: UpdatableEdge,
}, },
{ {
name: 'Update Node',
path: '/update-node', path: '/update-node',
component: UpdateNode, component: UpdateNode,
}, },
{ {
name: 'useReactFlow',
path: '/usereactflow', path: '/usereactflow',
component: UseReactFlow, component: UseReactFlow,
}, },
{ {
name: 'useUpdateNodeInternals',
path: '/useupdatenodeinternals', path: '/useupdatenodeinternals',
component: UseUpdateNodeInternals, component: UseUpdateNodeInternals,
}, },
{ {
name: 'Validation',
path: '/validation', path: '/validation',
component: Validation, component: Validation,
}, },
{ {
name: 'useKeyPress',
path: '/use-key-press', path: '/use-key-press',
component: UseKeyPress, component: UseKeyPress,
}, },
]; ];
const Header = () => { const Header = () => {
const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const onChange = (event: ChangeEvent<HTMLSelectElement>) => navigate(event.target.value); const navigate = useNavigate();
const [currentPath, setCurrentPath] = useState(location.pathname);
useEffect(() => {
const name = routes.find((route) => route.path === currentPath)?.name;
document.title = `React Flow Examples${name ? ' - ' + name : ''}`;
navigate(currentPath)
}, [currentPath]);
return ( return (
<header> <header>
<a className="logo" href="https://github.com/wbkd/react-flow"> <a className="logo" href="https://github.com/wbkd/react-flow">
React Flow Dev React Flow Dev
</a> </a>
<select defaultValue={location.pathname} onChange={onChange}> <select
value={currentPath}
onChange={(event) => setCurrentPath(event.target.value)}
>
{routes.map((route) => ( {routes.map((route) => (
<option value={route.path} key={route.path}> <option value={route.path} key={route.path}>
{route.path === '/' ? 'basic' : route.path.substring(1, route.path.length)} {route.name}
</option> </option>
))} ))}
</select> </select>