Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This component allows transforming a string representation of React components before they are written to output. It's not a new functionality, previously it was exposed via undocumented `unstable__transformChildren` prop in <Box> and <Text> components. I think it has been proven to be stable and there are definitely use cases for it, so it's time to let anyone leverage this feature. Now deprecated `unstable_transformChildren` prop is still going to be available and work as before to maintain backwards compatibility. However, it will be removed in future major versions.
- Loading branch information
Vadim Demedes
committed
Apr 26, 2020
1 parent
4aefaa0
commit 9ed46a5
Showing
12 changed files
with
175 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -134,7 +134,8 @@ | |
"error", | ||
{ | ||
"allow": [ | ||
"^unstable__" | ||
"^unstable__", | ||
"^internal_" | ||
] | ||
} | ||
], | ||
|
This file contains 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 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 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 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 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,25 @@ | ||
import React, {FC, ReactNode} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export interface TransformProps { | ||
readonly transform: (children: ReactNode) => ReactNode; | ||
readonly children: ReactNode; | ||
} | ||
|
||
/* | ||
* Transform a string representation of React components before they are written to output. | ||
*/ | ||
export const Transform: FC<TransformProps> = ({children, transform}) => ( | ||
<span | ||
style={{flexDirection: 'row'}} | ||
// @ts-ignore | ||
internal_transform={transform} | ||
> | ||
{children} | ||
</span> | ||
); | ||
|
||
Transform.propTypes = { | ||
transform: PropTypes.func.isRequired, | ||
children: PropTypes.node.isRequired | ||
}; |
This file contains 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 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 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 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 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 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,65 @@ | ||
import React from 'react'; | ||
import test from 'ava'; | ||
import {renderToString} from './helpers/render-to-string'; | ||
import {Box, Text} from '../src'; | ||
|
||
test('transform children of <Box>', t => { | ||
const output = renderToString( | ||
<Box unstable__transformChildren={(string: string) => `[${string}]`}> | ||
<Box unstable__transformChildren={(string: string) => `{${string}}`}> | ||
test | ||
</Box> | ||
</Box> | ||
); | ||
|
||
t.is(output, '[{test}]'); | ||
}); | ||
|
||
test('transform children of <Text>', t => { | ||
const output = renderToString( | ||
<Text unstable__transformChildren={(string: string) => `[${string}]`}> | ||
<Text unstable__transformChildren={(string: string) => `{${string}}`}> | ||
test | ||
</Text> | ||
</Text> | ||
); | ||
|
||
t.is(output, '[{test}]'); | ||
}); | ||
|
||
test('squash multiple text nodes', t => { | ||
const output = renderToString( | ||
<Box unstable__transformChildren={(string: string) => `[${string}]`}> | ||
<Box unstable__transformChildren={(string: string) => `{${string}}`}> | ||
hello world | ||
</Box> | ||
</Box> | ||
); | ||
|
||
t.is(output, '[{hello world}]'); | ||
}); | ||
|
||
test('squash multiple nested text nodes', t => { | ||
const output = renderToString( | ||
<Box unstable__transformChildren={(string: string) => `[${string}]`}> | ||
<Box unstable__transformChildren={(string: string) => `{${string}}`}> | ||
hello | ||
<Text> world</Text> | ||
</Box> | ||
</Box> | ||
); | ||
|
||
t.is(output, '[{hello world}]'); | ||
}); | ||
|
||
test('squash empty `<Text>` nodes', t => { | ||
const output = renderToString( | ||
<Box unstable__transformChildren={(string: string) => `[${string}]`}> | ||
<Box unstable__transformChildren={(string: string) => `{${string}}`}> | ||
<Text>{[]}</Text> | ||
</Box> | ||
</Box> | ||
); | ||
|
||
t.is(output, ''); | ||
}); |