-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,795 additions
and
365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
web/src/beta/components/fields/ListField/index.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useArgs } from "@storybook/preview-api"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
import { useCallback } from "react"; | ||
|
||
import { styled } from "@reearth/services/theme"; | ||
|
||
import ListField, { Props } from "."; | ||
|
||
const meta: Meta<typeof ListField> = { | ||
component: ListField, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof ListField>; | ||
|
||
export const Default: Story = (args: Props) => { | ||
const [_, updateArgs] = useArgs(); | ||
|
||
const addItem = useCallback(() => { | ||
const randomId = (Math.random() + 1).toString(36).substring(7); | ||
updateArgs({ | ||
items: [ | ||
...args.items, | ||
{ | ||
id: randomId, | ||
value: `Item ${randomId}`, | ||
}, | ||
], | ||
}); | ||
}, [updateArgs, args.items]); | ||
|
||
const removeItem = useCallback( | ||
(key: string) => { | ||
updateArgs({ items: args.items.filter(({ id }) => id != key) }); | ||
}, | ||
[updateArgs, args.items], | ||
); | ||
|
||
const onItemDrop = useCallback( | ||
(item: { id: string; value: string }, index: number) => { | ||
const items = [...args.items]; | ||
items.splice( | ||
items.findIndex(x => x.id === item.id), | ||
1, | ||
); | ||
items.splice(index, 0, item); | ||
updateArgs({ items }); | ||
}, | ||
[updateArgs, args.items], | ||
); | ||
|
||
const onSelect = useCallback((id: string) => updateArgs({ selected: id }), [updateArgs]); | ||
|
||
return ( | ||
<Wrapper> | ||
<div> | ||
<ListField | ||
{...args} | ||
addItem={addItem} | ||
removeItem={removeItem} | ||
onItemDrop={onItemDrop} | ||
onSelect={onSelect} | ||
/> | ||
</div> | ||
</Wrapper> | ||
); | ||
}; | ||
|
||
const Wrapper = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 10%; | ||
margin-left: 2rem; | ||
margin-top: 2rem; | ||
height: 300px; | ||
width: 300px; | ||
`; | ||
|
||
Default.args = { | ||
name: "List Field", | ||
description: "List field Sample description", | ||
items: [ | ||
{ | ||
id: "w3tlwi", | ||
value: "Item w3tlwi", | ||
}, | ||
{ | ||
id: "77eg5", | ||
value: "Item 77eg5", | ||
}, | ||
{ | ||
id: "7p218", | ||
value: "Item 7p218", | ||
}, | ||
{ | ||
id: "xquyo", | ||
value: "Item xquyo", | ||
}, | ||
{ | ||
id: "2mewj", | ||
value: "Item 2mewj", | ||
}, | ||
{ | ||
id: "d2gmu", | ||
value: "Item d2gmu", | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { useCallback, useMemo } from "react"; | ||
|
||
import Button from "@reearth/beta/components/Button"; | ||
import DragAndDropList, { | ||
Props as DragAndDropProps, | ||
} from "@reearth/beta/components/DragAndDropList"; | ||
import Property from "@reearth/beta/components/fields"; | ||
import Text from "@reearth/beta/components/Text"; | ||
import { useT } from "@reearth/services/i18n"; | ||
import { styled } from "@reearth/services/theme"; | ||
|
||
type ListItem = { | ||
id: string; | ||
value: string; | ||
}; | ||
|
||
export type Props = { | ||
name?: string; | ||
description?: string; | ||
items: ListItem[]; | ||
removeItem: (id: string) => void; | ||
addItem: () => void; | ||
onSelect: (id: string) => void; | ||
selected?: string; | ||
} & Pick<DragAndDropProps, "onItemDrop">; | ||
|
||
const ListField: React.FC<Props> = ({ | ||
name, | ||
description, | ||
items, | ||
removeItem, | ||
addItem, | ||
onItemDrop, | ||
onSelect, | ||
selected, | ||
}: Props) => { | ||
const t = useT(); | ||
|
||
const deleteItem = useCallback(() => { | ||
if (!selected) return; | ||
removeItem(selected); | ||
}, [selected, removeItem]); | ||
|
||
const getId = useCallback(({ id }: ListItem) => { | ||
return id; | ||
}, []); | ||
|
||
const disableRemoveButton = useMemo(() => { | ||
if (!selected) return true; | ||
|
||
return !items.find(({ id }) => id == selected); | ||
}, [items, selected]); | ||
|
||
return ( | ||
<Property name={name} description={description}> | ||
<FieldWrapper> | ||
<DragAndDropList<ListItem> | ||
uniqueKey="ListField" | ||
items={items} | ||
onItemDrop={onItemDrop} | ||
getId={getId} | ||
renderItem={({ id, value }) => ( | ||
<Item onClick={() => onSelect(id)} selected={selected === id}> | ||
<Text size="xFootnote">{value}</Text> | ||
</Item> | ||
)} | ||
gap={0} | ||
/> | ||
</FieldWrapper> | ||
<ButtonGroup> | ||
<ButtonWrapper | ||
onClick={deleteItem} | ||
icon="trash" | ||
buttonType="secondary" | ||
text={t("Remove")} | ||
size="medium" | ||
disabled={disableRemoveButton} | ||
/> | ||
<ButtonWrapper | ||
onClick={addItem} | ||
icon="plus" | ||
buttonType="secondary" | ||
text={t("Add Item")} | ||
size="medium" | ||
/> | ||
</ButtonGroup> | ||
</Property> | ||
); | ||
}; | ||
|
||
const FieldWrapper = styled.div` | ||
min-height: 84px; | ||
max-height: 224px; | ||
border-radius: 4px; | ||
border: 1px solid rgba(77, 83, 88, 1); | ||
overflow: auto; | ||
`; | ||
|
||
const Item = styled.div<{ selected: boolean }>` | ||
display: flex; | ||
align-items: center; | ||
padding: 0 12px; | ||
height: 28px; | ||
cursor: pointer; | ||
background: ${({ theme, selected }) => (selected ? theme.select.main : "inherit")}; | ||
&:hover { | ||
background: ${({ theme, selected }) => (selected ? theme.select.main : theme.bg[2])}; | ||
} | ||
`; | ||
|
||
const ButtonGroup = styled.div` | ||
display: flex; | ||
gap: 4px; | ||
`; | ||
|
||
const ButtonWrapper = styled(Button)` | ||
height: 28px; | ||
width: 100%; | ||
padding: 0px; | ||
margin: 0px; | ||
opacity: ${({ disabled }) => (disabled ? 0.6 : 1)}; | ||
`; | ||
|
||
export default ListField; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.