An open source design system that's fully customizable and agent ready
TypeScript
resultCount (#593)
Andrey Godere9462c778817
diff --git a/apps/storybook/stories/PowerSearch.stories.tsx b/apps/storybook/stories/PowerSearch.stories.tsxindex f4190a0b93..ef0d13b1e1 100644--- a/apps/storybook/stories/PowerSearch.stories.tsx+++ b/apps/storybook/stories/PowerSearch.stories.tsx@@ -7,6 +7,7 @@ import type {PowerSearchChangeType,} from '@xds/core/PowerSearch';import type {XDSSearchSource, XDSSearchableItem} from '@xds/core/Typeahead';+import {XDSButton} from '@xds/core/Button';import {MagnifyingGlassIcon} from '@heroicons/react/24/outline';// =============================================================================@@ -915,3 +916,49 @@ export const WithStartIcon: Story = {},name: 'With Start Icon',};++export const WithResultCount: Story = {+ render: args => {+ const [filters, setFilters] = useState<PowerSearchFilter[]>([+ {field: 'status', operator: 'is', value: {type: 'enum', value: 'open'}},+ ]);+ return (+ <XDSPowerSearch+ {...args}+ config={basicConfig}+ filters={filters}+ onChange={newFilters => setFilters([...newFilters])}+ resultCount={1234}+ startIcon={MagnifyingGlassIcon}+ />+ );+ },+ args: {+ label: 'Search',+ isLabelHidden: true,+ placeholder: 'Search...',+ },+ name: 'With Result Count',+};++export const WithEndContentPowerSearch: Story = {+ render: args => {+ const [filters, setFilters] = useState<PowerSearchFilter[]>([]);+ return (+ <XDSPowerSearch+ {...args}+ config={basicConfig}+ filters={filters}+ onChange={newFilters => setFilters([...newFilters])}+ resultCount={42}+ endContent={<XDSButton label="Save" variant="primary" size="sm" />}+ />+ );+ },+ args: {+ label: 'Search',+ isLabelHidden: true,+ placeholder: 'Search...',+ },+ name: 'With End Content and Result Count',+};diff --git a/apps/storybook/stories/Tokenizer.stories.tsx b/apps/storybook/stories/Tokenizer.stories.tsxindex a62fd766dd..8104f9e296 100644--- a/apps/storybook/stories/Tokenizer.stories.tsx+++ b/apps/storybook/stories/Tokenizer.stories.tsx@@ -2,6 +2,7 @@ import {useState} from 'react';import type {Meta, StoryObj} from '@storybook/react';import {XDSTokenizer} from '@xds/core/Tokenizer';import type {XDSSearchableItem, XDSSearchSource} from '@xds/core/Typeahead';+import {XDSButton} from '@xds/core/Button';import {MagnifyingGlassIcon} from '@heroicons/react/24/outline';// Sample data@@ -187,3 +188,25 @@ export const WithStartIconAndTokens: Story = {},name: 'With Start Icon and Tokens',};++export const WithEndContent: Story = {+ render: args => {+ const [value, setValue] = useState<XDSSearchableItem[]>([+ users[0],+ users[2],+ ]);+ return (+ <XDSTokenizer+ {...args}+ searchSource={userSource}+ value={value}+ onChange={items => setValue(items)}+ endContent={<XDSButton label="Apply" variant="primary" size="sm" />}+ />+ );+ },+ args: {+ label: 'Team Members',+ },+ name: 'With End Content',+};diff --git a/packages/core/src/PowerSearch/XDSPowerSearch.tsx b/packages/core/src/PowerSearch/XDSPowerSearch.tsxindex 3f2699435f..3eff91ee17 100644--- a/packages/core/src/PowerSearch/XDSPowerSearch.tsx+++ b/packages/core/src/PowerSearch/XDSPowerSearch.tsx@@ -29,7 +29,12 @@ import type {XDSIconType} from '../Icon';import type {XDSIconName} from '../Icon/globalIconRegistry';import type {XDSInputStatus} from '../Field';import {useXDSLayer} from '../Layer';-import {spacingVars} from '../theme/tokens.stylex';+import {+ spacingVars,+ colorVars,+ textSizeVars,+ lineHeightVars,+} from '../theme/tokens.stylex';import {useInternalConfig} from './useInternalConfig';import {usePowerSearchSource} from './usePowerSearchSource';import {formatFilterValue} from './formatFilterValue';@@ -86,6 +91,15 @@ const popoverLayerStyles = stylex.create({},});+const resultCountStyles = stylex.create({+ text: {+ fontSize: textSizeVars['--text-sm'],+ lineHeight: lineHeightVars['--leading-snug'],+ color: colorVars['--color-text-secondary'],+ whiteSpace: 'nowrap',+ },+});+function truncateString(value: string, limit: number): string {return value.length > limit + 3 ? value.slice(0, limit) + '...' : value;}@@ -341,6 +355,16 @@ export interface XDSPowerSearchProps {popoverSaveButtonLabel?: string;/** Timezone ID for date formatting. */timezoneID?: string;+ /**+ * Content to display at the end of the input row.+ * Useful for action buttons or other controls.+ */+ endContent?: React.ReactNode;+ /**+ * Number of results matching the current filters.+ * When a number, formatted as "N results". When a string, displayed as-is.+ */+ resultCount?: number | string;/** Imperative handle ref. */ref?: React.Ref<XDSPowerSearchHandle>;/** Test ID. */@@ -431,6 +455,8 @@ export function XDSPowerSearch({maxTokenLength = 40,popoverSaveButtonLabel = 'Apply',timezoneID,+ endContent,+ resultCount,ref,'data-testid': testId,xstyle,@@ -701,6 +727,35 @@ export function XDSPowerSearch({const popoverPartialFilter =popoverState.type !== 'idle' ? popoverState.partialFilter : null;+ // Build combined endContent from resultCount + endContent props+ const combinedEndContent = useMemo(() => {+ let resultCountNode: React.ReactNode = null;+ if (resultCount != null) {+ if (typeof resultCount === 'number') {+ const formatted = new Intl.NumberFormat().format(resultCount);+ resultCountNode = (+ <span {...stylex.props(resultCountStyles.text)}>+ {formatted} {resultCount === 1 ? 'result' : 'results'}+ </span>+ );+ } else {+ resultCountNode = (+ <span {...stylex.props(resultCountStyles.text)}>{resultCount}</span>+ );+ }+ }++ if (resultCountNode && endContent) {+ return (+ <>+ {resultCountNode}+ {endContent}+ </>+ );+ }+ return resultCountNode || endContent || undefined;+ }, [resultCount, endContent]);+return (<><div ref={layer.ref}>@@ -717,6 +772,7 @@ export function XDSPowerSearch({hasAutoFocus={hasAutoFocus}hasClear={hasClear && !isReadOnly}startIcon={startIcon}+ endContent={combinedEndContent}isDisabled={isDisabled}hasEntriesOnFocusdebounceMs={0}diff --git a/packages/core/src/Tokenizer/XDSTokenizer.tsx b/packages/core/src/Tokenizer/XDSTokenizer.tsxindex 27ec838dc5..55376c09a7 100644--- a/packages/core/src/Tokenizer/XDSTokenizer.tsx+++ b/packages/core/src/Tokenizer/XDSTokenizer.tsx@@ -116,6 +116,11 @@ export interface XDSTokenizerProps<T extends XDSSearchableItem> {isDisabled?: boolean;/** Show clear button (clears all tokens). @default false */hasClear?: boolean;+ /**+ * Content to display at the end of the input row.+ * Useful for buttons, result counts, or other controls.+ */+ endContent?: ReactNode;/** Auto-focus on mount. @default false */hasAutoFocus?: boolean;/** Input size. @default 'md' */@@ -192,6 +197,12 @@ const styles = stylex.create({'@media (hover: hover)': 1,},},+ flexShrink: 0,+ },+ endSection: {+ display: 'flex',+ alignItems: 'center',+ gap: spacingVars['--spacing-1'],marginInlineStart: 'auto',flexShrink: 0,},@@ -280,6 +291,7 @@ export function XDSTokenizer<T extends XDSSearchableItem>({emptySearchResultsText,isDisabled = false,hasClear = false,+ endContent,hasAutoFocus,size = 'md',debounceMs,@@ -490,17 +502,22 @@ export function XDSTokenizer<T extends XDSSearchableItem>({: undefined}/>- {hasClear && value.length > 0 && !isDisabled && (- <button- type="button"- aria-label="Clear all"- onClick={e => {- e.stopPropagation();- handleClearAll();- }}- {...stylex.props(styles.clearAllButton)}>- <XDSIcon icon="close" size="sm" />- </button>+ {(endContent || (hasClear && value.length > 0 && !isDisabled)) && (+ <div {...stylex.props(styles.endSection)}>+ {endContent}+ {hasClear && value.length > 0 && !isDisabled && (+ <button+ type="button"+ aria-label="Clear all"+ onClick={e => {+ e.stopPropagation();+ handleClearAll();+ }}+ {...stylex.props(styles.clearAllButton)}>+ <XDSIcon icon="close" size="sm" />+ </button>+ )}+ </div>)}</div></XDSField>