-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: Allow ReactElement in LabeledValue value #7679
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
Changes from 18 commits
9493cf0
88f15a3
0afe1be
550d2f4
80305b2
2215ad5
56e6602
07ceb2c
2ff7369
1d8b5f4
c01680d
f57fc8a
c86e72f
e2f1044
99cb77e
b1dd5db
66cbe10
c3cb043
2eb8bd4
d482cc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {DateTime, LabeledValueBaseProps} from '@react-spectrum/labeledvalue/src/LabeledValue'; | ||
import {RangeValue} from '@react-types/shared'; | ||
import {ReactElement} from 'react'; | ||
|
||
// The doc generator is not smart enough to handle the real types for LabeledValue so this is a simpler one. | ||
export interface LabeledValueProps extends LabeledValueBaseProps { | ||
/** The value to display. */ | ||
value: string | string[] | number | RangeValue<number> | DateTime | RangeValue<DateTime>, | ||
value: string | string[] | number | RangeValue<number> | DateTime | RangeValue<DateTime> | ReactElement, | ||
/** Formatting options for the value. The available options depend on the type passed to the `value` prop. */ | ||
formatOptions?: Intl.NumberFormatOptions | Intl.DateTimeFormatOptions | Intl.ListFormatOptions | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,15 @@ | |
*/ | ||
|
||
import {CalendarDate, CalendarDateTime, Time, ZonedDateTime} from '@internationalized/date'; | ||
import {ComponentMeta, ComponentStoryObj} from '@storybook/react'; | ||
import {Content} from '@react-spectrum/view'; | ||
import {ContextualHelp} from '@react-spectrum/contextualhelp'; | ||
import {Heading} from '@react-spectrum/text'; | ||
import {LabeledValue} from '..'; | ||
import {Link} from '@react-spectrum/link'; | ||
import {Meta} from '@storybook/react'; | ||
import React from 'react'; | ||
|
||
type LabeledValueStory = ComponentStoryObj<typeof LabeledValue>; | ||
|
||
export default { | ||
const meta: Meta<typeof LabeledValue> = { | ||
title: 'LabeledValue', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding this back in fixed the storybook naming structure |
||
component: LabeledValue, | ||
argTypes: { | ||
|
@@ -43,84 +42,95 @@ export default { | |
} | ||
} | ||
} | ||
} as ComponentMeta<typeof LabeledValue>; | ||
}; | ||
|
||
export default meta; | ||
|
||
export let Default: LabeledValueStory = { | ||
export let Default = { | ||
args: {label: 'Test', value: 'foo '.repeat(20)}, | ||
name: 'String' | ||
}; | ||
|
||
export let StringArray: LabeledValueStory = { | ||
export let StringArray = { | ||
args: {label: 'Test', value: ['wow', 'cool', 'awesome']}, | ||
name: 'String array' | ||
}; | ||
|
||
export let CalendarDateType: LabeledValueStory = { | ||
export let CalendarDateType = { | ||
args: {label: 'Test', value: new CalendarDate(2019, 6, 5)}, | ||
name: 'CalendarDate' | ||
}; | ||
|
||
export let CalendarDateTimeType: LabeledValueStory = { | ||
export let CalendarDateTimeType = { | ||
args: {label: 'Test', value: new CalendarDateTime(2020, 2, 3, 12, 23, 24, 120)}, | ||
name: 'CalendarDateTime' | ||
}; | ||
|
||
export let CalendarDateTimeTypeFormatOptions: LabeledValueStory = { | ||
export let CalendarDateTimeTypeFormatOptions = { | ||
args: {label: 'Test', value: new CalendarDateTime(2020, 2, 3, 12, 23, 24, 120), formatOptions: {dateStyle: 'short', timeStyle: 'short'}}, | ||
name: 'CalendarDateTime with formatOptions' | ||
}; | ||
|
||
export let ZonedDateTimeType: LabeledValueStory = { | ||
export let ZonedDateTimeType = { | ||
args: {label: 'Test', value: new ZonedDateTime(2020, 2, 3, 'America/Los_Angeles', -28800000)}, | ||
name: 'ZonedDateTime' | ||
}; | ||
|
||
export let DateType: LabeledValueStory = { | ||
export let DateType = { | ||
args: {label: 'Test', value: new Date(2000, 5, 5)}, | ||
name: 'Date' | ||
}; | ||
|
||
export let TimeType: LabeledValueStory = { | ||
export let TimeType = { | ||
args: {label: 'Test', value: new Time(9, 45)}, | ||
name: 'Time' | ||
}; | ||
|
||
export let CalendarDateRange: LabeledValueStory = { | ||
export let CalendarDateRange = { | ||
args: {label: 'Test', value: {start: new CalendarDate(2019, 6, 5), end: new CalendarDate(2019, 7, 5)}}, | ||
name: 'RangeValue<CalendarDate>' | ||
}; | ||
|
||
export let CalendarDateTimeRange: LabeledValueStory = { | ||
export let CalendarDateTimeRange = { | ||
args: {label: 'Test', value: {start: new CalendarDateTime(2020, 2, 3, 12, 23, 24, 120), end: new CalendarDateTime(2020, 3, 3, 12, 23, 24, 120)}}, | ||
name: 'RangeValue<CalendarDateTime>' | ||
}; | ||
|
||
export let ZonedDateTimeRange: LabeledValueStory = { | ||
export let ZonedDateTimeRange = { | ||
args: {label: 'Test', value: {start: new ZonedDateTime(2020, 2, 3, 'America/Los_Angeles', -28800000), end: new ZonedDateTime(2020, 3, 3, 'America/Los_Angeles', -28800000)}}, | ||
name: 'RangeValue<ZonedDateTime>' | ||
}; | ||
|
||
export let DateRange: LabeledValueStory = { | ||
export let DateRange = { | ||
args: {label: 'Test', value: {start: new Date(2019, 6, 5), end: new Date(2019, 6, 10)}}, | ||
name: 'RangeValue<Date>' | ||
}; | ||
|
||
export let TimeRange: LabeledValueStory = { | ||
export let TimeRange = { | ||
args: {label: 'Test', value: {start: new Time(9, 45), end: new Time(10, 50)}}, | ||
name: 'RangeValue<Time>' | ||
}; | ||
|
||
export let Number: LabeledValueStory = { | ||
export let Number = { | ||
args: {label: 'Test', value: 10}, | ||
name: 'Number' | ||
}; | ||
|
||
export let NumberRange: LabeledValueStory = { | ||
export let NumberRange = { | ||
args: {label: 'Test', value: {start: 10, end: 20}}, | ||
name: 'RangeValue<Number>' | ||
}; | ||
|
||
export let WithContextualHelp: LabeledValueStory = { | ||
|
||
export let CustomComponent = { | ||
args: { | ||
label: 'Test', | ||
value: <Link href="https://www.adobe.com">Adobe</Link> | ||
}, | ||
name: 'Custom component' | ||
}; | ||
|
||
export let WithContextualHelp = { | ||
args: { | ||
label: 'Test', | ||
value: 25, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,6 +275,35 @@ describe('LabeledValue', function () { | |
expect(staticField).toHaveTextContent('10 β 20'); | ||
}); | ||
|
||
it('renders correctly with ReactElement value', function () { | ||
let {getByTestId} = render( | ||
<LabeledValue | ||
data-testid="test-id" | ||
label="Field label" | ||
value={<a href="https://test.com">test</a>} /> | ||
); | ||
|
||
let staticField = getByTestId('test-id'); | ||
expect(staticField).toBeInTheDocument(); | ||
expect(staticField).toHaveTextContent('test'); | ||
expect( | ||
within(staticField).getByRole('link', {name: 'test'}) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('throws when an editable value is provided', async function () { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
try { | ||
render( | ||
<LabeledValue | ||
label="Field label" | ||
value={<input />} /> | ||
); | ||
} catch (e) { | ||
expect(e.message).toEqual('LabeledValue cannot contain an editable value.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's possible that this isn't throwing, and the test is still passing. We'll want to do the assertion outside of the catch to double check this. I'll try to double check this test when I have a chance. The change looks great, but there may be something we're missing in terms of the test setup. Sorry for the delays! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the assertion outside of the catch! |
||
} | ||
}); | ||
|
||
it('attaches a user provided ref to the outer div', function () { | ||
let ref = React.createRef(); | ||
let {getByTestId} = render( | ||
|
Uh oh!
There was an error while loading. Please reload this page.