alpha

LabeledValue

A LabeledValue displays a non-editable value with a label. It formats numbers, dates, times, and lists according to the user's locale.

installyarn add @react-spectrum/labeledvalue
version3.0.0-alpha.1
usageimport {LabeledValue} from '@react-spectrum/labeledvalue'

Example#


<LabeledValue label="File name" value="Budget.xls" />
<LabeledValue label="File name" value="Budget.xls" />
<LabeledValue
  label="File name"
  value="Budget.xls"
/>

Value#


In addition to a string as shown above, a LabeledValue accepts numbers, dates, times, and lists of strings in the value prop.

Numbers#

When the value prop is set to a number, LabeledValue formats it according to the user's locale.

<LabeledValue label="Number of cookies" value={1024} />
<LabeledValue label="Number of cookies" value={1024} />
<LabeledValue
  label="Number of cookies"
  value={1024}
/>

Custom formatOptions can also be provided to format the value as a percentage, unit, currency, etc. This prop is compatible with the option parameter of Intl.NumberFormat.

<LabeledValue
  label="File size"
  value={1.2}
  formatOptions={{ style: 'unit', unit: 'megabyte' }}
/>
<LabeledValue
  label="File size"
  value={1.2}
  formatOptions={{ style: 'unit', unit: 'megabyte' }}
/>
<LabeledValue
  label="File size"
  value={1.2}
  formatOptions={{
    style: 'unit',
    unit: 'megabyte'
  }}
/>

An object with start and end properties may also be provided to format a numeric range.

<LabeledValue
  label="Price range"
  value={{ start: 150, end: 400 }}
  formatOptions={{
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 0
  }}
/>
<LabeledValue
  label="Price range"
  value={{ start: 150, end: 400 }}
  formatOptions={{
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits: 0
  }}
/>
<LabeledValue
  label="Price range"
  value={{
    start: 150,
    end: 400
  }}
  formatOptions={{
    style: 'currency',
    currency: 'USD',
    minimumFractionDigits:
      0
  }}
/>

Dates and times#

The value prop may be set to a JavaScript Date object, or one of the types from @internationalized/date to display a date or time, which is formatted according to the user's locale.

import {getLocalTimeZone, today} from '@internationalized/date';

<LabeledValue
  label="Date modified"
  value={today(getLocalTimeZone()).subtract({ weeks: 1 })}
/>
import {
  getLocalTimeZone,
  today
} from '@internationalized/date';

<LabeledValue
  label="Date modified"
  value={today(getLocalTimeZone()).subtract({ weeks: 1 })}
/>
import {
  getLocalTimeZone,
  today
} from '@internationalized/date';

<LabeledValue
  label="Date modified"
  value={today(
    getLocalTimeZone()
  ).subtract({
    weeks: 1
  })}
/>

By default, the formatting depends on the type of value provided. Above, a CalendarDate is provided, so only the date is displayed. To display a time, pass a Time object. You can also provide a CalendarDateTime or ZonedDateTime to display a date with a time.

import {now} from '@internationalized/date';

<LabeledValue label="Page load time" value={now()} />
import {now} from '@internationalized/date';

<LabeledValue label="Page load time" value={now()} />
import {now} from '@internationalized/date';

<LabeledValue
  label="Page load time"
  value={now()}
/>

An object with start and end properties may also be provided to format a date or time range.

import {Time} from '@internationalized/date';

<LabeledValue
  label="Business hours"
  value={{ start: new Time(8, 30), end: new Time(18) }}
/>
import {Time} from '@internationalized/date';

<LabeledValue
  label="Business hours"
  value={{ start: new Time(8, 30), end: new Time(18) }}
/>
import {Time} from '@internationalized/date';

<LabeledValue
  label="Business hours"
  value={{
    start: new Time(
      8,
      30
    ),
    end: new Time(18)
  }}
/>

Custom formatOptions can also be provided control the exact date format. This prop is compatible with the option parameter of Intl.DateTimeFormat.

<LabeledValue
  label="Appointment date"
  value={new Date(2022, 6, 5)}
  formatOptions={{ dateStyle: 'short' }}
/>
<LabeledValue
  label="Appointment date"
  value={new Date(2022, 6, 5)}
  formatOptions={{ dateStyle: 'short' }}
/>
<LabeledValue
  label="Appointment date"
  value={new Date(
    2022,
    6,
    5
  )}
  formatOptions={{
    dateStyle: 'short'
  }}
/>

Lists#

When the value prop is set to an array of strings, they are rendered as a comma-separated list according to the user's locale.

<LabeledValue
  label="Pizza toppings"
  value={['Pepperoni', 'Pineapple', 'Mushroom', 'Garlic']}
/>
<LabeledValue
  label="Pizza toppings"
  value={['Pepperoni', 'Pineapple', 'Mushroom', 'Garlic']}
/>
<LabeledValue
  label="Pizza toppings"
  value={[
    'Pepperoni',
    'Pineapple',
    'Mushroom',
    'Garlic'
  ]}
/>

By default, the list is displayed as a conjunction (an "and"-based grouping of items). This may be changed to a disjunction (an "or"-based grouping), or a pure comma-separated list using the formatOptions prop. This is compatible with the option parameter of Intl.ListFormat.

<LabeledValue
  label="Interests"
  value={['Travel', 'Hiking', 'Snorkeling', 'Camping']}
  formatOptions={{ type: 'unit' }}
/>
<LabeledValue
  label="Interests"
  value={['Travel', 'Hiking', 'Snorkeling', 'Camping']}
  formatOptions={{ type: 'unit' }}
