Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
DropList tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tinkertrain committed Jun 8, 2021
1 parent f149147 commit fae7f32
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/components/DropList/DropList.ListItem.jsx
Expand Up @@ -5,11 +5,13 @@ import {
getItemContentKeyName,
isItemADivider,
isItemAGroupLabel,
objectHasKey,
} from './DropList.utils'
import {
DividerUI,
GroupLabelUI,
ListItemUI,
ListItemTextUI,
SelectedBadge,
} from './DropList.css'

Expand Down Expand Up @@ -53,7 +55,8 @@ const ListItem = forwardRef(
isDisabled && 'is-disabled',
highlightedIndex === index && 'is-highlighted',
withMultipleSelection && 'with-multiple-selection',
isString(extraClassNames) && extraClassNames
isString(extraClassNames) && extraClassNames,
objectHasKey(item, 'className') && item.className
)
}

Expand Down Expand Up @@ -84,7 +87,9 @@ const ListItem = forwardRef(
withMultipleSelection={withMultipleSelection}
{...itemProps}
>
<span>{isObject(item) ? item[contentKey] : item}</span>
<ListItemTextUI>
{isObject(item) ? item[contentKey] : item}
</ListItemTextUI>
{withMultipleSelection ? (
<SelectedBadge isSelected={isSelected} />
) : null}
Expand Down
11 changes: 9 additions & 2 deletions src/components/DropList/DropList.css.js
Expand Up @@ -25,7 +25,7 @@ export const DropListWrapperUI = styled('div')`
export const MenuListUI = styled('ul')`
width: 100%;
max-height: 200px;
overflow-y: scroll;
overflow-y: auto;
margin: 0;
padding: 5px 0 0 0;
list-style: none;
Expand Down Expand Up @@ -68,7 +68,7 @@ export const ListItemUI = styled('li')`
align-items: center;
height: 36px;
margin: 0 5px 2px;
padding: 0 15px;
padding: 0 10px 0 15px;
border-radius: 3px;
line-height: 36px;
color: ${getColor('charcoal.600')};
Expand Down Expand Up @@ -122,6 +122,13 @@ export const ListItemUI = styled('li')`
}
`

export const ListItemTextUI = styled('span')`
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
`

export const EmptyListUI = styled('div')`
height: 36px;
margin: 0 5px;
Expand Down
8 changes: 6 additions & 2 deletions src/components/DropList/DropList.jsx
Expand Up @@ -253,14 +253,18 @@ function DropListManager({
}

const itemShape = PropTypes.shape({
label: requiredItemPropsCheck,
value: requiredItemPropsCheck,
className: PropTypes.string,
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
isDisabled: PropTypes.bool,
label: requiredItemPropsCheck,
value: requiredItemPropsCheck,
type: PropTypes.string,
})

const dividerShape = PropTypes.shape({
type: PropTypes.oneOf(['divider', 'Divider']).isRequired,
})

const groupShape = PropTypes.shape({
label: requiredItemPropsCheck,
value: requiredItemPropsCheck,
Expand Down
12 changes: 12 additions & 0 deletions src/components/DropList/DropList.stories.mdx
Expand Up @@ -227,6 +227,18 @@ const itemShape = PropTypes.shape({
})
```

- Laser Targeting: if you need to target a specific List Item for styling (or else), you can add the property `className` to each item in the array, the value will be added to the existing css class names on the `DropListItem`:

```js
const items = [
{ label: 'a', className: 'paintItBlue' },
{ label: 'ab' },
{ label: 'abc' },
{ type: 'divider' },
{ label: 'b', className: 'paintItBlue' },
]
```

- Dividers: Add a `type = divider` property to an object to create a divider

```js
Expand Down
25 changes: 25 additions & 0 deletions src/components/DropList/DropList.test.js
Expand Up @@ -123,6 +123,31 @@ describe('Render', () => {
})
})

test('should add a classname to the list item if present in the item', async () => {
const { getByRole, queryByText } = render(
<DropList
items={someItems.map((item, index) => {
if (index === 1) {
return { className: 'paintItBlue', ...item }
}
return item
})}
toggler={<SimpleButton text="Button Toggler" />}
/>
)
const toggler = getByRole('button')

user.click(toggler)

await waitFor(() => {
expect(
queryByText(someItems[1].label).parentElement.classList.contains(
'paintItBlue'
)
).toBeTruthy()
})
})

test('should render a menu list with dividers', async () => {
const { container, getByRole, queryByRole } = render(
<DropList
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/specs/dropdown.specs.js
Expand Up @@ -2,7 +2,7 @@ import { createSpec, faker } from '@helpscout/helix'

export const ItemSpec = createSpec({
id: faker.datatype.uuid(),
label: faker.name.firstName(),
label: faker.company.companyName(),
value: faker.name.firstName(),
// onClick: () => (value, props) => console.log('Clicked!', value),
})
Expand Down

0 comments on commit fae7f32

Please sign in to comment.