Skip to content

Commit

Permalink
feat(compoments): allow custom item texts for DownloadButton (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastilian authored Jan 14, 2025
1 parent 1d526e9 commit 318e044
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/components/src/DownloadButton/DownloadButton.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,15 @@ describe('DownloadButton component', () => {
});
expect(onSelect.mock.calls.length).toBe(0);
});

it('should allow customising the dropdown text', async () => {
const itemText = 'Export all items to CSV';
render(<DownloadButton itemTexts={{ csv: itemText }} />);
await act(async () => {
userEvent.click(screen.getByRole('button', { name: 'Export' }));
});

expect(await screen.findByText(itemText)).toBeInTheDocument();
});
});
});
16 changes: 12 additions & 4 deletions packages/components/src/DownloadButton/DownloadButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export interface DownloadButtonProps extends Omit<DropdownProps, 'onSelect' | 't
* Determines if the button is disabled or not
*/
isDisabled?: boolean;
itemTexts?: {
csv?: string;
json?: string;
};
}

/**
Expand All @@ -37,6 +41,10 @@ const DownloadButton: React.FunctionComponent<DownloadButtonProps> = ({
onSelect = () => undefined,
isDisabled,
tooltipText = 'Export data',
itemTexts = {
csv: undefined,
json: undefined,
},
...props
}) => {
const [isOpen, setIsOpen] = useState(false);
Expand Down Expand Up @@ -69,19 +77,19 @@ const DownloadButton: React.FunctionComponent<DownloadButtonProps> = ({
component="button"
onClick={(event) => onSelect(event, 'csv')}
isDisabled={isDisabled}
aria-label="Export to CSV"
aria-label={itemTexts.csv || 'Export to CSV'}
>
Export to CSV
{itemTexts.csv || 'Export to CSV'}
</DropdownItem>
<DropdownItem
aria-label="Export to JSON"
aria-label={itemTexts.json || 'Export to JSON'}
key="download-json"
ouiaId="DownloadJSON"
component="button"
onClick={(event) => onSelect(event, 'json')}
isDisabled={isDisabled}
>
Export to JSON
{itemTexts.json || 'Export to JSON'}
</DropdownItem>
{extraItems}
</DropdownList>
Expand Down

0 comments on commit 318e044

Please sign in to comment.