The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/packages/grafana-ui/src/components/FileUpload/FileUpload.tsx

80 lines
2.1 KiB

import React, { FC, FormEvent, useCallback, useState } from 'react';
import { GrafanaTheme } from '@grafana/data';
import { css, cx } from 'emotion';
import { getFormStyles, Icon } from '../index';
import { stylesFactory, useTheme } from '../../themes';
export interface Props {
onFileUpload: (event: FormEvent<HTMLInputElement>) => void;
/** Accepted file extensions */
accept?: string;
className?: string;
}
function trimFileName(fileName: string) {
const nameLength = 16;
const delimiter = fileName.lastIndexOf('.');
const extension = fileName.substring(delimiter);
const file = fileName.substring(0, delimiter);
if (file.length < nameLength) {
return fileName;
}
return `${file.substring(0, nameLength)}...${extension}`;
}
export const FileUpload: FC<Props> = ({ onFileUpload, className, children = 'Upload file', accept = '*' }) => {
const theme = useTheme();
const style = getStyles(theme);
const [fileName, setFileName] = useState('');
const onChange = useCallback((event: FormEvent<HTMLInputElement>) => {
const file = event.currentTarget?.files?.[0];
if (file) {
setFileName(file.name ?? '');
}
onFileUpload(event);
}, []);
return (
<>
<label className={cx(style.button, className)}>
<Icon name="upload" className={style.icon} />
{children}
<input
type="file"
id="fileUpload"
className={style.fileUpload}
onChange={onChange}
multiple={false}
accept={accept}
/>
</label>
{fileName && (
<span aria-label="File name" className={style.fileName}>
{trimFileName(fileName)}
</span>
)}
</>
);
};
const getStyles = stylesFactory((theme: GrafanaTheme) => {
const buttonFormStyle = getFormStyles(theme, { variant: 'primary', invalid: false, size: 'md' }).button.button;
return {
fileUpload: css`
display: none;
`,
button: css`
${buttonFormStyle}
`,
icon: css`
margin-right: ${theme.spacing.xs};
`,
fileName: css`
margin-left: ${theme.spacing.xs};
`,
};
});