Repository navigation
Share the Group by split button #3429
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@hyperdx/app': patch | ||
| --- | ||
|
|
||
| Show a Group by field count as a number on a narrow screen |
This file contains hidden or 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 hidden or 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
108 changes: 108 additions & 0 deletions
108
packages/app/src/components/SplitValueButton/SplitValueButton.stories.tsx
This file contains hidden or 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,108 @@ | ||
| import { Group, Stack, Text } from '@mantine/core'; | ||
| import type { Meta, StoryObj } from '@storybook/nextjs'; | ||
|
|
||
| import { SplitValueButton } from './SplitValueButton'; | ||
|
|
||
| const meta: Meta<typeof SplitValueButton> = { | ||
| title: 'Components/SplitValueButton', | ||
| component: SplitValueButton, | ||
| parameters: { | ||
| layout: 'padded', | ||
| docs: { | ||
| description: { | ||
| component: | ||
| 'The shared trigger for Group by, Columns, and Sort. Use it when a search control shows the current value of a clause and opens an editor. Do not use it for an action such as Save, Run, or Apply.', | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof SplitValueButton>; | ||
|
|
||
| const noop = () => {}; | ||
|
|
||
| export const WhenToUse: Story = { | ||
| render: () => ( | ||
| <Stack gap="lg" maw={560}> | ||
| <Text size="sm"> | ||
| Use this button for a search clause whose value is one field, or a count | ||
| of fields. The name stays fixed. The value is what you press. | ||
| </Text> | ||
| <Stack gap="xs"> | ||
| <Text size="xs" c="dimmed"> | ||
| Group by splits the histogram. One series for each value of the field. | ||
| </Text> | ||
| <SplitValueButton | ||
| name="Group by" | ||
| summary="SeverityText" | ||
| count={1} | ||
| compact={false} | ||
| onClick={noop} | ||
| data-testid="story-group-by" | ||
| /> | ||
| </Stack> | ||
| <Stack gap="xs"> | ||
| <Text size="xs" c="dimmed"> | ||
| Columns chooses which columns the results table shows, and their | ||
| order. | ||
| </Text> | ||
| <SplitValueButton | ||
| name="Columns" | ||
| summary="4 fields" | ||
| count={4} | ||
| compact={false} | ||
| onClick={noop} | ||
| data-testid="story-columns" | ||
| /> | ||
| </Stack> | ||
| <Stack gap="xs"> | ||
| <Text size="xs" c="dimmed"> | ||
| Sort sets the order of the result rows. One field includes its | ||
| direction. | ||
| </Text> | ||
| <SplitValueButton | ||
| name="Sort" | ||
| summary="Timestamp desc" | ||
| count={1} | ||
| compact={false} | ||
| onClick={noop} | ||
| data-testid="story-sort" | ||
| /> | ||
| </Stack> | ||
| <Stack gap="xs"> | ||
| <Text size="xs" c="dimmed"> | ||
| On a narrow screen a count is only the number. One field keeps its | ||
| name, and an empty clause stays none. | ||
| </Text> | ||
| <Group gap="sm"> | ||
| <SplitValueButton | ||
| name="Group by" | ||
| summary="SeverityText" | ||
| count={1} | ||
| compact | ||
| onClick={noop} | ||
| data-testid="story-group-by-compact" | ||
| /> | ||
| <SplitValueButton | ||
| name="Columns" | ||
| summary="4 fields" | ||
| count={4} | ||
| compact | ||
| onClick={noop} | ||
| data-testid="story-columns-compact" | ||
| /> | ||
| <SplitValueButton | ||
| name="Sort" | ||
| summary="none" | ||
| count={0} | ||
| dimmed | ||
| compact | ||
| onClick={noop} | ||
| data-testid="story-sort-compact" | ||
| /> | ||
| </Group> | ||
| </Stack> | ||
| </Stack> | ||
| ), | ||
| }; |
91 changes: 91 additions & 0 deletions
91
packages/app/src/components/SplitValueButton/SplitValueButton.tsx
This file contains hidden or 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,91 @@ | ||
| import { ComponentPropsWithoutRef, Ref } from 'react'; | ||
| import cx from 'classnames'; | ||
| import { Text, UnstyledButton } from '@mantine/core'; | ||
| import { IconChevronDown } from '@tabler/icons-react'; | ||
|
|
||
| import { useCompactViewport } from '@/components/CompactViewport/useCompactViewport'; | ||
|
|
||
| import classes from './SplitValueButton.module.scss'; | ||
|
|
||
| /** One field by name, several as a count, or none. */ | ||
| export function fieldListSummary(fields: string[]): string { | ||
| if (fields.length === 0) return 'none'; | ||
| if (fields.length === 1) return fields[0]; | ||
| return `${fields.length} fields`; | ||
| } | ||
|
|
||
| type SplitValueButtonProps = { | ||
| /** Fixed name, such as Group by, Columns, or Sort. */ | ||
| name: string; | ||
| /** Desktop value: a field name, "3 fields", or "none". */ | ||
| summary: string; | ||
| /** How many fields the summary counts. Drives the narrow-screen number. */ | ||
| count: number; | ||
| /** The clause is still the source default, so the value is quiet. */ | ||
| dimmed?: boolean; | ||
| /** Full clause, shown on hover when the value is only a summary. */ | ||
| title?: string; | ||
| onClick: () => void; | ||
| /** Story and test override. Otherwise follows the compact viewport. */ | ||
| compact?: boolean; | ||
| 'data-testid': string; | ||
| ref?: Ref<HTMLButtonElement>; | ||
| } & Omit<ComponentPropsWithoutRef<'button'>, 'onClick' | 'title'>; | ||
|
|
||
| /** | ||
| * Split button for a search clause: a fixed name, then the current value. | ||
| * | ||
| * Use it for Group by, Columns, and Sort. The name says which clause it is. | ||
| * The value is one field, or "3 fields" when there are several. On a narrow | ||
| * screen that count is only the number, so the row still fits. | ||
| * | ||
| * Group by splits the histogram, one series per value. Columns chooses and | ||
| * orders the results table. Sort sets the row order, and a single field | ||
| * includes its direction. | ||
| * | ||
| * Do not use it for an action such as Save, Run, or Apply. | ||
| */ | ||
| export function SplitValueButton({ | ||
| name, | ||
| summary, | ||
| count, | ||
| dimmed, | ||
| title, | ||
| onClick, | ||
| compact, | ||
| 'data-testid': testId, | ||
| className, | ||
| ref, | ||
| ...others | ||
| }: SplitValueButtonProps) { | ||
| const viewportCompact = useCompactViewport(); | ||
| const isCompact = compact ?? viewportCompact; | ||
| const shown = isCompact && count > 1 ? String(count) : summary; | ||
|
|
||
| return ( | ||
| <div className={classes.control}> | ||
| <Text size="xs" fw={500} className={classes.label}> | ||
| {name} | ||
| </Text> | ||
| <UnstyledButton | ||
| ref={ref} | ||
| {...others} | ||
| className={cx(classes.target, className)} | ||
| onClick={onClick} | ||
| title={title} | ||
| aria-label={`${name} ${summary}`} | ||
| data-testid={testId} | ||
| > | ||
| <Text | ||
| size="xs" | ||
| fw={600} | ||
| className={classes.value} | ||
| c={dimmed ? 'dimmed' : undefined} | ||
| > | ||
| {shown} | ||
| </Text> | ||
| <IconChevronDown size={14} /> | ||
| </UnstyledButton> | ||
| </div> | ||
| ); | ||
| } |
53 changes: 53 additions & 0 deletions
53
packages/app/src/components/SplitValueButton/__tests__/SplitValueButton.test.tsx
This file contains hidden or 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,53 @@ | ||
| import { screen } from '@testing-library/react'; | ||
|
|
||
| import { SplitValueButton } from '@/components/SplitValueButton/SplitValueButton'; | ||
|
|
||
| describe('SplitValueButton', () => { | ||
| it('shows a field count as words on a wide screen', () => { | ||
| renderWithMantine( | ||
| <SplitValueButton | ||
| name="Columns" | ||
| summary="3 fields" | ||
| count={3} | ||
| compact={false} | ||
| onClick={() => {}} | ||
| data-testid="columns" | ||
| />, | ||
| ); | ||
| expect(screen.getByTestId('columns')).toHaveTextContent('3 fields'); | ||
| expect(screen.getByTestId('columns')).toHaveAccessibleName( | ||
| 'Columns 3 fields', | ||
| ); | ||
| }); | ||
|
|
||
| it('shows only the number on a narrow screen', () => { | ||
| renderWithMantine( | ||
| <SplitValueButton | ||
| name="Columns" | ||
| summary="3 fields" | ||
| count={3} | ||
| compact | ||
| onClick={() => {}} | ||
| data-testid="columns" | ||
| />, | ||
| ); | ||
| expect(screen.getByTestId('columns')).toHaveTextContent('3'); | ||
| expect(screen.getByTestId('columns')).toHaveAccessibleName( | ||
| 'Columns 3 fields', | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps a single field name on a narrow screen', () => { | ||
| renderWithMantine( | ||
| <SplitValueButton | ||
| name="Group by" | ||
| summary="SeverityText" | ||
| count={1} | ||
| compact | ||
| onClick={() => {}} | ||
| data-testid="group-by" | ||
| />, | ||
| ); | ||
| expect(screen.getByTestId('group-by')).toHaveTextContent('SeverityText'); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toHaveTextContent('3')also passes when the button displays3 fields, so this test misses a broken narrow-screen count. Use an exact text check. Also add a case without thecompactoverride so the tests catch a broken connection touseCompactViewport.