Skip to content

Commit 1005d03

Browse files
committed
Add fixed height components.
1 parent 2b9d1f4 commit 1005d03

File tree

7 files changed

+130
-0
lines changed

7 files changed

+130
-0
lines changed

index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export { createCheckboxComponent } from './react-native/components/createCheckbo
99
export { createCreatableSelectComponent } from './react-native/components/createCreatableSelectComponent'
1010
export { createFileStoreManagerComponent } from './react-native/components/createFileStoreManagerComponent'
1111
export { createFiniteStateMachineRoutingComponent } from './react-native/components/createFiniteStateMachineRoutingComponent'
12+
export { createFixedHeightComponent } from './react-native/components/createFixedHeightComponent'
1213
export { createFixedWidthComponent } from './react-native/components/createFixedWidthComponent'
1314
export { createFlashMessageComponent } from './react-native/components/createFlashMessageComponent'
1415
export { createFlatColorBackgroundComponent } from './react-native/components/createFlatColorBackgroundComponent'
@@ -106,6 +107,7 @@ export type { FileRequestBody } from './react-native/types/FileRequestBody'
106107
export type { FileStoreInterface } from './react-native/types/FileStoreInterface'
107108
export type { FiniteStateMachineRouterState } from './react-native/types/FiniteStateMachineRouterState'
108109
export type { FiniteStateMachineRoutingProps } from './react-native/types/FiniteStateMachineRoutingProps'
110+
export type { FixedHeightProps } from './react-native/types/FixedHeightProps'
109111
export type { FixedWidthProps } from './react-native/types/FixedWidthProps'
110112
export type { FlashMessageProps } from './react-native/types/FlashMessageProps'
111113
export type { FlashMessageState } from './react-native/types/FlashMessageState'
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as React from 'react'
2+
import { StyleSheet, View } from 'react-native'
3+
import type { FixedHeightProps } from '../../types/FixedHeightProps'
4+
5+
/**
6+
* Creates a React component which has a fixed height.
7+
* @param height The height of the component
8+
* @returns The created component.
9+
*/
10+
export const createFixedHeightComponent = (
11+
height: number
12+
): React.FunctionComponent<FixedHeightProps> => {
13+
const styles = StyleSheet.create({
14+
fillsContainer: {
15+
height,
16+
width: '100%'
17+
},
18+
fitsContent: {
19+
height
20+
}
21+
})
22+
23+
const FixedHeight: React.FunctionComponent<FixedHeightProps> = ({ children, width }) => (
24+
<View
25+
style={
26+
width === 'fillsContainer' ? styles.fillsContainer : styles.fitsContent
27+
}
28+
pointerEvents="box-none"
29+
>
30+
{children}
31+
</View>
32+
)
33+
34+
return FixedHeight
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# `react-native-app-helpers/createFixedHeightComponent`
2+
3+
Creates a React component which has a fixed height.
4+
5+
## Usage
6+
7+
```tsx
8+
import { createFixedHeightComponent } from "react-native-app-helpers";
9+
10+
const ExampleComponent = createFixedHeightComponent(243);
11+
12+
const ExampleScreen = () => (
13+
<ExampleComponent width="fillsContainer">
14+
<Text>This is 243 wide and fills its container horizontally.</Text>
15+
</ExampleComponent>
16+
);
17+
```
18+
19+
```tsx
20+
import { createFixedHeightComponent } from "react-native-app-helpers";
21+
22+
const ExampleComponent = createFixedHeightComponent(243);
23+
24+
const ExampleScreen = () => (
25+
<ExampleComponent width="fitsContent">
26+
<Text>This is 243 wide and fits its content horizontally.</Text>
27+
</ExampleComponent>
28+
);
29+
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as React from 'react'
2+
import { Text, View } from 'react-native'
3+
import {
4+
createFixedHeightComponent,
5+
unwrapRenderedFunctionComponent
6+
} from '../../..'
7+
8+
test('renders as expected when filling its container vertically', () => {
9+
const Component = createFixedHeightComponent(243)
10+
11+
const rendered = (
12+
<Component width="fillsContainer">
13+
<Text>Example Content</Text>
14+
</Component>
15+
)
16+
17+
expect(unwrapRenderedFunctionComponent(rendered)).toEqual(
18+
<View style={{ height: 243, width: '100%' }} pointerEvents="box-none">
19+
<Text>Example Content</Text>
20+
</View>
21+
)
22+
})
23+
24+
test('renders as expected when fitting its content vertically', () => {
25+
const Component = createFixedHeightComponent(243)
26+
27+
const rendered = (
28+
<Component width="fitsContent">
29+
<Text>Example Content</Text>
30+
</Component>
31+
)
32+
33+
expect(unwrapRenderedFunctionComponent(rendered)).toEqual(
34+
<View style={{ height: 243 }} pointerEvents="box-none">
35+
<Text>Example Content</Text>
36+
</View>
37+
)
38+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type * as React from 'react'
2+
3+
/**
4+
* Props to be given to fixed height components.
5+
*/
6+
export type FixedHeightProps = React.PropsWithChildren<{
7+
/**
8+
* Describes how the component's widthwidth should be sized.
9+
*/
10+
readonly width: 'fillsContainer' | 'fitsContent'
11+
}>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `react-native-app-helpers/FixedHeightProps`
2+
3+
Props to be given to fixed height components.
4+
5+
## Usage
6+
7+
```tsx
8+
import type { FixedHeightProps } from "react-native-app-helpers";
9+
10+
const example: FixedHeightProps = {
11+
width: "fitsContent",
12+
};
13+
```

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { createTextComponent } from "react-native-app-helpers";
2929
- [createCreatableSelectComponent](./react-native/components/createCreatableSelectComponent/readme.md)
3030
- [createFileStoreManagerComponent](./react-native/components/createFileStoreManagerComponent/readme.md)
3131
- [createFiniteStateMachineRoutingComponent](./react-native/components/createFiniteStateMachineRoutingComponent/readme.md)
32+
- [createFixedHeightComponent](./react-native/components/createFixedHeightComponent/readme.md)
3233
- [createFixedWidthComponent](./react-native/components/createFixedWidthComponent/readme.md)
3334
- [createFlashMessageComponent](./react-native/components/createFlashMessageComponent/readme.md)
3435
- [createFlatColorBackgroundComponent](./react-native/components/createFlatColorBackgroundComponent/readme.md)
@@ -126,6 +127,7 @@ import { createTextComponent } from "react-native-app-helpers";
126127
- [FileRequestBody](./react-native/types/FileRequestBody/readme.md)
127128
- [FiniteStateMachineRouterState](./react-native/types/FiniteStateMachineRouterState/readme.md)
128129
- [FiniteStateMachineRoutingProps](./react-native/types/FiniteStateMachineRoutingProps/readme.md)
130+
- [FixedHeightProps](./react-native/types/FixedHeightProps/readme.md)
129131
- [FixedWidthProps](./react-native/types/FixedWidthProps/readme.md)
130132
- [FlashMessageProps](./react-native/types/FlashMessageProps/readme.md)
131133
- [FlashMessageState](./react-native/types/FlashMessageState/readme.md)

0 commit comments

Comments
 (0)