Skip to content
Snippets Groups Projects
Commit 8a55b76d authored by Max Frederiksen's avatar Max Frederiksen
Browse files

CE-3632: Tidy up AutoComplete stories, implement typing EllipsisText

parent faa5e7d4
No related branches found
No related tags found
No related merge requests found
Pipeline #219569 failed
...@@ -6,7 +6,7 @@ import { ...@@ -6,7 +6,7 @@ import {
AutocompleteProps as MuiAutocompleteProps AutocompleteProps as MuiAutocompleteProps
} from "@mui/material"; } from "@mui/material";
interface AutocompleteProps<Value> { export interface AutocompleteProps<Value> {
loading: boolean; loading: boolean;
autocompleteProps: MuiAutocompleteProps<Value, false, true, false>; autocompleteProps: MuiAutocompleteProps<Value, false, true, false>;
textFieldProps: TextFieldProps; textFieldProps: TextFieldProps;
......
import { Autocomplete } from "./Autocomplete"; import { Autocomplete, type AutocompleteProps } from "./Autocomplete";
export { Autocomplete }; export { Autocomplete, type AutocompleteProps };
import { Tooltip, Typography } from "@mui/material"; import {
import { string, object, bool, node } from "prop-types"; Tooltip,
Typography,
TypographyProps,
TooltipProps
} from "@mui/material";
export interface EllipsisTextProps {
title?: string;
disableTooltip?: boolean;
TypographyProps?: TypographyProps;
TooltipProps?: TooltipProps;
children: string;
}
/**
* Displays cutoff text with an ellipsis and a tooltip with the rest of the text.
* @param {string} title text to display in the tooltip. The child element is used
* by default so title is only necessary if the child is a React component.
* @param {boolean} disableTooltip disables the tooltip feature.
* @param {object} TypographyProps Additional props for the Typography component
* @param {object} TooltipProps Additional props for the Tooltip component
*/
export const EllipsisText = ({ export const EllipsisText = ({
title, title,
disableTooltip = false, disableTooltip = false,
TypographyProps, TypographyProps,
TooltipProps, TooltipProps,
children children
}) => { }: EllipsisTextProps) => {
const renderedText = ( const renderedText = (
<Typography <Typography
noWrap noWrap
...@@ -39,10 +43,3 @@ export const EllipsisText = ({ ...@@ -39,10 +43,3 @@ export const EllipsisText = ({
</Tooltip> </Tooltip>
); );
}; };
EllipsisText.propTypes = {
title: string,
disableTooltip: bool,
TypographyProps: object,
TooltipProps: object,
children: node
};
import { EllipsisText } from "./EllipsisText"; import { EllipsisText, type EllipsisTextProps } from "./EllipsisText";
export { EllipsisText }; export { EllipsisText, type EllipsisTextProps };
import { ReactNode } from "react";
import { Paper, Typography } from "@mui/material"; import { Paper, Typography } from "@mui/material";
import { MemoryRouter } from "react-router-dom"; import { MemoryRouter } from "react-router-dom";
import { EllipsisText } from "../../../components/common/EllipsisText"; import { Meta } from "@storybook/react";
import {
EllipsisText,
EllipsisTextProps
} from "../../../components/common/EllipsisText";
import { ExternalLink, InternalLink } from "../../../components/common/Link"; import { ExternalLink, InternalLink } from "../../../components/common/Link";
export default { export default {
title: "Common/EllipsisText", title: "Common/EllipsisText",
component: EllipsisText component: EllipsisText
}; } as Meta<typeof EllipsisText>;
interface ContainerProps {
children: ReactNode;
}
const Container = ({ children }) => { const Container = ({ children }: ContainerProps) => {
return ( return (
<Paper sx={{ height: "100vh", padding: 2 }}> <Paper sx={{ height: "100vh", padding: 2 }}>
<Typography>The following text is too long:</Typography> <Typography>The following text is too long:</Typography>
...@@ -28,17 +37,18 @@ const Container = ({ children }) => { ...@@ -28,17 +37,18 @@ const Container = ({ children }) => {
); );
}; };
const TextTemplate = ({ text, ...props }) => { const TextTemplate = ({ children, ...props }: EllipsisTextProps) => {
return ( return (
<Container> <Container>
<EllipsisText {...props}>{text}</EllipsisText> <EllipsisText {...props}>{children}</EllipsisText>
</Container> </Container>
); );
}; };
export const Default = (args) => <TextTemplate {...args} />; export const Default = (args: EllipsisTextProps) => <TextTemplate {...args} />;
Default.args = { Default.args = {
text: "There's some really important information here but it is obscured!", children:
"There's some really important information here but it is obscured!",
TypographyProps: { TypographyProps: {
variant: "body2" variant: "body2"
}, },
...@@ -48,43 +58,43 @@ Default.args = { ...@@ -48,43 +58,43 @@ Default.args = {
} }
}; };
const ExternalLinkTemplate = ({ text, ...props }) => { const ExternalLinkTemplate = ({ children, ...props }: EllipsisTextProps) => {
return ( return (
<Container> <Container>
<ExternalLink <ExternalLink
newTab newTab
href="https://europeanspallationsource.se/" href="https://europeanspallationsource.se/"
label={text} label={children}
> >
<EllipsisText {...props}>{text}</EllipsisText> <EllipsisText {...props}>{children}</EllipsisText>
</ExternalLink> </ExternalLink>
</Container> </Container>
); );
}; };
export const ExternalLinkEllipsisText = (args) => ( export const ExternalLinkEllipsisText = (args: EllipsisTextProps) => (
<ExternalLinkTemplate {...args} /> <ExternalLinkTemplate {...args} />
); );
ExternalLinkEllipsisText.args = { ExternalLinkEllipsisText.args = {
...Default.args ...Default.args
}; };
const InternalLinkTemplate = ({ text, ...props }) => { const InternalLinkTemplate = ({ children, ...props }: EllipsisTextProps) => {
return ( return (
<MemoryRouter> <MemoryRouter>
<Container> <Container>
<InternalLink <InternalLink
to="/" to="/"
label={text} label={children}
> >
<EllipsisText {...props}>{text}</EllipsisText> <EllipsisText {...props}>{children}</EllipsisText>
</InternalLink> </InternalLink>
</Container> </Container>
</MemoryRouter> </MemoryRouter>
); );
}; };
export const InternalLinkEllipsisText = (args) => ( export const InternalLinkEllipsisText = (args: EllipsisTextProps) => (
<InternalLinkTemplate {...args} /> <InternalLinkTemplate {...args} />
); );
InternalLinkEllipsisText.args = { InternalLinkEllipsisText.args = {
......
...@@ -25,13 +25,13 @@ const data = [ ...@@ -25,13 +25,13 @@ const data = [
]; ];
interface AutocompleteProps { interface AutocompleteProps {
loading?: boolean; loading: boolean;
} }
const Template = ({ loading, ...args }: AutocompleteProps) => { const Template = ({ loading, ...args }: AutocompleteProps) => {
return ( return (
<Autocomplete <Autocomplete
loading={loading || false} loading={loading}
autocompleteProps={{ autocompleteProps={{
options: data, options: data,
getOptionLabel: (option) => { getOptionLabel: (option) => {
...@@ -57,7 +57,12 @@ const Template = ({ loading, ...args }: AutocompleteProps) => { ...@@ -57,7 +57,12 @@ const Template = ({ loading, ...args }: AutocompleteProps) => {
); );
}; };
export const Default = (args: typeof Autocomplete) => <Template {...args} />; export const Default = (args: typeof Autocomplete) => (
<Template
loading={false}
{...args}
/>
);
export const Loading = (args: typeof Autocomplete) => ( export const Loading = (args: typeof Autocomplete) => (
<Template <Template
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment