Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adds feature metrics for collection prefs and button group #3180

Merged
merged 5 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3991,6 +3991,7 @@ Use this to provide a unique accessible name for each button group on the page."
"type": "string",
},
{
"defaultValue": "false",
"description": "Use this property to determine dropdown placement strategy for all menu dropdown items.
By default, the dropdown height is constrained to fit inside the height of its next scrollable container element.
Enabling this property will allow the dropdown to extend beyond that container by using fixed positioning and
Expand Down
34 changes: 34 additions & 0 deletions src/button-group/__tests__/button-group-dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { fireEvent } from '@testing-library/react';
import { warnOnce } from '@cloudscape-design/component-toolkit/internal';

import { ButtonGroupProps } from '../../../lib/components/button-group';
import * as baseComponentHooks from '../../../lib/components/internal/hooks/use-base-component';
import { renderButtonGroup } from './common';

import buttonStyles from '../../../lib/components/button/styles.css.js';

const useBaseComponentSpy = jest.spyOn(baseComponentHooks, 'default');

jest.mock('@cloudscape-design/component-toolkit/internal', () => ({
...jest.requireActual('@cloudscape-design/component-toolkit/internal'),
warnOnce: jest.fn(),
Expand Down Expand Up @@ -156,3 +159,34 @@ test('handles file upload', () => {

expect(onFilesChange).toHaveBeenCalledWith(expect.objectContaining({ detail: { id: 'file', files: [file] } }));
});

test('adds feature metrics', () => {
renderButtonGroup({
items: [
{ type: 'icon-button', id: 'icon1', text: 'icon1' },
{
type: 'group',
text: 'group1',
items: [
{ type: 'icon-toggle-button', id: 'toggle1', text: 'toggle1', pressed: false },
{ type: 'icon-toggle-button', id: 'toggle2', text: 'toggle2', pressed: false },
],
},
{ type: 'icon-file-input', id: 'file1', text: 'file1' },
{ type: 'menu-dropdown', id: 'menu1', text: 'menu1', items: [] },
],
});
expect(useBaseComponentSpy).toHaveBeenCalledWith('ButtonGroup', {
props: {
variant: 'icon',
dropdownExpandToViewport: false,
},
metadata: {
iconButtonsCount: 1,
iconToggleButtonsCount: 2,
iconFileInputsCount: 1,
menuDropdownsCount: 1,
groupsCount: 1,
},
});
});
27 changes: 26 additions & 1 deletion src/button-group/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ import InternalButtonGroup from './internal';
export { ButtonGroupProps };

const ButtonGroup = React.forwardRef(
({ variant, dropdownExpandToViewport, ...rest }: ButtonGroupProps, ref: React.Ref<ButtonGroupProps.Ref>) => {
({ variant, dropdownExpandToViewport = false, ...rest }: ButtonGroupProps, ref: React.Ref<ButtonGroupProps.Ref>) => {
const baseProps = getBaseProps(rest);
const itemCounts = getItemCounts(rest.items);
const baseComponentProps = useBaseComponent('ButtonGroup', {
props: {
variant,
dropdownExpandToViewport,
},
metadata: {
iconButtonsCount: itemCounts['icon-button'],
iconToggleButtonsCount: itemCounts['icon-toggle-button'],
iconFileInputsCount: itemCounts['icon-file-input'],
menuDropdownsCount: itemCounts['menu-dropdown'],
groupsCount: itemCounts.group,
},
});

const externalProps = getExternalProps(rest);
Expand All @@ -35,5 +43,22 @@ const ButtonGroup = React.forwardRef(
}
);

function getItemCounts(allItems: readonly ButtonGroupProps.ItemOrGroup[] = []) {
const counters = { 'icon-button': 0, 'icon-toggle-button': 0, 'icon-file-input': 0, 'menu-dropdown': 0, group: 0 };

function count(items: readonly ButtonGroupProps.ItemOrGroup[]) {
for (const item of items) {
counters[item.type] += 1;

if (item.type === 'group') {
count(item.items);
}
}
}
count(allItems);

return counters;
}

applyDisplayName(ButtonGroup, 'ButtonGroup');
export default ButtonGroup;
5 changes: 4 additions & 1 deletion src/button-group/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IconProps } from '../icon/interfaces';
import { BaseComponentProps } from '../internal/base-component';
import { NonCancelableEventHandler } from '../internal/events';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { SomeRequired } from '../internal/types';

export interface ButtonGroupProps extends BaseComponentProps {
/**
Expand Down Expand Up @@ -97,7 +98,9 @@ export interface ButtonGroupProps extends BaseComponentProps {
onFilesChange?: NonCancelableEventHandler<ButtonGroupProps.FilesChangeDetails>;
}

export interface InternalButtonGroupProps extends ButtonGroupProps, InternalBaseComponentProps {}
export interface InternalButtonGroupProps
extends SomeRequired<ButtonGroupProps, 'dropdownExpandToViewport'>,
InternalBaseComponentProps {}

export namespace ButtonGroupProps {
export type Variant = 'icon';
Expand Down
Loading