Skip to content

Commit

Permalink
fix: reintroduce dropped typeahead stories (#721)
Browse files Browse the repository at this point in the history
During a refactor a few of these examples got dropped and stopped
functioning. Adding them back in to help with regression testing of the
downshift upgrade.

Additionally I discovered that the complex list items example doesn't
work even when adding back in args to the default. This apparently has
not worked since some time in 2019, as of the following commit 1c01ea9

Thus, I removed it completely rather than trying to untangle it. We
evidently have not needed it.
  • Loading branch information
Russell Anderson authored May 20, 2022
1 parent ac511ba commit 9a3511b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as React from "react";
import { Item } from "../../../typeahead/components/Typeahead";
import { BadgeDatum } from "../../components/TextInputWithBadges";
import { items } from "../../../typeahead/stories/helpers/itemMocks";

interface RenderProps {
selectedItems: string[];
Expand Down
86 changes: 56 additions & 30 deletions packages/typeahead/stories/Typeahead.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import { Typeahead, TextInput } from "../../index";
import MultiselectTypeahead from "./helpers/MultiselectTypeahead";
import FilteredListTypeahead from "./helpers/FilteredListTypeahead";
import { padding } from "../../shared/styles/styleUtils";
import { complexItems, items } from "./helpers/itemMocks";
import { css } from "@emotion/css";

const storyWrapper = css`
width: 300px;
`;

const items = [
{ label: "Exosphere", value: "Exosphere" },
{ label: "Thermosphere", value: "Thermosphere" },
{ label: "Mesosphere", value: "Mesosphere" },
{ label: "Stratosphere", value: "Stratosphere" },
{ label: "Ozone Layer", value: "Ozone Layer" },
{ label: "Troposphere", value: "Troposphere" }
];

export default {
title: "Forms/Typeahead",
component: Typeahead,
Expand All @@ -23,14 +31,22 @@ const Template: Story = args => (
textField={
<TextInput id="default" inputLabel="Default" placeholder="Placeholder" />
}
{...args}
/>
);

export const Default = Template.bind({});

export const MenuHasMaxHeight = Template.bind({});
MenuHasMaxHeight.args = {
menuMaxHeight: 100
menuMaxHeight: 100,
textField: (
<TextInput
id="maxHeight"
inputLabel="Menu max height"
placeholder="Placeholder"
/>
)
};

export const PreFilledSelectedItem = args => (
Expand All @@ -50,45 +66,55 @@ export const PreFilledSelectedItem = args => (
/>
);

export const Multiselect = () => (
<div className={storyWrapper}>
<MultiselectTypeahead>
{({ items, selectHandler, selectedItems }) => (
<Typeahead
items={items}
selectedItems={selectedItems}
multiSelect={true}
textField={
<TextInput
id="multiselect"
inputLabel="Multiselectable"
placeholder="Placeholder"
/>
}
onSelect={selectHandler}
/>
)}
</MultiselectTypeahead>
</div>
);

const onSelectHandler = selectedItems => {
alert(`${selectedItems[0]} selected`);
};

export const WithOnSelectCallback = Template.bind({});
WithOnSelectCallback.args = {
onSelect: onSelectHandler,
items
textField: (
<TextInput id="onselect" inputLabel="onSelect" placeholder="Placeholder" />
)
};

export const ComplexListItems = Template.bind({});
ComplexListItems.args = {
items: complexItems
};
export const Multiselect = () => (
<MultiselectTypeahead>
{({ onSelect, selectedItems }) => (
<Typeahead
items={items}
selectedItems={selectedItems}
multiSelect={true}
textField={
<TextInput
id="multiselect"
inputLabel="Multiselectable"
placeholder="Placeholder"
/>
}
onSelect={onSelect}
/>
)}
</MultiselectTypeahead>
);

export const WithDisabledItem = Template.bind({});
WithDisabledItem.args = {
items: [...items, { label: "K8sphere", value: "K8sphere", disabled: true }]
items: [...items, { label: "K8sphere", value: "K8sphere", disabled: true }],
textField: (
<TextInput
id="withdisabled"
inputLabel="With a disabled item"
placeholder="Placeholder"
/>
)
};

export const FilterWhileTyping = () => <FilteredListTypeahead items={items} />;

export const CustomMenuEmptyState = () => (
<FilteredListTypeahead
menuEmptyState={<div className={padding("all")}>Nothing to show</div>}
items={items}
/>
);
73 changes: 25 additions & 48 deletions packages/typeahead/stories/helpers/MultiselectTypeahead.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,46 @@
import * as React from "react";
import { cx } from "@emotion/css";
import { Item } from "../../components/Typeahead";
import {
flex,
flexItem,
padding,
textWeight
} from "../../../shared/styles/styleUtils";
import { items } from "./itemMocks";

interface RenderProps {
items: Item[];
selectedItems: string[];
selectHandler: (selectedItems: string[]) => void;
onSelect: (selectedItems: string[]) => void;
}

interface MultiselectTypeaheadProps {
children: (renderProps: RenderProps) => React.ReactNode;
}

interface MultiselectTypeaheadState {
selectedItems: string[];
}

class MultiselectTypeahead extends React.PureComponent<
MultiselectTypeaheadProps,
MultiselectTypeaheadState
> {
constructor(props) {
super(props);

this.selectHandler = this.selectHandler.bind(this);
const MultiSelectTypeahead = ({ children }: MultiselectTypeaheadProps) => {
const [selectedItems, setSelectedItems] = React.useState<string[]>([]);

this.state = {
selectedItems: props.selectedItems || []
};
}
const handleSelect = newlySelectedItems => {
setSelectedItems(newlySelectedItems);
};

selectHandler(selectedItems) {
this.setState({ selectedItems });
}

render() {
const { children } = this.props;

return (
<div className={flex()}>
<div className={flexItem("grow")}>
{children({
selectedItems: this.state.selectedItems,
selectHandler: this.selectHandler,
items
})}
</div>
<div className={cx(flexItem("grow"), padding("left"))}>
<span className={textWeight("medium")}>Selections:</span>
<ul>
{this.state.selectedItems.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</div>
return (
<div className={flex()}>
<div className={flexItem("grow")}>
{children({
selectedItems,
onSelect: handleSelect
})}
</div>
);
}
}
<div className={cx(flexItem("grow"), padding("left"))}>
<span className={textWeight("medium")}>Selections:</span>
<ul>
{selectedItems.map((item, i) => (
<li key={i}>{item}</li>
))}
</ul>
</div>
</div>
);
};

export default MultiselectTypeahead;
export default React.memo(MultiSelectTypeahead);
39 changes: 0 additions & 39 deletions packages/typeahead/stories/helpers/itemMocks.tsx

This file was deleted.

0 comments on commit 9a3511b

Please sign in to comment.