react-router-dom#resolvePath TypeScript Examples

The following examples show how to use react-router-dom#resolvePath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Items.tsx    From backstage with Apache License 2.0 6 votes vote down vote up
useLocationMatch = (
  submenu: React.ReactElement<SidebarSubmenuProps>,
  location: Location,
): boolean =>
  useElementFilter(
    submenu.props.children,
    elements => {
      let active = false;
      elements
        .getElements()
        .forEach(
          ({
            props: { to, dropdownItems },
          }: {
            props: Partial<SidebarSubmenuItemProps>;
          }) => {
            if (!active) {
              if (dropdownItems?.length) {
                dropdownItems.forEach(
                  ({ to: _to }) =>
                    (active =
                      active || isLocationMatch(location, resolvePath(_to))),
                );
                return;
              }
              if (to) {
                active = isLocationMatch(location, resolvePath(to));
              }
            }
          },
        );
      return active;
    },
    [location.pathname],
  )
Example #2
Source File: index.tsx    From backstage with Apache License 2.0 6 votes vote down vote up
AuditLinkList = ({ audits = [], selectedId }: AuditLinkListProps) => (
  <List
    data-testid="audit-sidebar"
    component="nav"
    aria-label="lighthouse audit history"
  >
    {audits.map(audit => (
      <ListItem
        key={audit.id}
        selected={audit.id === selectedId}
        button
        component={Link}
        replace
        to={resolvePath(generatePath('audit/:id', { id: audit.id }), '../../')}
      >
        <ListItemIcon>
          <AuditStatusIcon audit={audit} />
        </ListItemIcon>
        <ListItemText primary={formatTime(audit.timeCreated)} />
      </ListItem>
    ))}
  </List>
)
Example #3
Source File: SidebarSubmenuItem.tsx    From backstage with Apache License 2.0 4 votes vote down vote up
SidebarSubmenuItem = (props: SidebarSubmenuItemProps) => {
  const { title, to, icon: Icon, dropdownItems } = props;
  const classes = useStyles();
  const { setIsHoveredOn } = useContext(SidebarItemWithSubmenuContext);
  const closeSubmenu = () => {
    setIsHoveredOn(false);
  };
  const toLocation = useResolvedPath(to ?? '');
  const currentLocation = useLocation();
  let isActive = isLocationMatch(currentLocation, toLocation);

  const [showDropDown, setShowDropDown] = useState(false);
  const handleClickDropdown = () => {
    setShowDropDown(!showDropDown);
  };
  if (dropdownItems !== undefined) {
    dropdownItems.some(item => {
      const resolvedPath = resolvePath(item.to);
      isActive = isLocationMatch(currentLocation, resolvedPath);
      return isActive;
    });
    return (
      <div className={classes.itemContainer}>
        <Tooltip title={title} enterDelay={500} enterNextDelay={500}>
          <button
            onClick={handleClickDropdown}
            onTouchStart={e => e.stopPropagation()}
            className={classnames(
              classes.item,
              isActive ? classes.selected : undefined,
            )}
          >
            {Icon && <Icon fontSize="small" />}
            <Typography variant="subtitle1" className={classes.label}>
              {title}
            </Typography>
            {showDropDown ? (
              <ArrowDropUpIcon className={classes.dropdownArrow} />
            ) : (
              <ArrowDropDownIcon className={classes.dropdownArrow} />
            )}
          </button>
        </Tooltip>
        {dropdownItems && showDropDown && (
          <div className={classes.dropdown}>
            {dropdownItems.map((object, key) => (
              <Tooltip
                key={key}
                title={object.title}
                enterDelay={500}
                enterNextDelay={500}
              >
                <Link
                  to={object.to}
                  underline="none"
                  className={classes.dropdownItem}
                  onClick={closeSubmenu}
                  onTouchStart={e => e.stopPropagation()}
                >
                  <Typography className={classes.textContent}>
                    {object.title}
                  </Typography>
                </Link>
              </Tooltip>
            ))}
          </div>
        )}
      </div>
    );
  }

  return (
    <div className={classes.itemContainer}>
      <Tooltip title={title} enterDelay={500} enterNextDelay={500}>
        <Link
          to={to!}
          underline="none"
          className={classnames(
            classes.item,
            isActive ? classes.selected : undefined,
          )}
          onClick={closeSubmenu}
          onTouchStart={e => e.stopPropagation()}
        >
          {Icon && <Icon fontSize="small" />}
          <Typography variant="subtitle1" className={classes.label}>
            {title}
          </Typography>
        </Link>
      </Tooltip>
    </div>
  );
}