/>
<LabeledValue
  label="Interests"
  value={[
    'Travel',
    'Hiking',
    'Snorkeling',
    'Camping'
  ]}
  formatOptions={{
    type: 'unit'
  }}
/>

Labeling#


A visual label must be provided to the LabeledValue using the label prop.

Internationalization#

In order to internationalize a LabeledValue, a localized string should be passed to the label prop.

LabeledValue automatically formats numbers, dates, times, and lists according to the user's locale, as defined by the Provider component. String values provided to a LabeledValue should be translated accordingly.

Props#


NameTypeDefaultDescription
valuestringstring[]numberRangeValue<number>DateTimeRangeValue<DateTime>The value to display.
labelReactNodeThe content to display as the label.
formatOptionsIntl.NumberFormatOptionsIntl.DateTimeFormatOptionsIntl.ListFormatOptionsFormatting options for the value. The available options depend on the type passed to the value prop.
labelPositionLabelPosition'top'The label's overall position relative to the element it is labeling.
labelAlignAlignment'start'The label's horizontal alignment relative to the element it is labeling.
contextualHelpReactNodeA ContextualHelp element to place next to the label.
Layout
NameTypeDefaultDescription
flexResponsive<stringnumberboolean>When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN.
flexGrowResponsive<number>When used in a flex layout, specifies how the element will grow to fit the space available. See MDN.
flexShrinkResponsive<number>When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN.
flexBasisResponsive<numberstring>When used in a flex layout, specifies the initial main size of the element. See MDN.
alignSelfResponsive<'auto''normal''start''end''center''flex-start''flex-end''self-start''self-end''stretch'>Overrides the alignItems property of a flex or grid container. See MDN.
justifySelfResponsive<'auto''normal''start''end''flex-start''flex-end''self-start''self-end''center''left''right''stretch'>Specifies how the element is justified inside a flex or grid container. See MDN.
orderResponsive<number>The layout order for the element within a flex or grid container. See MDN.
gridAreaResponsive<string>When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN.
gridColumnResponsive<string>When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN.
gridRowResponsive<string>When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN.
gridColumnStartResponsive<string>When used in a grid layout, specifies the starting column to span within the grid. See MDN.
gridColumnEndResponsive<string>When used in a grid layout, specifies the ending column to span within the grid. See MDN.
gridRowStartResponsive<string>When used in a grid layout, specifies the starting row to span within the grid. See MDN.
gridRowEndResponsive<string>When used in a grid layout, specifies the ending row to span within the grid. See MDN.
Spacing
NameTypeDefaultDescription
marginResponsive<DimensionValue>The margin for all four sides of the element. See MDN.
marginTopResponsive<DimensionValue>The margin for the top side of the element. See MDN.
marginBottomResponsive<DimensionValue>The margin for the bottom side of the element. See MDN.
marginStartResponsive<DimensionValue>The margin for the logical start side of the element, depending on layout direction. See MDN.
marginEndResponsive<DimensionValue>The margin for the logical end side of an element, depending on layout direction. See MDN.
marginXResponsive<DimensionValue>The margin for both the left and right sides of the element. See MDN.
marginYResponsive<DimensionValue>The margin for both the top and bottom sides of the element. See MDN.
Sizing
NameTypeDefaultDescription
widthResponsive<DimensionValue>The width of the element. See MDN.
minWidthResponsive<DimensionValue>The minimum width of the element. See MDN.
maxWidthResponsive<DimensionValue>The maximum width of the element. See MDN.
heightResponsive<DimensionValue>The height of the element. See MDN.
minHeightResponsive<DimensionValue>The minimum height of the element. See MDN.
maxHeightResponsive<DimensionValue>The maximum height of the element. See MDN.
Positioning
NameTypeDefaultDescription
positionResponsive<'static''relative''absolute''fixed''sticky'>Specifies how the element is positioned. See MDN.
topResponsive<DimensionValue>The top position for the element. See MDN.
bottomResponsive<DimensionValue>The bottom position for the element. See MDN.
leftResponsive<DimensionValue>The left position for the element. See MDN. Consider using start instead for RTL support.
rightResponsive<DimensionValue>The right position for the element. See MDN. Consider using start instead for RTL support.
startResponsive<DimensionValue>The logical start position for the element, depending on layout direction. See MDN.
endResponsive<DimensionValue>The logical end position for the element, depending on layout direction. See MDN.
zIndexResponsive<number>The stacking order for the element. See MDN.
isHiddenResponsive<boolean>Hides the element.
Accessibility
NameTypeDefaultDescription
idstringThe element's unique identifier. See MDN.
Advanced
NameTypeDefaultDescription
UNSAFE_classNamestringSets the CSS className for the element. Only use as a last resort. Use style props instead.
UNSAFE_styleCSSPropertiesSets inline style for the element. Only use as a last resort. Use style props instead.

Visual options#


Label alignment and position#

View guidelines

By default, the label is positioned above the LabeledValue. The labelPosition prop can be used to position the label to the side. The labelAlign prop can be used to align the label as "start" or "end". For left-to-right (LTR) languages, "start" refers to the left most edge of the LabeledValue and "end" refers to the right-most edge. For right-to-left (RTL) languages, this is flipped.

<LabeledValue
  label="File name"
  value="Onboarding.pdf"
  labelPosition="side"
  labelAlign="end"
/>
<LabeledValue
  label="File name"
  value="Onboarding.pdf"
  labelPosition="side"
  labelAlign="end"
/>
<LabeledValue
  label="File name"
  value="Onboarding.pdf"
  labelPosition="side"
  labelAlign="end"
/>