forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request blockscout#2014 from blockscout/tag-group-select
tag group select component
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import _noop from 'lodash/noop'; | ||
import React from 'react'; | ||
|
||
import { test, expect } from 'playwright/lib'; | ||
|
||
import TagGroupSelect from './TagGroupSelect'; | ||
|
||
test.use({ viewport: { width: 480, height: 140 } }); | ||
|
||
test('base view +@dark-mode', async({ render }) => { | ||
const component = await render( | ||
<TagGroupSelect | ||
items={ [ { id: '1', title: 'Option 1' }, { id: '2', title: 'Option 2' }, { id: 'duck', title: 'Cute little duck' } ] } | ||
value="duck" | ||
onChange={ _noop } | ||
/>, | ||
); | ||
|
||
await component.getByText('Option 2').hover(); | ||
|
||
await expect(component).toHaveScreenshot(); | ||
}); |
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,56 @@ | ||
import { HStack, Tag } from '@chakra-ui/react'; | ||
import React from 'react'; | ||
|
||
type Props<T extends string> = { | ||
items: Array<{ id: T; title: string }>; | ||
} & ( | ||
{ | ||
value: T; | ||
onChange: (value: T) => void; | ||
isMulti?: false; | ||
} | { | ||
value: Array<T>; | ||
onChange: (value: Array<T>) => void; | ||
isMulti: true; | ||
} | ||
) | ||
|
||
const TagGroupSelect = <T extends string>({ items, value, isMulti, onChange }: Props<T>) => { | ||
const onItemClick = React.useCallback((event: React.SyntheticEvent) => { | ||
const itemValue = (event.currentTarget as HTMLDivElement).getAttribute('data-id') as T; | ||
if (isMulti) { | ||
let newValue; | ||
if (value.includes(itemValue)) { | ||
newValue = value.filter(i => i !== itemValue); | ||
} else { | ||
newValue = [ ...value, itemValue ]; | ||
} | ||
onChange(newValue); | ||
} else { | ||
onChange(itemValue); | ||
} | ||
}, [ isMulti, onChange, value ]); | ||
|
||
return ( | ||
<HStack> | ||
{ items.map(item => { | ||
const isSelected = isMulti ? value.includes(item.id) : value === item.id; | ||
return ( | ||
<Tag | ||
variant="select" | ||
key={ item.id } | ||
data-id={ item.id } | ||
data-selected={ isSelected } | ||
fontWeight={ 500 } | ||
cursor="pointer" | ||
onClick={ onItemClick } | ||
> | ||
{ item.title } | ||
</Tag> | ||
); | ||
}) } | ||
</HStack> | ||
); | ||
}; | ||
|
||
export default TagGroupSelect; |
Binary file added
BIN
+3.91 KB
...__screenshots__/TagGroupSelect.pw.tsx_dark-color-mode_base-view-dark-mode-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+3.72 KB
...pSelect/__screenshots__/TagGroupSelect.pw.tsx_default_base-view-dark-mode-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.