Skip to content

Commit

Permalink
Make styles overridable to support different font sizes (#57)
Browse files Browse the repository at this point in the history
Needed for observation/app#634

- Make the LargeButton title text style overridable
- Make InputField styles overridable
  • Loading branch information
SjaakSchilperoort authored Dec 6, 2024
1 parent 17b4dc0 commit 1df76aa
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@observation.org/react-native-components",
"version": "1.47.0",
"version": "1.48.0",
"main": "src/index.ts",
"repository": "[email protected]:observation/react-native-components.git",
"author": "Observation.org",
Expand Down
10 changes: 8 additions & 2 deletions src/components/InputField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type Props = {
containerStyle?: StyleProp<ViewStyle>
inputProps?: TextInputProps & RefAttributes<TextInput>
inputStyle?: StyleProp<ViewStyle>
errorStyle?: StyleProp<ViewStyle>
descriptionStyle?: StyleProp<ViewStyle>
label?: string
rightIcon?: JSX.Element
description?: string
Expand All @@ -22,6 +24,8 @@ const InputField = ({
containerStyle,
inputProps,
inputStyle,
errorStyle,
descriptionStyle,
label,
rightIcon,
description,
Expand Down Expand Up @@ -64,12 +68,14 @@ const InputField = ({
icon={<Icon name="exclamation-triangle" size={theme.icon.size.small} color={theme.color.error} />}
text={errorMessage}
style={{
textStyle: styles.errorStyle,
textStyle: [styles.errorStyle, errorStyle],
containerStyle: { marginTop: theme.margin.half },
}}
/>
)}
{description && <Text style={{ marginTop: theme.margin.half, ...styles.descriptionStyle }}>{description}</Text>}
{description && (
<Text style={[{ marginTop: theme.margin.half }, styles.descriptionStyle, descriptionStyle]}>{description}</Text>
)}
</View>
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/LargeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { StyleProp, StyleSheet, Text, TouchableOpacity, View, ViewStyle } from 'react-native'
import { StyleProp, StyleSheet, Text, TextStyle, TouchableOpacity, View, ViewStyle } from 'react-native'

import { Icon } from './Icon'
import { IconName } from '../lib/Icons'
Expand All @@ -11,6 +11,7 @@ import theme from '../styles/theme'

type LargeButtonProps = {
title: string
titleStyle?: StyleProp<TextStyle>
onPress?: () => void
onPressIn?: () => void
iconName?: IconName
Expand Down Expand Up @@ -52,6 +53,7 @@ const LargeButton = ({
danger,
style,
title,
titleStyle,
iconName,
onPress,
onPressIn,
Expand All @@ -74,7 +76,7 @@ const LargeButton = ({
<Icon name={iconName} size={theme.icon.size.large} color={iconColor} />
</View>
)}
<Text style={[styles.title, textStyle]}>{title}</Text>
<Text style={[styles.title, titleStyle, textStyle]}>{title}</Text>
</View>
</TouchableOpacity>
)
Expand Down
14 changes: 14 additions & 0 deletions src/components/__tests__/LargeButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'

import { fireEvent, render } from '@testing-library/react-native'

import appTextStyle from '../../styles/text'
import LargeButton from '../LargeButton'

const onPress = jest.fn()
Expand Down Expand Up @@ -52,6 +53,19 @@ describe('LargeButton', () => {
expect(toJSON()).toMatchSnapshot()
})

test('Rendering, with title style', () => {
const { toJSON } = render(
<LargeButton
iconName="info-circle"
title="Press me"
titleStyle={appTextStyle.subtitle}
onPress={onPress}
style={{ flex: 1 }}
/>,
)
expect(toJSON()).toMatchSnapshot()
})

test('Click', async () => {
const { getByTestId } = render(<LargeButton title="Press me" onPress={onPress} style={{ flex: 1 }} />)
await fireEvent.press(getByTestId('touchable-opacity'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ exports[`BottomSheet Rendering Only buttons 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -313,6 +314,7 @@ exports[`BottomSheet Rendering With button 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down
42 changes: 25 additions & 17 deletions src/components/__tests__/__snapshots__/InputField.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,20 @@ exports[`InputField Rendering With a description 1`] = `
</View>
<Text
style={
{
"color": "#666666",
"fontFamily": "Ubuntu",
"fontSize": 12,
"fontStyle": "normal",
"fontWeight": "normal",
"lineHeight": 16,
"marginTop": 8,
}
[
{
"marginTop": 8,
},
{
"color": "#666666",
"fontFamily": "Ubuntu",
"fontSize": 12,
"fontStyle": "normal",
"fontWeight": "normal",
"lineHeight": 16,
},
undefined,
]
}
>
The description
Expand Down Expand Up @@ -431,14 +436,17 @@ exports[`InputField Rendering With an error message 1`] = `
{
"flexShrink": 1,
},
{
"color": "#EA554B",
"fontFamily": "Ubuntu",
"fontSize": 12,
"fontStyle": "normal",
"fontWeight": "normal",
"lineHeight": 16,
},
[
{
"color": "#EA554B",
"fontFamily": "Ubuntu",
"fontSize": 12,
"fontStyle": "normal",
"fontWeight": "normal",
"lineHeight": 16,
},
undefined,
],
]
}
>
Expand Down
107 changes: 107 additions & 0 deletions src/components/__tests__/__snapshots__/LargeButton.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ exports[`LargeButton Rendering, disabled, primary 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -142,6 +143,7 @@ exports[`LargeButton Rendering, disabled, secondary 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#E6E6E6",
},
Expand Down Expand Up @@ -218,6 +220,7 @@ exports[`LargeButton Rendering, enabled, primary 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -296,6 +299,7 @@ exports[`LargeButton Rendering, enabled, secondary 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#0066B1",
},
Expand Down Expand Up @@ -372,6 +376,7 @@ exports[`LargeButton Rendering, primary, danger 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -450,6 +455,7 @@ exports[`LargeButton Rendering, secondary, danger 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#EA554B",
},
Expand Down Expand Up @@ -526,6 +532,7 @@ exports[`LargeButton Rendering, secondary, danger, disabled => defaults to prima
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -617,6 +624,106 @@ exports[`LargeButton Rendering, with icon 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
]
}
>
Press me
</Text>
</View>
</View>
`;

exports[`LargeButton Rendering, with title style 1`] = `
<View
accessibilityState={
{
"busy": undefined,
"checked": undefined,
"disabled": undefined,
"expanded": undefined,
"selected": undefined,
}
}
accessibilityValue={
{
"max": undefined,
"min": undefined,
"now": undefined,
"text": undefined,
}
}
accessible={true}
collapsable={false}
focusable={true}
onClick={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
{
"alignItems": "center",
"backgroundColor": "#0066B1",
"borderRadius": 4,
"flex": 1,
"height": 32,
"justifyContent": "center",
"margin": 16,
"opacity": 1,
"overflow": "hidden",
}
}
testID="touchable-opacity"
>
<View
style={
{
"flexDirection": "row",
"marginHorizontal": 16,
}
}
>
<View
style={
{
"justifyContent": "center",
"paddingRight": 8,
}
}
>
<Icon
color="#FFFFFF"
name="circle-info"
size={16}
type="light"
/>
</View>
<Text
style={
[
{
"color": "#212121",
"fontFamily": "Ubuntu",
"fontSize": 14,
"fontStyle": "normal",
"fontWeight": "bold",
"lineHeight": 20,
"textAlignVertical": "center",
},
{
"color": "#212121",
"fontFamily": "Ubuntu",
"fontSize": 16,
"fontStyle": "normal",
"fontWeight": "bold",
"lineHeight": 26,
},
{
"color": "#FFFFFF",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ exports[`NotificationPopup Rendering Left and right buttons 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -265,6 +266,7 @@ exports[`NotificationPopup Rendering Left and right buttons 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -467,6 +469,7 @@ exports[`NotificationPopup Rendering Only left button 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -719,6 +722,7 @@ exports[`NotificationPopup Rendering With close button 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down Expand Up @@ -800,6 +804,7 @@ exports[`NotificationPopup Rendering With close button 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ exports[`Tooltip Rendering With button 1`] = `
"lineHeight": 20,
"textAlignVertical": "center",
},
undefined,
{
"color": "#FFFFFF",
},
Expand Down

0 comments on commit 1df76aa

Please sign in to comment.