-
Notifications
You must be signed in to change notification settings - Fork 53
feat: 'render' callback for shorthand props #519
Changes from all commits
bca8450
9fe1514
fa9e34c
54a247f
c262702
e5d08b8
f32a87e
f072a59
6529da2
7fd942d
11fbbff
d63bf2c
c4f01bd
27e32e1
3cbef28
71eeab6
2b4d886
971f26e
e36be12
a3399e4
67929d4
8e0e614
f20320f
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 |
---|---|---|
|
@@ -14,11 +14,12 @@ import AccordionTitle from './AccordionTitle' | |
import AccordionContent from './AccordionContent' | ||
import { defaultBehavior } from '../../lib/accessibility' | ||
import { Accessibility } from '../../lib/accessibility/types' | ||
|
||
import { | ||
ComponentEventHandler, | ||
Extendable, | ||
ShorthandRenderFunction, | ||
ShorthandValue, | ||
ShorthandRenderFunction, | ||
} from '../../../types/utils' | ||
|
||
export interface AccordionProps extends UIComponentProps, ChildrenComponentProps { | ||
|
@@ -46,24 +47,20 @@ export interface AccordionProps extends UIComponentProps, ChildrenComponentProps | |
}[] | ||
|
||
/** | ||
* A custom render iterator for rendering each Accordion panel content. | ||
* The default component, props, and children are available for each panel content. | ||
* A custom renderer for each Accordion's panel title. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
* @param {React.ReactType} Component - The panel's component type. | ||
* @param {object} props - The panel's computed props. | ||
*/ | ||
renderContent?: ShorthandRenderFunction | ||
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. In this case, I find this methods useful. They are not callback render for a shorthand, but for each item of array. Do we want to get rid of this as well? It would be nice from user perspective for the array shorthands, to be able to define this logic via an API property. What are your thoughts on this @kuzhelov ? 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. Solution with no additional API changes// this could be a helper util
const customTreeShorthand = (value, renderTree) => (render => render(value, renderTree))
const renderCustomMenuItem = (Component, props) => <Component {...props} />
<Menu items={[
{ as: ..., ... },
...
]
.map(shorthandValue => customTreeShorthand(shorthandValue, renderCustomMenuItem))
}
.. /> Solution with
|
||
renderPanelTitle?: ShorthandRenderFunction | ||
|
||
/** | ||
* A custom render iterator for rendering each Accordion panel title. | ||
* The default component, props, and children are available for each panel title. | ||
* A custom renderer for each Accordion's panel content. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
* @param {React.ReactType} Component - The panel's component type. | ||
* @param {object} props - The panel's computed props. | ||
*/ | ||
renderTitle?: ShorthandRenderFunction | ||
renderPanelContent?: ShorthandRenderFunction | ||
|
||
/** | ||
* Accessibility behavior if overridden by the user. | ||
|
@@ -104,8 +101,9 @@ class Accordion extends AutoControlledComponent<Extendable<AccordionProps>, any> | |
), | ||
]), | ||
accessibility: PropTypes.func, | ||
renderTitle: PropTypes.func, | ||
renderContent: PropTypes.func, | ||
|
||
renderPanelTitle: PropTypes.func, | ||
renderPanelContent: PropTypes.func, | ||
} | ||
|
||
public static defaultProps = { | ||
|
@@ -153,7 +151,7 @@ class Accordion extends AutoControlledComponent<Extendable<AccordionProps>, any> | |
|
||
renderPanels = () => { | ||
const children: any[] = [] | ||
const { panels, renderContent, renderTitle } = this.props | ||
const { panels, renderPanelContent, renderPanelTitle } = this.props | ||
|
||
_.each(panels, (panel, index) => { | ||
const { content, title } = panel | ||
|
@@ -163,13 +161,13 @@ class Accordion extends AutoControlledComponent<Extendable<AccordionProps>, any> | |
AccordionTitle.create(title, { | ||
defaultProps: { active, index }, | ||
overrideProps: this.handleTitleOverrides, | ||
render: renderTitle, | ||
render: renderPanelTitle, | ||
}), | ||
) | ||
children.push( | ||
AccordionContent.create(content, { | ||
defaultProps: { active }, | ||
render: renderContent, | ||
render: renderPanelContent, | ||
}), | ||
) | ||
}) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import * as PropTypes from 'prop-types' | ||
import * as React from 'react' | ||
import * as _ from 'lodash' | ||
import { Extendable, ShorthandValue } from '../../../types/utils' | ||
import { | ||
UIComponent, | ||
customPropTypes, | ||
|
@@ -9,7 +10,6 @@ import { | |
ChildrenComponentProps, | ||
commonPropTypes, | ||
} from '../../lib' | ||
import { Extendable, ShorthandRenderFunction, ShorthandValue } from '../../../types/utils' | ||
import Icon from '../Icon/Icon' | ||
import Button from '../Button/Button' | ||
import Text from '../Text/Text' | ||
|
@@ -33,51 +33,6 @@ export interface AttachmentProps extends UIComponentProps, ChildrenComponentProp | |
|
||
/** Value indicating percent complete. */ | ||
progress?: string | number | ||
|
||
/** | ||
* A custom render function the action slot. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
*/ | ||
renderAction?: ShorthandRenderFunction | ||
|
||
/** | ||
* A custom render function the description slot. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
*/ | ||
renderDescription?: ShorthandRenderFunction | ||
|
||
/** | ||
* A custom render function the header slot. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
*/ | ||
renderHeader?: ShorthandRenderFunction | ||
|
||
/** | ||
* A custom render function the icon slot. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
*/ | ||
renderIcon?: ShorthandRenderFunction | ||
|
||
/** | ||
* A custom render function the progress slot. | ||
* | ||
* @param {React.ReactType} Component - The computed component for this slot. | ||
* @param {object} props - The computed props for this slot. | ||
* @param {ReactNode|ReactNodeArray} children - The computed children for this slot. | ||
*/ | ||
renderProgress?: ShorthandRenderFunction | ||
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. 👍 much cleaner API! |
||
} | ||
|
||
/** | ||
|
@@ -100,62 +55,41 @@ class Attachment extends UIComponent<Extendable<AttachmentProps>, any> { | |
header: customPropTypes.itemShorthand, | ||
icon: customPropTypes.itemShorthand, | ||
progress: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), | ||
renderAction: PropTypes.func, | ||
renderDescription: PropTypes.func, | ||
renderHeader: PropTypes.func, | ||
renderIcon: PropTypes.func, | ||
renderProgress: PropTypes.func, | ||
} | ||
|
||
renderComponent({ ElementType, classes, rest, styles, variables }) { | ||
const { | ||
header, | ||
description, | ||
icon, | ||
action, | ||
progress, | ||
renderIcon, | ||
renderHeader, | ||
renderDescription, | ||
renderAction, | ||
renderProgress, | ||
} = this.props | ||
const { header, description, icon, action, progress } = this.props | ||
|
||
return ( | ||
<ElementType {...rest} className={classes.root}> | ||
{icon && ( | ||
<div className={classes.icon}> | ||
{Icon.create(icon, { | ||
defaultProps: { size: 'big' }, | ||
render: renderIcon, | ||
})} | ||
</div> | ||
)} | ||
{(header || description) && ( | ||
<div className={classes.content}> | ||
{Text.create(header, { | ||
defaultProps: { styles: styles.header }, | ||
render: renderHeader, | ||
})} | ||
|
||
{Text.create(description, { | ||
defaultProps: { styles: styles.description }, | ||
render: renderDescription, | ||
})} | ||
</div> | ||
)} | ||
{action && ( | ||
<div className={classes.action}> | ||
{Button.create(action, { | ||
defaultProps: { iconOnly: true, text: true }, | ||
render: renderAction, | ||
})} | ||
</div> | ||
)} | ||
{!_.isNil(progress) && | ||
Slot.create('', { | ||
defaultProps: { className: classes.progress }, | ||
render: renderProgress, | ||
})} | ||
</ElementType> | ||
) | ||
|
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.
👍