This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
[WIP] feat: add compose(), caching, converting components to hooks #2229
Draft
layershifter
wants to merge
4
commits into
master
Choose a base branch
from
feat/compose
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0521e5a
feat: add compose
layershifter 372d5b9
fix cruft after rebase
layershifter b8bf865
Merge branches 'feat/compose' and 'master' of https://github.com/star…
layershifter 3f66203
Merge branches 'feat/compose' and 'master' of https://github.com/star…
layershifter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
docs/src/examples/components/Slider/Types/SliderExample.shorthand.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import * as React from 'react' | ||
import { Slider } from '@fluentui/react' | ||
|
||
const SliderExampleShorthand = () => <Slider /> | ||
const SliderExampleShorthand = () => ( | ||
<> | ||
<Slider /> | ||
<Slider /> | ||
</> | ||
) | ||
|
||
export default SliderExampleShorthand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as React from 'react' | ||
|
||
type ComposeOptions = { | ||
// TODO: better typings PLZ | ||
className?: string | ||
displayName: string | ||
mapPropsToBehavior?: Function | ||
mapPropsToStyles?: Function | ||
handledProps?: string[] | ||
overrideStyles?: boolean | ||
} | ||
|
||
const COMPOSE_CONFIG_PROP_NAME = '__unstable_config' | ||
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. Should this go in the We need a package which partners can easily use to compose components and provide a theme, with the least amount of other dependencies in it. This will allow a solid foundation to exist for building out an ecosystem of components. 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 don't think that composition covers only styling aspects 🤔 |
||
|
||
export type ComposableProps = { [COMPOSE_CONFIG_PROP_NAME]?: ComposeOptions } | ||
|
||
export const compose = <UserProps, CProps = {}>( | ||
Component: React.ComponentType<CProps>, | ||
options: ComposeOptions, | ||
): React.ComponentType<CProps & UserProps> => { | ||
const ComposedComponent = Component.bind(null) | ||
|
||
ComposedComponent.displayName = options.displayName | ||
|
||
// We are passing config via props by setting default prop value | ||
ComposedComponent.defaultProps = { ...(Component.defaultProps || {}) } | ||
// @ts-ignore TODO PLS FIX ME | ||
ComposedComponent.defaultProps[COMPOSE_CONFIG_PROP_NAME] = options | ||
|
||
return ComposedComponent as any | ||
} | ||
|
||
export const useComposedConfig = <P extends ComposableProps>(props: P) => { | ||
const { [COMPOSE_CONFIG_PROP_NAME]: options } = props | ||
|
||
const { | ||
className = '', | ||
displayName, | ||
handledProps = [], | ||
mapPropsToBehavior = () => ({}), | ||
mapPropsToStyles = () => ({}), | ||
overrideStyles = false, | ||
} = options || {} | ||
|
||
return { | ||
behaviorProps: mapPropsToBehavior(props), | ||
styleProps: mapPropsToStyles(props), | ||
className, | ||
displayName, | ||
handledProps: handledProps.concat(['__unstable_config']), | ||
overrideStyles, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { compose } from '@fluentui/react-bindings' | ||
|
||
import { createShorthandFactory } from '../../utils' | ||
import Image, { ImageProps } from '../Image/Image' | ||
|
||
export interface AvatarImageProps extends ImageProps {} | ||
|
||
const AvatarImage = compose(Image, { | ||
displayName: 'AvatarImage', | ||
}) | ||
|
||
// @ts-ignore | ||
AvatarImage.create = createShorthandFactory({ | ||
// @ts-ignore | ||
Component: AvatarImage, | ||
mappedProp: 'src', | ||
}) | ||
|
||
export default AvatarImage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { compose } from '@fluentui/react-bindings' | ||
|
||
import { createShorthandFactory, SizeValue } from '../../utils' | ||
import Box, { BoxProps } from '../Box/Box' | ||
|
||
export interface AvatarLabelProps extends BoxProps { | ||
size?: SizeValue | ||
} | ||
|
||
const AvatarLabel = compose(Box, { | ||
displayName: 'AvatarLabel', | ||
mapPropsToStyles: props => ({ size: props.size }), | ||
}) | ||
|
||
// @ts-ignore | ||
AvatarLabel.create = createShorthandFactory({ | ||
// @ts-ignore | ||
Component: AvatarLabel, | ||
mappedProp: 'content', | ||
}) | ||
|
||
export default AvatarLabel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { compose } from '@fluentui/react-bindings' | ||
|
||
import { createShorthandFactory } from '../../utils' | ||
import Status, { StatusProps } from '../Status/Status' | ||
|
||
export interface AvatarStatusProps extends StatusProps {} | ||
|
||
const AvatarStatus = compose(Status, { | ||
displayName: 'AvatarStatus', | ||
}) | ||
|
||
// @ts-ignore | ||
AvatarStatus.create = createShorthandFactory({ | ||
// @ts-ignore | ||
Component: AvatarStatus, | ||
mappedProp: 'state', | ||
}) | ||
|
||
export default AvatarStatus |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Revert me plz