An open source design system that's fully customizable and agent ready
TypeScript
fix(Field): let a row shrink the field (#6814)
David Petersena0979f0144c9
diff --git a/.changeset/field-shrinks-in-rows.md b/.changeset/field-shrinks-in-rows.mdnew file mode 100644index 0000000000..ca76a8be58--- /dev/null+++ b/.changeset/field-shrinks-in-rows.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Field-based inputs (TextInput, Selector, DateInput, and the rest of the input family) can now be shrunk by their row: the Field root resets its automatic minimum size, so a filter bar of a search box and selectors no longer pushes past its container on phones+@thedjpetersendiff --git a/apps/storybook/stories/Field.stories.tsx b/apps/storybook/stories/Field.stories.tsxindex 826487b601..98b21befb4 100644--- a/apps/storybook/stories/Field.stories.tsx+++ b/apps/storybook/stories/Field.stories.tsx@@ -5,6 +5,10 @@ import type {Meta, StoryObj} from '@storybook/react';import * as stylex from '@stylexjs/stylex';import {Field} from '@astryxdesign/core/Field';import {TextInput} from '@astryxdesign/core/TextInput';+import {HStack} from '@astryxdesign/core/HStack';+import {Icon} from '@astryxdesign/core/Icon';+import {Selector} from '@astryxdesign/core/Selector';+import {StackItem} from '@astryxdesign/core/Stack';import {EnvelopeIcon} from '@heroicons/react/24/outline';import {colorVars,@@ -308,3 +312,85 @@ export const StatusVariants: Story = {);},};++function OrdersFilterBar({wrap = 'wrap'}: {wrap?: 'wrap' | 'nowrap'}) {+ const [query, setQuery] = useState('');+ const [status, setStatus] = useState('Awaiting fulfillment');+ const [region, setRegion] = useState('All regions');+ const [sort, setSort] = useState('Newest first');+ return (+ <HStack gap={2} wrap={wrap} vAlign="center">+ <StackItem size="fill">+ <TextInput+ label="Search orders"+ isLabelHidden+ placeholder="Search by order or customer"+ startIcon={<Icon icon="search" />}+ value={query}+ onChange={setQuery}+ />+ </StackItem>+ <Selector+ label="Status"+ isLabelHidden+ options={[+ 'All statuses',+ 'Awaiting fulfillment',+ 'Shipped',+ 'Delivered',+ 'Returned',+ ]}+ value={status}+ onChange={setStatus}+ />+ <Selector+ label="Region"+ isLabelHidden+ options={['All regions', 'North America', 'Europe', 'Asia Pacific']}+ value={region}+ onChange={setRegion}+ />+ <Selector+ label="Sort"+ isLabelHidden+ options={['Newest first', 'Oldest first', 'Highest total']}+ value={sort}+ onChange={setSort}+ />+ </HStack>+ );+}++const frameCaption = {+ fontSize: 12,+ color: 'var(--color-text-secondary)',+ marginBottom: 8,+};++/**+ * The Field.doc.mjs "Filter bar" example (a search TextInput and three+ * Selectors) in a 390px phone frame, a 768px tablet frame, and at full width.+ * The last frame is the same bar without `wrap` at 390px: its fields now+ * shrink inside the frame instead of pushing past it, which is why filter bars+ * should wrap on phones.+ */+export const FilterBar: Story = {+ render: () => (+ <div style={{display: 'flex', flexDirection: 'column', gap: 24}}>+ {[390, 768].map(width => (+ <div key={width} style={{width, outline: '1px dashed #ccc'}}>+ <p style={frameCaption}>{width}px, wrap</p>+ <OrdersFilterBar />+ </div>+ ))}+ <div>+ <p style={frameCaption}>Full width, wrap</p>+ <OrdersFilterBar />+ </div>+ <div style={{width: 390, outline: '1px dashed #ccc'}}>+ <p style={frameCaption}>390px, no wrap: fields shrink inside the row</p>+ <OrdersFilterBar wrap="nowrap" />+ </div>+ </div>+ ),+};diff --git a/packages/core/src/Field/Field.doc.mjs b/packages/core/src/Field/Field.doc.mjsindex 60f1c97452..855ddf21b3 100644--- a/packages/core/src/Field/Field.doc.mjs+++ b/packages/core/src/Field/Field.doc.mjs@@ -136,7 +136,7 @@ export const docs = {{name: 'width',type: 'SizeValue',- description: 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. Prefer this over setting width via xstyle/className/style, which only size the inner control box.',+ description: 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. Prefer this over setting width via xstyle/className/style, which only size the inner control box. Without a width, a row can shrink the whole field below the natural width of its control, so filter bars shrink instead of overflowing.',},{name: 'ref',@@ -184,6 +184,64 @@ export const docs = {],},examples: [+ {+ label: 'Filter bar',+ code: `+import {useState} from 'react';+import {HStack} from '@astryxdesign/core/HStack';+import {Icon} from '@astryxdesign/core/Icon';+import {Selector} from '@astryxdesign/core/Selector';+import {StackItem} from '@astryxdesign/core/Stack';+import {TextInput} from '@astryxdesign/core/TextInput';++// Every Field-based input (TextInput, Selector, DateInput, ...) can be shrunk+// by its row, so a filter bar never pushes the page sideways. Let the row wrap+// so that on a phone the search box keeps a full line and the selectors move+// below it; on wider screens everything stays on one line, with the search box+// taking the free space.+function OrdersFilterBar() {+ const [query, setQuery] = useState('');+ const [status, setStatus] = useState('Awaiting fulfillment');+ const [region, setRegion] = useState('All regions');+ const [sort, setSort] = useState('Newest first');+ return (+ <HStack gap={2} wrap="wrap" vAlign="center">+ <StackItem size="fill">+ <TextInput+ label="Search orders"+ isLabelHidden+ placeholder="Search by order or customer"+ startIcon={<Icon icon="search" />}+ value={query}+ onChange={setQuery}+ />+ </StackItem>+ <Selector+ label="Status"+ isLabelHidden+ options={['All statuses', 'Awaiting fulfillment', 'Shipped', 'Delivered', 'Returned']}+ value={status}+ onChange={setStatus}+ />+ <Selector+ label="Region"+ isLabelHidden+ options={['All regions', 'North America', 'Europe', 'Asia Pacific']}+ value={region}+ onChange={setRegion}+ />+ <Selector+ label="Sort"+ isLabelHidden+ options={['Newest first', 'Oldest first', 'Highest total']}+ value={sort}+ onChange={setSort}+ />+ </HStack>+ );+}+`,+ },{label: 'Wrap a custom control',code: `diff --git a/packages/core/src/Field/Field.test.tsx b/packages/core/src/Field/Field.test.tsxindex 9fb7d8a608..6a20c80110 100644--- a/packages/core/src/Field/Field.test.tsx+++ b/packages/core/src/Field/Field.test.tsx@@ -581,3 +581,39 @@ describe('Field', () => {});});});++describe('Field in a narrow row', () => {+ it('lets a row shrink the field below its control width', () => {+ const {container} = render(+ <Field label="Search" inputID="search">+ <input id="search" />+ </Field>,+ );+ const root = container.querySelector('.astryx-field')!;+ expect(getComputedStyle(root).minWidth).toBe('0');+ });++ it('keeps an explicit width, which a row may still shrink', () => {+ const {container} = render(+ <Field label="Search" inputID="search" width={240}>+ <input id="search" />+ </Field>,+ );+ const root = container.querySelector('.astryx-field')!;+ expect(root.getAttribute('style')).toContain('240');+ expect(getComputedStyle(root).minWidth).toBe('0');+ });++ it('leaves horizontal-labels mode (display: contents) untouched', () => {+ const {container} = render(+ <FormLayoutContext value={{direction: 'horizontal-labels'}}>+ <Field label="Name" inputID="name">+ <input id="name" />+ </Field>+ </FormLayoutContext>,+ );+ const root = container.firstChild as HTMLElement;+ expect(getComputedStyle(root).display).toBe('contents');+ expect(getComputedStyle(root).minWidth).not.toBe('0');+ });+});diff --git a/packages/core/src/Field/Field.tsx b/packages/core/src/Field/Field.tsxindex 07d0a593c4..0c4778d155 100644--- a/packages/core/src/Field/Field.tsx+++ b/packages/core/src/Field/Field.tsx@@ -41,6 +41,11 @@ const styles = stylex.create({// detached/tooltip fields — whose input wrapper renders outside the// attached-status wrapper — compete with page-level stacking (#5689).isolation: 'isolate',+ // The Field root is the flex/grid item a row actually sizes. With the+ // automatic minimum it held a filter bar at its controls' intrinsic+ // widths (a native input's ~20ch, a Selector's full value), so the row+ // overflowed on phones; the reset lets the row shrink the whole field.+ minWidth: 0,},containerGap: {gap: spacingVars['--spacing-1'],diff --git a/packages/core/src/Selector/Selector.doc.mjs b/packages/core/src/Selector/Selector.doc.mjsindex 6f716d1d8e..64c02c3173 100644--- a/packages/core/src/Selector/Selector.doc.mjs+++ b/packages/core/src/Selector/Selector.doc.mjs@@ -344,7 +344,7 @@ export const docs = {name: 'width',type: 'SizeValue',description:- 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned.',+ 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. Without a width the field follows its displayed value and can shrink with its row (the value truncates) instead of overflowing it; use `<HStack wrap="wrap">` for filter bars so selectors move to a second line on phones.',},{name: 'startIcon',diff --git a/packages/core/src/Selector/Selector.test.tsx b/packages/core/src/Selector/Selector.test.tsxindex 452a79c5bb..24f5f2ede6 100644--- a/packages/core/src/Selector/Selector.test.tsx+++ b/packages/core/src/Selector/Selector.test.tsx@@ -4614,3 +4614,33 @@ describe('Selector press model', () => {);});});++describe('Selector in a narrow row', () => {+ it('lets a row shrink a standalone selector instead of overflowing', () => {+ const {container} = render(+ <Selector+ label="Status"+ options={['Awaiting fulfillment', 'Shipped']}+ value="Awaiting fulfillment"+ onChange={() => {}}+ />,+ );+ const root = container.querySelector('.astryx-field')!;+ expect(getComputedStyle(root).minWidth).toBe('0');+ });++ it('keeps an explicit width alongside the reset', () => {+ const {container} = render(+ <Selector+ label="Qty"+ options={['1', '2']}+ value="1"+ onChange={() => {}}+ width={80}+ />,+ );+ const root = container.querySelector('.astryx-field')!;+ expect(root.getAttribute('style')).toContain('80');+ expect(getComputedStyle(root).minWidth).toBe('0');+ });+});diff --git a/packages/core/src/TextInput/TextInput.doc.mjs b/packages/core/src/TextInput/TextInput.doc.mjsindex 97fba3402f..5b828c6c2d 100644--- a/packages/core/src/TextInput/TextInput.doc.mjs+++ b/packages/core/src/TextInput/TextInput.doc.mjs@@ -162,7 +162,7 @@ export const docs = {name: 'width',type: 'SizeValue',description:- 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned.',+ 'Width of the field (number = pixels, string used as-is, e.g. "100%"). Sizes the whole field (label, control, and status) so they stay aligned. Without a width the field can shrink with its row, so a filter bar never overflows; use `<HStack wrap="wrap">` when the controls should move to a second line instead.',},{name: 'autoComplete',diff --git a/packages/core/src/TextInput/TextInput.test.tsx b/packages/core/src/TextInput/TextInput.test.tsxindex 36ed1ca7b7..df16b96608 100644--- a/packages/core/src/TextInput/TextInput.test.tsx+++ b/packages/core/src/TextInput/TextInput.test.tsx@@ -1132,3 +1132,12 @@ describe('TextInput text size', () => {});});+describe('TextInput in a narrow row', () => {+ it('lets a filter bar shrink the whole field instead of overflowing', () => {+ const {container} = render(+ <TextInput label="Search" value="" onChange={() => {}} />,+ );+ const root = container.querySelector('.astryx-field')!;+ expect(getComputedStyle(root).minWidth).toBe('0');+ });+});