diff --git a/CHANGELOG.md b/CHANGELOG.md index f977426818..ea05d41315 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Update Silver color scheme, adding `foregroundHover`, `foregroundPressed` and `background` definitions @pompomon ([#2078](https://github.com/microsoft/fluent-ui-react/pull/2078)) - Expanding experimental accessibility schema to more components @mshoho ([#2052](https://github.com/stardust-ui/react/pull/2052)) - Add base `Carousel` component @silviuavram ([#1979](https://github.com/microsoft/fluent-ui-react/pull/1979)) +- Add support for render props pattern via `children` prop to shorthands @layershifter ([#1951](https://github.com/stardust-ui/react/pull/1951)) ### Documentation - Add usage example for `Tooltip` on disabled elements @mnajdova ([#2091](https://github.com/microsoft/fluent-ui-react/pull/2091)) diff --git a/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx b/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx index 31b9e9c535..bcf9058881 100644 --- a/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx +++ b/docs/src/components/ComponentDoc/ComponentExample/ComponentExample.tsx @@ -342,8 +342,9 @@ class ComponentExample extends React.Component - render({ content: 'Copy' }, (Component, props) => ( + { + content: 'Copy', + children: (Component, props) => ( {(active, onClick) => ( )} - )), + ), + }, { disabled: currentCodeLanguage !== 'ts', icon: 'github', diff --git a/docs/src/examples/components/Avatar/Variations/AvatarExampleImageCustomization.shorthand.tsx b/docs/src/examples/components/Avatar/Variations/AvatarExampleImageCustomization.shorthand.tsx index 1dd39a16eb..566c56fa1a 100644 --- a/docs/src/examples/components/Avatar/Variations/AvatarExampleImageCustomization.shorthand.tsx +++ b/docs/src/examples/components/Avatar/Variations/AvatarExampleImageCustomization.shorthand.tsx @@ -14,10 +14,11 @@ const AvatarExampleImageCustomizationShorthand = () => ( />   + image={{ + name: 'chess rook', // This example does not react to the avatar size variable // and otherwise produces bad results when border is applied compared to "normal" image - render({ name: 'chess rook' }, (ComponentType, props) => ( + children: (ComponentType, props) => ( ( variables={{ color: 'blue' }} styles={{ boxSizing: 'border-box', padding: '8px' }} /> - )) - } + ), + }} status={{ color: 'green', icon: 'stardust-checkmark', title: 'Available' }} /> diff --git a/docs/src/examples/components/Menu/Usage/MenuExampleWithTooltip.shorthand.tsx b/docs/src/examples/components/Menu/Usage/MenuExampleWithTooltip.shorthand.tsx index 5c2eecfafe..8deff759ab 100644 --- a/docs/src/examples/components/Menu/Usage/MenuExampleWithTooltip.shorthand.tsx +++ b/docs/src/examples/components/Menu/Usage/MenuExampleWithTooltip.shorthand.tsx @@ -1,32 +1,36 @@ import * as React from 'react' import { Menu, Tooltip } from '@fluentui/react' +const itemRenderer = (MenuItem, props) => { + const { tooltip = '', ...rest } = props + + return ( + + + + ) +} const items = [ - { key: 'editorials', content: 'Editorials', tooltip: 'Click for opening Editorials' }, - { key: 'review', content: 'Reviews', tooltip: 'Click for opening Reviews' }, - { key: 'events', content: 'Upcoming Events', tooltip: 'Click for opening Upcoming Events' }, + { + key: 'editorials', + content: 'Editorials', + tooltip: 'Click for opening Editorials', + children: itemRenderer, + }, + { + key: 'review', + content: 'Reviews', + tooltip: 'Click for opening Reviews', + children: itemRenderer, + }, + { + key: 'events', + content: 'Upcoming Events', + tooltip: 'Click for opening Upcoming Events', + children: itemRenderer, + }, ] -const MenuExampleWithTooltip = () => ( - render => - render( - /* what to render */ - item, - - /* how to render */ - (MenuItem, props) => { - const { tooltip = '', ...rest } = props || {} - return ( - - - - ) - }, - ), - )} - /> -) +const MenuExampleWithTooltip = () => export default MenuExampleWithTooltip diff --git a/docs/src/examples/components/Toolbar/Performance/CustomToolbar.perf.tsx b/docs/src/examples/components/Toolbar/Performance/CustomToolbar.perf.tsx index 88884a24fc..458e39aae1 100644 --- a/docs/src/examples/components/Toolbar/Performance/CustomToolbar.perf.tsx +++ b/docs/src/examples/components/Toolbar/Performance/CustomToolbar.perf.tsx @@ -517,26 +517,23 @@ const layouts: Record = { const CustomToolbar: React.FunctionComponent = props => { const { layout = 'standard' } = props - const items = layouts[layout](props).map(item => - _.isNil((item as any).tooltip) - ? item - : render => - render( - item, // rendering Tooltip for the Toolbar Item - (ToolbarItem, props) => { - const { tooltip, key, ...rest } = props // Adding tooltipAsLabelBehavior as the ToolbarItems contains only icon - - return ( - } - accessibility={tooltipAsLabelBehavior} - content={tooltip} - /> - ) - }, - ), - ) + const items = layouts[layout](props).map((item: ToolbarItemProps) => ({ + ...item, + children: (item as any).tooltip + ? (ToolbarItem, props) => { + const { tooltip, key, ...rest } = props // Adding tooltipAsLabelBehavior as the ToolbarItems contains only icon + + return ( + } + accessibility={tooltipAsLabelBehavior} + content={tooltip} + /> + ) + } + : null, + })) return } diff --git a/docs/src/examples/components/Toolbar/Usage/ToolbarExampleWithTooltip.shorthand.tsx b/docs/src/examples/components/Toolbar/Usage/ToolbarExampleWithTooltip.shorthand.tsx index 32eb3e0937..10f8d097ff 100644 --- a/docs/src/examples/components/Toolbar/Usage/ToolbarExampleWithTooltip.shorthand.tsx +++ b/docs/src/examples/components/Toolbar/Usage/ToolbarExampleWithTooltip.shorthand.tsx @@ -1,5 +1,4 @@ import * as React from 'react' -import * as _ from 'lodash' import { Toolbar, Tooltip, @@ -58,27 +57,24 @@ const ToolbarExampleShorthand = () => { menuOpen: moreMenuOpen, onMenuOpenChange: (e, { menuOpen }) => setMoreMenuOpen(menuOpen), }, - ].map(item => - _.isNil(item.tooltip) - ? item - : render => - render( - item, - // rendering Tooltip for the Toolbar Item - (ToolbarItem, props) => { - const { tooltip, key, ...rest } = props - // Adding tooltipAsLabelBehavior as the ToolbarItems contains only icon - return ( - } - accessibility={tooltipAsLabelBehavior} - content={tooltip} - /> - ) - }, - ), - )} + ].map(item => ({ + ...item, + // rendering Tooltip for the Toolbar Item + children: item.tooltip + ? (ToolbarItem, props) => { + const { tooltip, key, ...rest } = props + // Adding tooltipAsLabelBehavior as the ToolbarItems contains only icon + return ( + } + accessibility={tooltipAsLabelBehavior} + content={tooltip} + /> + ) + } + : undefined, + }))} /> ) } diff --git a/docs/src/pages/ShorthandProps.mdx b/docs/src/pages/ShorthandProps.mdx index 7a9ae66ddf..f49aa16c2a 100644 --- a/docs/src/pages/ShorthandProps.mdx +++ b/docs/src/pages/ShorthandProps.mdx @@ -56,126 +56,53 @@ It is also possible to pass falsy values (`false`, `null` or ``` -## Function as value +## Using Render Props -Providing function as a shorthand value is the most involving but, at the same time, the most -powerful option for customizing component's slot. The only requirements for this function are: - -- it should finish synchronously -- it should return React Element as a result - -Thus, in its simplest form, it could be used the following way: - -```jsx -