Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(list): Add uuid for each list item #179

Merged
merged 4 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@
"react-dom": "17.0.2"
},
"dependencies": {
"@types/uuid": "8.3.4",
"classnames": "2.3.1",
"react-textarea-autosize": "8.3.2"
"react-textarea-autosize": "8.3.2",
"uuid": "8.3.2"
},
"peerDependencies": {
"react": "17.0.2",
Expand Down
20 changes: 8 additions & 12 deletions src/list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import "./_list.scss";
import React, {Fragment} from "react";
import classNames from "classnames";

import {generateListItemKey} from "./util/listUtils";

export interface ListProps<Item = any> {
items: Item[];
children: (item: Item, testid: string, index?: number) => JSX.Element;
Expand Down Expand Up @@ -36,19 +38,13 @@ function List<Item extends any>({
<ul className={listClassName} role={role} data-testid={testid}>
{items.map((item: Item, index: number) => {
const listItemTestId = `${testid}.item-${index}`;
let key = listItemTestId;

// @ts-ignore
if (item && typeof item === "object" && item.id) {
// @ts-ignore
key = item.id;
}

if (listItemKeyGenerator) {
key = listItemKeyGenerator(item, listItemTestId);
}

return <Fragment key={key}>{children(item, listItemTestId, index)}</Fragment>;
return (
<Fragment
key={generateListItemKey({listItemKeyGenerator, listItemTestId, item})}>
{children(item, listItemTestId, index)}
</Fragment>
);
})}

{placeholderProps?.shouldDisplayPlaceholder && placeholderProps.placeholder}
Expand Down
27 changes: 27 additions & 0 deletions src/list/util/listUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {v4 as uuidv4} from "uuid";

import {ListProps} from "../List";

function generateListItemKey<Item = any>({
listItemKeyGenerator,
listItemTestId,
item
}: {
listItemKeyGenerator: ListProps["listItemKeyGenerator"];
listItemTestId: string;
item: Item;
}) {
let key = uuidv4();

if (listItemKeyGenerator) {
key = listItemKeyGenerator(item, listItemTestId);
// @ts-ignore
} else if (item && typeof item === "object" && item.id) {
// @ts-ignore
key = item.id;
}

return key;
}

export {generateListItemKey};
30 changes: 29 additions & 1 deletion stories/8-List.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {storiesOf} from "@storybook/react";

import List from "../src/list/List";
import ListItem from "../src/list/item/ListItem";
import {useState} from "@storybook/addons";
import Button from "../src/button/Button";
import StateProvider from "./utils/StateProvider";

const users = [
Expand Down Expand Up @@ -103,6 +103,20 @@ function ClickableUserListItem({user}) {
);
}

function RemovableUserListItem({user, onRemove}) {
return (
<ListItem>
<div style={{display: "flex", gap: "16px", alignItems: "center"}}>
{user.name}

<Button onClick={onRemove}>{"Remove"}</Button>
</div>

{style}
</ListItem>
);
}

storiesOf("List", module)
.add("Has Items", () => (
<Fragment>
Expand Down Expand Up @@ -143,4 +157,18 @@ storiesOf("List", module)

{style}
</Fragment>
))
.add("Removable Items", () => (
<StateProvider initialState={users}>
{(state, setState) => (
<List items={state}>
{(user) => (
<RemovableUserListItem
user={user}
onRemove={() => setState(state.filter((item) => item.id !== user.id))}
/>
)}
</List>
)}
</StateProvider>
));