From 675ad5fa45f4127bedf38722273f3e7bb83c8a53 Mon Sep 17 00:00:00 2001 From: Andrew Holloway Date: Thu, 23 May 2024 18:59:45 -0500 Subject: [PATCH] feat(Select): support horizontal labels (#1962) - update and add snapshots and stories for this - also add story descriptions and updated code blocks - update display of graphical stories --- src/components/Select/Select-v2.module.css | 12 + src/components/Select/Select-v2.stories.tsx | 142 ++++++---- src/components/Select/Select-v2.tsx | 8 + .../__snapshots__/Select-v2.test.tsx.snap | 243 ++++++++++-------- 4 files changed, 235 insertions(+), 170 deletions(-) diff --git a/src/components/Select/Select-v2.module.css b/src/components/Select/Select-v2.module.css index 09954c10c..cf9f88a5d 100644 --- a/src/components/Select/Select-v2.module.css +++ b/src/components/Select/Select-v2.module.css @@ -35,6 +35,18 @@ z-index: 100; } +.select--label-layout-vertical { + flex-direction: column; +} + +.select--label-layout-horizontal { + display: flex; + flex-direction: row; + align-items: baseline; + + gap: 0.5rem; +} + /** * The button to trigger the display of the select field. */ diff --git a/src/components/Select/Select-v2.stories.tsx b/src/components/Select/Select-v2.stories.tsx index c634017fe..35fbd8d63 100644 --- a/src/components/Select/Select-v2.stories.tsx +++ b/src/components/Select/Select-v2.stories.tsx @@ -3,7 +3,6 @@ import { expect } from '@storybook/test'; import { userEvent, within } from '@storybook/testing-library'; import React from 'react'; import { Select } from './Select-v2'; -import Icon from '../Icon'; const meta: Meta = { title: 'Components/V2/Select', @@ -115,6 +114,7 @@ export const Default: StoryObj = { 'data-testid': 'dropdown', defaultValue: exampleOptions[0], name: 'select', + className: 'w-60', children: ( <> @@ -161,6 +161,18 @@ export const Default: StoryObj = { }, }; +export const HorizontalLabel: StoryObj = { + args: { + ...Default.args, + className: 'w-60', + labelLayout: 'horizontal', + label: 'Animal?', + }, + parameters: { + ...Default.parameters, + }, +}; + /** * Instead of a render prop for `Select.Button`, you can forego the render prop for the button and use static text instead. * This mode is also useful if you want to use a controlled component and manage state yourself. @@ -374,6 +386,9 @@ export const WithFieldNote: StoryObj = { ), }, + parameters: { + ...Default.parameters, + }, }; /** @@ -391,7 +406,15 @@ export const UncontrolledHeadless: StoryObj = { <> {({ value, open, disabled }) => ( - + )} @@ -411,7 +434,15 @@ export const UncontrolledHeadless: StoryObj = { - {({ open }) => ( - <> - - {() => ( - - )} - - - {exampleOptions.map((option) => ( - - {option.label} - - ))} - - - )} - - ); - }, -}; - /** * This shows the contents of `Select` upon render. Mostly to demonstrate it is possible, to capture a snapshot of the appearance. */ @@ -878,6 +903,9 @@ export const OpenByDefault: StoryObj = { badges: ['intro-1.2', 'current-2.0'], layout: 'centered', chromatic: { delay: 300, disableSnapshot: true }, + docs: { + ...Default.parameters?.docs, + }, }, play: selectCat, }; diff --git a/src/components/Select/Select-v2.tsx b/src/components/Select/Select-v2.tsx index c3fd49ab3..d13369222 100644 --- a/src/components/Select/Select-v2.tsx +++ b/src/components/Select/Select-v2.tsx @@ -78,6 +78,12 @@ type SelectProps = ExtractProps & * Visible text label for the component. */ label?: string; + /** + * Whether the label is adjacent to the field (horizontal) or above the field (vertical) + * + * **Default is `"vertical"`**. + */ + labelLayout?: 'vertical' | 'horizontal'; /** * Whether it should show the field hint or not * @@ -196,6 +202,7 @@ export function Select({ isError, isWarning, label, + labelLayout = 'vertical', modifiers = defaultPopoverModifiers, name, onFirstUpdate, @@ -243,6 +250,7 @@ export function Select({ const componentClassName = clsx( styles['select'], fieldNote && styles['select--has-fieldNote'], + labelLayout && styles[`select--label-layout-${labelLayout}`], className, ); const sharedProps = { diff --git a/src/components/Select/__snapshots__/Select-v2.test.tsx.snap b/src/components/Select/__snapshots__/Select-v2.test.tsx.snap index 614c2f8e6..70c13b468 100644 --- a/src/components/Select/__snapshots__/Select-v2.test.tsx.snap +++ b/src/components/Select/__snapshots__/Select-v2.test.tsx.snap @@ -2,29 +2,29 @@ exports[` Generated Snapshots Default story renders snapshot 1`] = `
Generated Snapshots Default story renders snapshot 1`] = ` exports[` Generated Snapshots Error story renders snapshot 1`] = `
+ +`; + +exports[` Generated Snapshots MultipleWithTruncation story renders snapshot 1`] = `
- -`; - -exports[` Generated Snapshots Warning story renders snapshot 1`] = `
@@ -519,13 +536,13 @@ exports[` Generated Snapshots WithFieldName story renders snapshot 1`] = `