/** * based on react-sidebar-v2: https://github.com/condense/react-leaflet-sidebarv2/blob/master/src/Sidebar.js * converting prop-types to typescript, additional features, cleanning typescript errors, and converting to function component */ import React from 'react'; // import { MapComponent } from 'react-leaflet' //resize CSS import './sidebar-resize.css'; //re-resizable import { Resizable } from 're-resizable'; const resizeRightOnly = { top: false, right: true, bottom: false, left: false, topRight: false, bottomRight: false, bottomLeft: false, topLeft: false, }; /** * Type definitions for react-leaflet-sidebarv2 0.6, taken from Definitely Typed * But some modifications are made * Project: https://github.com/condense/react-leaflet-sidebarv2 * Definitions by: Vikram Pareddy * Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped */ type Icon = string | React.ReactElement; type Anchor = 'top' | 'bottom'; type Position = 'left' | 'right'; type TabType = React.ReactElement | Array; export interface TabProps { id: string; header: string; icon: Icon; anchor?: Anchor; disabled?: boolean; onClose?: () => void; closeIcon?: Icon; position?: Position; active?: boolean; children?: TabType; } export interface SidebarProps { id: string; collapsed: boolean; position: Position; selected: string; closeIcon?: Icon; onClose?: () => void; onOpen?: (id: string) => void; //change children as optional for component children?: TabType; } //extend TabProps to have divider prop interface TabPropsAdd extends TabProps { //divider=true, then use divider divider?: boolean; } //change from TabProps to TabPropsAdd for considering divider icon export function Tab(props: TabPropsAdd) { const active = props.active ? ' active' : ''; let closeIcon; if (typeof props.closeIcon === 'string') closeIcon = ; else if (typeof props.closeIcon === 'object') closeIcon = props.closeIcon; else { //change fontawesome fa to fas const closecls = props.position === 'right' ? 'fas fa-caret-right' : 'fas fa-caret-left'; closeIcon = ; } return ( // change className

{props.header}
{closeIcon}

{props.children}
); } //using type definition from type-react-leaflet-sidebarv2.ts // // https://github.com/facebook/react/issues/2979#issuecomment-222379916 // const TabType = PropTypes.shape({ // type: PropTypes.oneOf([Tab]) // }); // type TabType = React.ReactElement | Array>; //changed MapComponent to React.Component export function Sidebar(props: SidebarProps) { function onClose(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); props.onClose && props.onClose(); } function onOpen(e: React.MouseEvent, tabid: string) { e.preventDefault(); e.stopPropagation(); props.onOpen && props.onOpen(tabid); } //set tab as any for now function renderTab(tab: any) { let icon; if (typeof tab.props.icon === 'string') icon = ; else if (typeof tab.props.icon === 'object') icon = tab.props.icon; const active = tab.props.id === props.selected ? ' active' : ''; const disabled = tab.props.disabled ? ' disabled' : ''; //line divider using image file (made by DK) if (tab.props.disabled && tab.props.divider) { return (
  • tab.props.disabled || onOpen(e, tab.props.id)} >
  • ); } else { return ( //add title attribute here for tooltip effect
  • tab.props.disabled || onOpen(e, tab.props.id)} > {icon}
  • ); } } //children here is a content inside so set it as any for now function renderPanes(children: any) { return React.Children.map(children, (p) => React.cloneElement(p, { onClose: onClose, closeIcon: props.closeIcon, active: p.props.id === props.selected, position: props.position || 'left', }) ); } // Override render() so the element contains a div we can render to //change sidebar -> leaflet-sidebar const position = ' leaflet-sidebar-' + (props.position || 'left'); const collapsed = props.collapsed ? ' collapsed' : ''; const allTabs = React.Children.toArray(props.children).filter((c) => !!c); //for now tab type at filter() is set to any for avoiding the type error on tab.props const bottomtabs = allTabs.filter( (tab: any) => tab.props.anchor === 'bottom' ); const toptabs = allTabs.filter((tab: any) => tab.props.anchor !== 'bottom'); return ( // change className; not clear why ref is used here so it is removed for now //
    this.rootElement = el}>
      {' '} {/* Top-aligned */} {toptabs.map((toptab) => renderTab(toptab))}
      {' '} {/* Bottom-aligned */} {bottomtabs.map((bottomtab) => renderTab(bottomtab))}
    {/* set re-resizable here for implementing resize functionality */} {renderPanes(allTabs)} {/* icon for showing two vertical bar like displaying resizable */}
    ); }