-
Notifications
You must be signed in to change notification settings - Fork 734
Feat/incubator dialog support size and custom header #1846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
M-i-k-e-l
wants to merge
5
commits into
master
from
feat/incubator-dialog-support-size-and-custom-header
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0fc6664
FadedScrollView make more generic
M-i-k-e-l 104ce95
Move FadedScrollView
M-i-k-e-l b791af8
Merge branch 'master' into infra/generify-faded-scroll-view
M-i-k-e-l 6fab20d
Incubator.Dialog - support width, height and customHeader
M-i-k-e-l 2bcbed1
Merge branch 'master' into feat/incubator-dialog-support-size-and-cus…
M-i-k-e-l 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
10 changes: 10 additions & 0 deletions
10
generatedTypes/src/incubator/Dialog/helpers/useSizeStyle.d.ts
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,10 @@ | ||
import { StyleProp, ViewStyle } from 'react-native'; | ||
import { StyleProps } from '../types'; | ||
export declare function getSizeFromPercentage(size: string, totalSize: number): number | undefined; | ||
export declare function getSizeAsNumber(isWidth: boolean, size?: string | number): number | undefined; | ||
export declare function getSize(isWidth: boolean, defaultSize?: number, propSize?: string | number, style?: StyleProp<ViewStyle>): number | undefined; | ||
declare const useSizeStyle: (props: StyleProps) => { | ||
width: number | undefined; | ||
height: number | undefined; | ||
}; | ||
export default useSizeStyle; |
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,109 @@ | ||
import * as uut from '../helpers/useSizeStyle'; | ||
|
||
describe('Dialog:useSizeStyle', () => { | ||
describe('getSizeFromPercentage', () => { | ||
it('should handle 100%', () => { | ||
expect(uut.getSizeFromPercentage('100%', 500)).toEqual(500); | ||
}); | ||
|
||
it('should handle two digits', () => { | ||
expect(uut.getSizeFromPercentage('40%', 200)).toEqual(80); | ||
}); | ||
|
||
it('should handle one digit', () => { | ||
expect(uut.getSizeFromPercentage('5%', 360)).toEqual(18); | ||
}); | ||
}); | ||
|
||
describe('getSizeAsNumber', () => { | ||
it('width | undefined', () => { | ||
expect(uut.getSizeAsNumber(true, undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('width | number', () => { | ||
expect(uut.getSizeAsNumber(true, 38)).toEqual(38); | ||
}); | ||
|
||
it('width | string', () => { | ||
expect(uut.getSizeAsNumber(true, '30%')).toEqual(225); | ||
}); | ||
|
||
it('height | undefined', () => { | ||
expect(uut.getSizeAsNumber(false, undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('height | number', () => { | ||
expect(uut.getSizeAsNumber(false, 38)).toEqual(38); | ||
}); | ||
|
||
it('height | string', () => { | ||
expect(uut.getSizeAsNumber(false, '30%')).toEqual(400.2); | ||
}); | ||
}); | ||
|
||
describe('getSize', () => { | ||
it('width | no default | no width | no style', () => { | ||
expect(uut.getSize(true, undefined, undefined, {backgroundColor: 'red'})).toEqual(undefined); | ||
}); | ||
|
||
it('width | default | no width | no style', () => { | ||
expect(uut.getSize(true, 250, undefined, {backgroundColor: 'red'})).toEqual(250); | ||
}); | ||
|
||
it('width | default | no width | no style (has height)', () => { | ||
expect(uut.getSize(true, 250, undefined, {backgroundColor: 'red', height: 40})).toEqual(250); | ||
}); | ||
|
||
it('width | default | width | no style', () => { | ||
expect(uut.getSize(true, 250, 30, {backgroundColor: 'red'})).toEqual(30); | ||
}); | ||
|
||
it('width | default | width | style', () => { | ||
expect(uut.getSize(true, 250, 30, {backgroundColor: 'red', width: 15})).toEqual(30); | ||
}); | ||
|
||
it('width | default | width (percentage) | style', () => { | ||
expect(uut.getSize(true, 250, '10%', {backgroundColor: 'red', width: 15})).toEqual(75); | ||
}); | ||
|
||
it('width | default | no width | style', () => { | ||
expect(uut.getSize(true, 250, undefined, {backgroundColor: 'red', width: 15})).toEqual(15); | ||
}); | ||
|
||
it('width | default | no width | style (percentage)', () => { | ||
expect(uut.getSize(true, 250, undefined, {backgroundColor: 'red', width: '15%'})).toEqual(112.5); | ||
}); | ||
|
||
it('height | no default | no height | no style', () => { | ||
expect(uut.getSize(false, undefined, undefined, {backgroundColor: 'red'})).toEqual(undefined); | ||
}); | ||
|
||
it('height | default | no height | no style', () => { | ||
expect(uut.getSize(false, 250, undefined, {backgroundColor: 'red'})).toEqual(250); | ||
}); | ||
|
||
it('height | default | no height | no style (has width)', () => { | ||
expect(uut.getSize(false, 250, undefined, {backgroundColor: 'red', width: 40})).toEqual(250); | ||
}); | ||
|
||
it('height | default | height | no style', () => { | ||
expect(uut.getSize(false, 250, 30, {backgroundColor: 'red'})).toEqual(30); | ||
}); | ||
|
||
it('height | default | height (percentage) | style', () => { | ||
expect(uut.getSize(false, 250, '10%', {backgroundColor: 'red', height: 15})).toEqual(133.4); | ||
}); | ||
|
||
it('height | default | height | style', () => { | ||
expect(uut.getSize(false, 250, 30, {backgroundColor: 'red', height: 15})).toEqual(30); | ||
}); | ||
|
||
it('height | default | no height | style', () => { | ||
expect(uut.getSize(false, 250, undefined, {backgroundColor: 'red', height: 15})).toEqual(15); | ||
}); | ||
|
||
it('height | default | no height | style (percentage)', () => { | ||
expect(uut.getSize(false, 250, undefined, {backgroundColor: 'red', height: '15%'})).toEqual(200.1); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
import {isUndefined} from 'lodash'; | ||
import {useMemo} from 'react'; | ||
import {StyleSheet, StyleProp, ViewStyle} from 'react-native'; | ||
import {Constants} from '../../../commons/new'; | ||
import {StyleProps} from '../types'; | ||
|
||
const PERCENTAGE_STRING_REGEX = '([0-9]*)%'; | ||
// exporting for tests | ||
export function getSizeFromPercentage(size: string, totalSize: number) { | ||
const matches = size.match(PERCENTAGE_STRING_REGEX); | ||
if (matches && matches.length > 0) { | ||
return (Number(matches[1]) * totalSize) / 100; | ||
} | ||
} | ||
|
||
// exporting for tests | ||
export function getSizeAsNumber(isWidth: boolean, size?: string | number) { | ||
if (size) { | ||
if (typeof size === 'number') { | ||
return size; | ||
} else { | ||
const result = getSizeFromPercentage(size, isWidth ? Constants.screenWidth : Constants.screenHeight); | ||
if (result) { | ||
return result; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// exporting for tests | ||
export function getSize(isWidth: boolean, | ||
defaultSize?: number, | ||
propSize?: string | number, | ||
style?: StyleProp<ViewStyle>) { | ||
const size = getSizeAsNumber(isWidth, propSize); | ||
if (!isUndefined(size)) { | ||
return size; | ||
} | ||
|
||
if (style) { | ||
const flattenedStyle = StyleSheet.flatten(style); | ||
const size = getSizeAsNumber(isWidth, isWidth ? flattenedStyle.width : flattenedStyle.height); | ||
if (!isUndefined(size)) { | ||
return size; | ||
} | ||
} | ||
|
||
return defaultSize; | ||
} | ||
|
||
const useSizeStyle = (props: StyleProps) => { | ||
const {width: propsWidth, height: propsHeight, containerStyle} = props; | ||
|
||
const width = useMemo(() => { | ||
return getSize(true, 250, propsWidth, containerStyle); | ||
}, [propsWidth, containerStyle]); | ||
|
||
const height = useMemo(() => { | ||
return getSize(false, undefined, propsHeight, containerStyle); | ||
}, [propsHeight, containerStyle]); | ||
|
||
return {width, height}; | ||
}; | ||
|
||
export default useSizeStyle; |
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.
Is this hook meant to convert string percentages to numbers?
Cause react-native already supports string percentages
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.
This is what I've talked with you about; RN does have this functionality, but it does not work because of the different views in the Dialog, I did not find a way to not break other things and have this work 😞
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.
Let's go over it together maybe we can find a solution