grafana/public/app/features/storage/Breadcrumb.tsx
Ashley Harrison 514d3111f4
Accessibility: Enable rule jsx-a11y/no-noninteractive-element-interactions (#58077)
* fixes for no-noninteractive-element-interactions

* remaining fixes

* add type="button"

* fix unit tests
2022-11-03 10:55:58 +00:00

61 lines
1.5 KiB
TypeScript

import { css } from '@emotion/css';
import { uniqueId } from 'lodash';
import React from 'react';
import { GrafanaTheme2 } from '@grafana/data';
import { Icon, IconName, useStyles2 } from '@grafana/ui';
interface Props {
rootIcon?: IconName;
pathName: string;
onPathChange: (path: string) => void;
}
export function Breadcrumb({ pathName, onPathChange, rootIcon }: Props) {
const styles = useStyles2(getStyles);
const paths = pathName.split('/').filter(Boolean);
return (
<ul className={styles.breadCrumb}>
{rootIcon && (
<li>
<Icon name={rootIcon} onClick={() => onPathChange('')} />
</li>
)}
{paths.map((path, index) => {
let url = '/' + paths.slice(0, index + 1).join('/');
const onClickBreadcrumb = () => onPathChange(url);
const isLastBreadcrumb = index === paths.length - 1;
return (
<li key={uniqueId(path)} onClick={isLastBreadcrumb ? undefined : onClickBreadcrumb}>
{path}
</li>
);
})}
</ul>
);
}
function getStyles(theme: GrafanaTheme2) {
return {
breadCrumb: css`
list-style: none;
padding: ${theme.spacing(2, 1)};
li {
display: inline;
:not(:last-child) {
color: ${theme.colors.text.link};
cursor: pointer;
}
+ li:before {
content: '>';
padding: ${theme.spacing(1)};
color: ${theme.colors.text.secondary};
}
}
`,
};
}