Skip to content

Commit

Permalink
fix(components): set DropdownButton default position and limit sizes
Browse files Browse the repository at this point in the history
Ticket: task/Transversal-152
Reviewed-by: @MIGUELez11
Refs: #197

feat(components): add `TextClamp`  one line to Dropdown Item label
Ticket: task/Transversal-152
Reviewed-by: @MIGUELez11
Refs: #197
  • Loading branch information
fermarinsanchez authored Oct 25, 2024
1 parent d6b39ae commit 6a79a4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const DROPDOWN_BUTTON_DEFAULT_PROPS = {
children: '',
chevronUp: false,
data: [],
width: 'target',
position: 'bottom-end',
};
export const DROPDOWN_BUTTON_PROP_TYPES = {
children: PropTypes.node,
Expand Down
30 changes: 24 additions & 6 deletions packages/components/src/form/DropdownButton/DropdownButton.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useMemo, useState } from 'react';
import React, { useMemo, useState, useRef, useEffect } from 'react';
import { ChevDownIcon, ChevUpIcon } from '@bubbles-ui/icons/outline';
import { noop } from 'lodash';
import { DropdownButtonStyles } from './DropdownButton.styles';
Expand All @@ -11,15 +11,18 @@ import { Popover } from '../../overlay/Popover';
import { Dropdown, Item } from '../../overlay/Dropdown';
import { ImageLoader } from '../../misc';

const DropdownButton = ({ itemComponent, data, chevronUp, width, ...props }) => {
const DropdownButton = ({ itemComponent, data, chevronUp, position, ...props }) => {
const [opened, setOpened] = useState(false);
const [buttonWidth, setButtonWidth] = useState(0);
const buttonRef = useRef(null);

const handleOnOption =
(onOption = noop) =>
() => {
onOption();
setOpened(false);
};
const { classes, cx } = DropdownButtonStyles({}, { name: 'DropdownButton' });

function renderIcon(icon) {
if (!icon) return null;
Expand All @@ -38,13 +41,20 @@ const DropdownButton = ({ itemComponent, data, chevronUp, width, ...props }) =>
[data],
);

const { classes, cx } = DropdownButtonStyles({}, { name: 'DropdownButton' });
useEffect(() => {
if (buttonRef.current) {
setButtonWidth(buttonRef.current.offsetWidth - 10);
}
}, []);

return (
<Popover
opened={opened}
offset={4}
position={position}
target={
<Button
ref={buttonRef}
{...props}
rightIcon={
chevronUp ? (
Expand All @@ -54,22 +64,30 @@ const DropdownButton = ({ itemComponent, data, chevronUp, width, ...props }) =>
)
}
onClick={() => setOpened(!opened)}
textAlign="appart"
textAlign="apart"
/>
}
width={width}
closeOnEscape
closeOnClickOutside={true}
onClose={() => setOpened(false)}
trapFocus
styles={{ boxShadow: 'none', border: 'none' }}
styles={{
dropdown: {
minWidth: buttonWidth,
maxWidth: 150,
},
}}
>
<Dropdown>
{handledData.map((item, index) => (
<Item
key={item.id || `DropdownButton Item ${index}`}
{...item}
icon={renderIcon(item.icon)}
style={{
minWidth: buttonWidth,
maxWidth: 200,
}}
/>
))}
</Dropdown>
Expand Down
6 changes: 4 additions & 2 deletions packages/components/src/overlay/Dropdown/Item/Item.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { forwardRef } from 'react';
import { CheckIcon } from '@bubbles-ui/icons/solid';
import { isString } from 'lodash';
import { Box } from '../../../layout/Box';
import { Text } from '../../../typography';
import { Text, TextClamp } from '../../../typography';
import { Avatar } from '../../../informative/Avatar';
import { ImageLoader } from '../../../misc';
import { ItemStyles } from './Item.styles';
Expand Down Expand Up @@ -33,7 +33,9 @@ const Item = forwardRef(
{isString(icon) ? <ImageLoader src={icon} height={16} width={16} /> : icon}
</Box>
)}
<Text className={classes.label}>{label}</Text>
<TextClamp lines={1}>
<Text className={classes.label}>{label}</Text>
</TextClamp>
{dataSelected && <CheckIcon className={classes.check} />}
</Box>
);
Expand Down

0 comments on commit 6a79a4f

Please sign in to comment.