LabeledValue
A LabeledValue displays a non-editable value with a label. It formats numbers, dates, times, and lists according to the user's locale.
install | yarn add @adobe/react-spectrum |
---|---|
added | 3.22.0 |
usage | import {LabeledValue} from '@adobe/react-spectrum' |
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 {getLocalTimeZone, now} from '@internationalized/date';
<LabeledValue label="Page load time" value={now(getLocalTimeZone())} />
import {
getLocalTimeZone,
now
} from '@internationalized/date';
<LabeledValue
label="Page load time"
value={now(getLocalTimeZone())}
/>
import {
getLocalTimeZone,
now
} from '@internationalized/date';
<LabeledValue
label="Page load time"
value={now(
getLocalTimeZone()
)}
/>
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#
Name | Type | Default | Description |
value | string
| string[]
| number
| RangeValue<number>
| DateTime
| RangeValue<DateTime> | — | The value to display. |
label | ReactNode | — | The content to display as the label. |
formatOptions | Intl.NumberFormatOptions
| Intl.DateTimeFormatOptions
| Intl.ListFormatOptions | — | Formatting options for the value. The available options depend on the type passed to the value prop. |
labelPosition | LabelPosition | 'top' | The label's overall position relative to the element it is labeling. |
labelAlign | Alignment | 'start' | The label's horizontal alignment relative to the element it is labeling. |
contextualHelp | ReactNode | — | A ContextualHelp element to place next to the label. |
Layout
Name | Type | Description |
flex | Responsive<string
| number
| boolean> | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'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. |
justifySelf | Responsive<'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. |
order | Responsive<number> | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
Spacing
Name | Type | Description |
margin | Responsive<DimensionValue> | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | The margin for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Description |
width | Responsive<DimensionValue> | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | The maximum height of the element. See MDN. |
Positioning
Name | Type | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | Hides the element. |
Advanced
Name | Type | Description |
UNSAFE_className | string | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | Sets inline style for the element. Only use as a last resort. Use style props instead. |
Visual options#
Label alignment and position#
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"
/>
Contextual help#
A ContextualHelp element may be placed next to the label to provide additional information or help about a LabeledValue.
import {Content, ContextualHelp, Heading} from '@adobe/react-spectrum';
<LabeledValue
label="Aperture"
value="f/1.5"
contextualHelp={
<ContextualHelp>
<Heading>What is the aperture?</Heading>
<Content>
The aperture setting controls the amount of light reaching the image
sensor.
</Content>
</ContextualHelp>
}
/>
import {
Content,
ContextualHelp,
Heading
} from '@adobe/react-spectrum';
<LabeledValue
label="Aperture"
value="f/1.5"
contextualHelp={
<ContextualHelp>
<Heading>What is the aperture?</Heading>
<Content>
The aperture setting controls the amount of light
reaching the image sensor.
</Content>
</ContextualHelp>
}
/>
import {
Content,
ContextualHelp,
Heading
} from '@adobe/react-spectrum';
<LabeledValue
label="Aperture"
value="f/1.5"
contextualHelp={
<ContextualHelp>
<Heading>
What is the
aperture?
</Heading>
<Content>
The aperture
setting
controls the
amount of light
reaching the
image sensor.
</Content>
</ContextualHelp>
}
/>