-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* setup flex gap * add types * throw error when gap is in percents * add changeset * fix tests
- Loading branch information
Showing
10 changed files
with
164 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
'@react-pdf/layout': minor | ||
'@react-pdf/renderer': minor | ||
'@react-pdf/stylesheet': minor | ||
'@react-pdf/types': minor | ||
--- | ||
|
||
implement flex gap |
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,46 @@ | ||
import Yoga from '@react-pdf/yoga'; | ||
import { isNil, matchPercent } from '@react-pdf/fns'; | ||
|
||
const checkPercents = (attr, value) => { | ||
const percent = matchPercent(value); | ||
|
||
if (percent) { | ||
throw new Error(`You can't pass percentage values to ${attr} property`); | ||
} | ||
}; | ||
|
||
/** | ||
* Set rowGap value to node's Yoga instance | ||
* | ||
* @param {Number} gap value | ||
* @param {Object} node instance | ||
* @return {Object} node instance | ||
*/ | ||
export const setRowGap = value => node => { | ||
const { yogaNode } = node; | ||
|
||
if (!isNil(value) && yogaNode) { | ||
checkPercents('rowGap', value); | ||
yogaNode.setGap(Yoga.GUTTER_ROW, value); | ||
} | ||
|
||
return node; | ||
}; | ||
|
||
/** | ||
* Set columnGap value to node's Yoga instance | ||
* | ||
* @param {Number} gap value | ||
* @param {Object} node instance | ||
* @return {Object} node instance | ||
*/ | ||
export const setColumnGap = value => node => { | ||
const { yogaNode } = node; | ||
|
||
if (!isNil(value) && yogaNode) { | ||
checkPercents('columnGap', value); | ||
yogaNode.setGap(Yoga.GUTTER_COLUMN, value); | ||
} | ||
|
||
return node; | ||
}; |
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,91 @@ | ||
/* eslint-disable react/no-array-index-key */ | ||
import React from 'react'; | ||
import { Document, Page, View } from '..'; | ||
import renderToImage from './renderComponent'; | ||
|
||
const mount = async children => { | ||
const image = await renderToImage( | ||
<Document> | ||
<Page size={[100, 100]}>{children}</Page> | ||
</Document>, | ||
); | ||
|
||
return image; | ||
}; | ||
|
||
const items = [ | ||
'red', | ||
'red', | ||
'red', | ||
'green', | ||
'green', | ||
'green', | ||
'blue', | ||
'blue', | ||
'blue', | ||
]; | ||
|
||
describe('flex', () => { | ||
test('should support gap', async () => { | ||
const image = await mount( | ||
<View | ||
style={{ | ||
height: '100%', | ||
width: '100%', | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
backgroundColor: '#e2e2e2', | ||
gap: 30, | ||
}} | ||
> | ||
{items.map((color, index) => ( | ||
<View | ||
key={index} | ||
style={{ | ||
width: 10, | ||
height: 10, | ||
backgroundColor: color, | ||
}} | ||
/> | ||
))} | ||
</View>, | ||
); | ||
|
||
expect(image).toMatchImageSnapshot(); | ||
}); | ||
|
||
test('should support rowGap and columnGap', async () => { | ||
const image = await mount( | ||
<View | ||
style={{ | ||
height: '100%', | ||
width: '100%', | ||
display: 'flex', | ||
flexWrap: 'wrap', | ||
backgroundColor: '#e2e2e2', | ||
rowGap: '60px', | ||
columnGap: '80px', | ||
}} | ||
> | ||
{items.slice(0, 4).map((color, index) => ( | ||
<View | ||
key={index} | ||
style={{ | ||
width: 10, | ||
height: 10, | ||
backgroundColor: color, | ||
}} | ||
/> | ||
))} | ||
</View>, | ||
); | ||
|
||
expect(image).toMatchImageSnapshot(); | ||
}); | ||
|
||
test('should throw when value is percent', async () => { | ||
expect(mount(<View style={{ gap: '10%' }} />)).rejects.toThrow( | ||
"You can't pass percentage values to columnGap property", | ||
); | ||
}); | ||
}); |
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
Binary file added
BIN
+381 Bytes
packages/renderer/tests/snapshots/gap-test-js-flex-should-support-gap-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+366 Bytes
...sts/snapshots/gap-test-js-flex-should-support-row-gap-and-column-gap-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,10 @@ | ||
const expandGap = (key, value) => { | ||
const match = `${value}`.split(' '); | ||
|
||
return { | ||
rowGap: match?.[0] || value, | ||
columnGap: match?.[1] || value, | ||
}; | ||
}; | ||
|
||
export default expandGap; |
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