-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: Add S2 LabeledValue #9965
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
Open
yihuiliao
wants to merge
16
commits into
main
Choose a base branch
from
s2-labeled-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7131ea5
initialize labeled value component
yihuiliao 0852a25
cleanup code
yihuiliao 9e5c3f0
more fixes, update stories
yihuiliao 2894b68
update types
yihuiliao b4d5eaf
initialize docs
yihuiliao c194b1f
update labeled value alignment
yihuiliao 1917eae
update docs
yihuiliao 714306c
add chromatic stories
yihuiliao 47731ae
fix install
yihuiliao 128d20a
Merge branch 'main' into s2-labeled-value
yihuiliao 44ac0f7
update exports
yihuiliao 0458b5e
update docs
yihuiliao d66c547
cleanup
yihuiliao 191f5ce
fix indent
yihuiliao 251a69e
fix docs example
yihuiliao e79c1cc
Merge branch 'main' into s2-labeled-value
snowystinger 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
47 changes: 47 additions & 0 deletions
47
packages/@react-spectrum/s2/chromatic/LabeledValue.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,47 @@ | ||
| /* | ||
| * Copyright 2026 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import {LabeledValue} from '../src/LabeledValue'; | ||
| import type {Meta} from '@storybook/react'; | ||
|
|
||
| const meta: Meta<typeof LabeledValue> = { | ||
| component: LabeledValue, | ||
| parameters: { | ||
| chromaticProvider: {disableAnimations: true} | ||
| }, | ||
| title: 'S2 Chromatic/LabeledValue' | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| export { | ||
| Default, | ||
| LongText, | ||
| StringArray, | ||
| CalendarDateType, | ||
| CalendarDateTimeType, | ||
| CalendarDateTimeTypeFormatOptions, | ||
| ZonedDateTimeType, | ||
| DateType, | ||
| TimeType, | ||
| CalendarDateRange, | ||
| CalendarDateTimeRange, | ||
| ZonedDateTimeRange, | ||
| DateRange, | ||
| TimeRange, | ||
| Number, | ||
| NumberRange, | ||
| CustomComponents, | ||
| WithContextualHelp, | ||
| FormCustomLayoutExample, | ||
| FormLayout | ||
| } from '../stories/LabeledValue.stories'; |
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,2 @@ | ||
| export {LabeledValue, LabeledValueContext} from '../src/LabeledValue'; | ||
| export type {LabeledValueProps} from '../src/LabeledValue'; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| /* | ||
| * Copyright 2024 Adobe. All rights reserved. | ||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||
| * governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| import {CalendarDate, CalendarDateTime, getLocalTimeZone, Time, toCalendarDateTime, today, ZonedDateTime} from '@internationalized/date'; | ||
| import {ContextValue} from 'react-aria-components/slots'; | ||
| import {controlFont, controlSize, field, fieldInput, getAllowedOverrides, StyleProps} from './style-utils' with {type: 'macro'}; | ||
| import {createContext, forwardRef, isValidElement, ReactElement, ReactNode} from 'react'; | ||
| import {DOMProps, DOMRef, DOMRefValue, RangeValue, SpectrumLabelableProps} from '@react-types/shared'; | ||
| import {FieldLabel} from './Field'; | ||
| import {filterDOMProps} from 'react-aria/filterDOMProps'; | ||
| import {FormContext, useFormProps} from './Form'; | ||
| import {style} from '../style' with {type: 'macro'}; | ||
| import {useContext, useEffect} from 'react'; | ||
| import {useDateFormatter} from 'react-aria/useDateFormatter'; | ||
| import {useDOMRef} from './useDOMRef'; | ||
| import {useListFormatter} from 'react-aria/useListFormatter'; | ||
| import {useNumberFormatter} from 'react-aria/useNumberFormatter'; | ||
| import {useSpectrumContextProps} from './useSpectrumContextProps'; | ||
|
|
||
| type NumberValue = number | RangeValue<number>; | ||
| interface NumberProps<T extends NumberValue> { | ||
| /** The value to display. */ | ||
| value: T, | ||
| /** Formatting options for the value. */ | ||
| formatOptions?: Intl.NumberFormatOptions | ||
| } | ||
|
|
||
| export type DateTime = Date | CalendarDate | CalendarDateTime | ZonedDateTime | Time; | ||
| type RangeDateTime = RangeValue<DateTime>; | ||
| type DateTimeValue = DateTime | RangeDateTime; | ||
| interface DateProps<T extends DateTimeValue> { | ||
| /** The value to display. */ | ||
| value: T, | ||
| /** Formatting options for the value. */ | ||
| formatOptions?: Intl.DateTimeFormatOptions | ||
| } | ||
|
|
||
| interface StringProps<T extends string> { | ||
| /** The value to display. */ | ||
| value: T, | ||
| /** Formatting options for the value. */ | ||
| formatOptions?: never | ||
| } | ||
|
|
||
| interface StringListProps<T extends string[]> { | ||
| /** The value to display. */ | ||
| value: T, | ||
| /** Formatting options for the value. */ | ||
| formatOptions?: Intl.ListFormatOptions | ||
| } | ||
|
|
||
| interface ReactElementProps<T extends ReactElement> { | ||
| /** The value to display. */ | ||
| value: T, | ||
| /** Formatting options for the value. */ | ||
| formatOptions?: never | ||
| } | ||
|
|
||
| export interface LabeledValueStyleProps { | ||
| /** | ||
| * The size of the component. | ||
| * @default 'M' | ||
| */ | ||
| size?: 'S' | 'M' | 'L' | 'XL' | ||
| } | ||
| export interface LabeledValueBaseProps extends DOMProps, StyleProps, Omit<SpectrumLabelableProps, 'necessityIndicator' | 'isRequired'> { | ||
| /** The content to display as the label. */ | ||
| label: ReactNode | ||
| } | ||
| type LabeledValueTypeProps<T> = | ||
| T extends NumberValue ? NumberProps<T> : | ||
| T extends DateTimeValue ? DateProps<T> : | ||
| T extends string[] ? StringListProps<T> : | ||
| T extends string ? StringProps<T> : | ||
| T extends ReactElement ? ReactElementProps<T> : | ||
| never; | ||
|
|
||
| type LabeledValueTypes = string[] | string | Date | CalendarDate | CalendarDateTime | ZonedDateTime | Time | number | RangeValue<number> | RangeValue<DateTime> | ReactElement; | ||
| export type LabeledValueProps<T> = LabeledValueTypeProps<T> & LabeledValueBaseProps & LabeledValueStyleProps; | ||
|
|
||
| export const LabeledValueContext = createContext<ContextValue<Partial<LabeledValueProps<any>>, DOMRefValue<HTMLDivElement>>>(null); | ||
|
|
||
| const fieldStyles = style({ | ||
| ...field() | ||
| }, getAllowedOverrides()); | ||
|
|
||
| const valueStyles = style({ | ||
| ...fieldInput(), | ||
| minHeight: { | ||
| isInForm: controlSize() | ||
| }, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| font: controlFont() | ||
| }); | ||
|
|
||
| /** | ||
| * A LabeledValue displays a non-editable value with a label. It formats numbers, | ||
| * dates, times, and lists according to the user's locale. | ||
| */ | ||
| export const LabeledValue = /*#__PURE__*/ forwardRef(function LabeledValue<T extends LabeledValueTypes>(props: LabeledValueProps<T>, ref: DOMRef<HTMLDivElement>) { | ||
| [props, ref] = useSpectrumContextProps(props as any, ref, LabeledValueContext) as any; | ||
| props = useFormProps(props); | ||
| let { | ||
| label, | ||
| value, | ||
| formatOptions, | ||
| size = 'M', | ||
| labelPosition = 'top', | ||
| labelAlign = 'start', | ||
| contextualHelp, | ||
| UNSAFE_className = '', | ||
| UNSAFE_style, | ||
| styles, | ||
| ...otherProps | ||
| } = props; | ||
| let formContext = useContext(FormContext); | ||
|
|
||
| let domRef = useDOMRef(ref); | ||
| useEffect(() => { | ||
| if ( | ||
| domRef?.current && | ||
| domRef.current.querySelectorAll('input, [contenteditable], textarea') | ||
| .length > 0 | ||
| ) { | ||
| throw new Error('LabeledValue cannot contain an editable value.'); | ||
| } | ||
| }, [domRef]); | ||
|
|
||
| let children: ReactNode; | ||
|
|
||
| if (Array.isArray(value)) { | ||
| children = <FormattedStringList value={value as string[]} formatOptions={formatOptions as Intl.ListFormatOptions} />; | ||
| } else if (typeof value === 'number') { | ||
| children = <FormattedNumber value={value} formatOptions={formatOptions as Intl.NumberFormatOptions} />; | ||
| } else if (typeof value === 'object' && value !== null && 'start' in value && typeof (value as RangeValue<number>).start === 'number') { | ||
| children = <FormattedNumber value={value as RangeValue<number>} formatOptions={formatOptions as Intl.NumberFormatOptions} />; | ||
| } else if (typeof value === 'object' && value !== null && 'start' in value) { | ||
| children = <FormattedDate value={value as RangeDateTime} formatOptions={formatOptions as Intl.DateTimeFormatOptions} />; | ||
| } else if (value instanceof Date || (typeof value === 'object' && value !== null && ('calendar' in value || 'hour' in value))) { | ||
| children = <FormattedDate value={value as DateTime} formatOptions={formatOptions as Intl.DateTimeFormatOptions} />; | ||
| } else if (typeof value === 'string') { | ||
| children = value; | ||
| } else if (isValidElement(value)) { | ||
| children = value; | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| {...filterDOMProps(otherProps)} | ||
| ref={domRef} | ||
| style={UNSAFE_style} | ||
| className={UNSAFE_className + fieldStyles({ | ||
| isInForm: !!formContext, | ||
| labelPosition, | ||
| size | ||
| }, styles)} > | ||
| <FieldLabel | ||
| elementType="span" | ||
| size={size} | ||
| labelPosition={labelPosition} | ||
| labelAlign={labelAlign} | ||
| contextualHelp={contextualHelp}> | ||
| {label} | ||
| </FieldLabel> | ||
| <span className={valueStyles({isInForm: !!formContext, size, labelPosition})}> | ||
| {children} | ||
| </span> | ||
| </div> | ||
| ); | ||
| }); | ||
|
|
||
| function FormattedStringList({value, formatOptions}: {value: string[], formatOptions?: Intl.ListFormatOptions}) { | ||
| let formatter = useListFormatter(formatOptions ?? {}); | ||
| return <>{formatter.format(value)}</>; | ||
| } | ||
|
|
||
| function FormattedNumber({value, formatOptions}: {value: NumberValue, formatOptions?: Intl.NumberFormatOptions}) { | ||
| let formatter = useNumberFormatter(formatOptions); | ||
| if (typeof value === 'object') { | ||
| return <>{formatter.formatRange(value.start, value.end)}</>; | ||
| } | ||
| return <>{formatter.format(value)}</>; | ||
| } | ||
|
|
||
| function FormattedDate<T extends DateTimeValue>({value, formatOptions}: {value: T, formatOptions?: Intl.DateTimeFormatOptions}) { | ||
| if (!formatOptions) { | ||
| formatOptions = getDefaultFormatOptions('start' in value ? (value as RangeDateTime).start : value as DateTime); | ||
| } | ||
|
|
||
| let dateFormatter = useDateFormatter(formatOptions); | ||
| let timeZone = dateFormatter.resolvedOptions().timeZone || getLocalTimeZone(); | ||
|
|
||
| if ('start' in value && 'end' in value) { | ||
| let start = convertDateTime((value as RangeDateTime).start, timeZone); | ||
| let end = convertDateTime((value as RangeDateTime).end, timeZone); | ||
| return <>{dateFormatter.formatRange(start, end)}</>; | ||
| } | ||
|
|
||
| return <>{dateFormatter.format(convertDateTime(value as DateTime, timeZone))}</>; | ||
| } | ||
|
|
||
| function convertDateTime(value: DateTime, timeZone: string): Date { | ||
| if ('timeZone' in value) { | ||
| return (value as ZonedDateTime).toDate(); | ||
| } else if ('calendar' in value) { | ||
| return (value as CalendarDate | CalendarDateTime).toDate(timeZone); | ||
| } else if (!(value instanceof Date)) { | ||
| return toCalendarDateTime(today(getLocalTimeZone()), value as Time).toDate(timeZone); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| function getDefaultFormatOptions(value: DateTime): Intl.DateTimeFormatOptions { | ||
| if (value instanceof Date) { | ||
| return {dateStyle: 'long', timeStyle: 'short'}; | ||
| } else if ('timeZone' in value) { | ||
| return {year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: 'numeric', timeZone: (value as ZonedDateTime).timeZone, timeZoneName: 'short'}; | ||
| } else if ('hour' in value && 'year' in value) { | ||
| return {dateStyle: 'long', timeStyle: 'short'}; | ||
| } else if ('hour' in value) { | ||
| return {timeStyle: 'short'}; | ||
| } else { | ||
| return {dateStyle: 'long'}; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
When LabeledValue is placed in a horizontal row next to components like TextField, Picker, or NumberField, the value needs extra vertical spacing so it aligns with the input text of those components. Standalone, however, LabeledValue should keep tight spacing between label and value. (See the Figma design for the spec)
Right now, the way I'm ensuring that LabeledValue is aligned correctly is by setting the height via controlSize when it is inside a Form (and setting
alignItems: center).However, some other use cases to consider:
<Form>wrapper → wants alignment, but formContext is null.<Form>→ doesn't want the extra spacing, but formContext is set.Should we expect users to wrap LabeledValue inside
<Form>if they want alignment? Do we care if there's extra vertical spacing if it sits in a single vertical column?