Skip to content

Commit b1d37da

Browse files
authored
feat: allow custom text transforms in buttons (#112)
* feat: allow custom text transform * fix failing test
1 parent 5dc7c59 commit b1d37da

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ The default handler for overflow menu on iOS is `overflowMenuPressHandlerActionS
218218

219219
One of the usual things you may want to do is override the cancel button label on iOS - see [example](example/screens/UsageWithOverflow.tsx).
220220

221+
#### Using custom text transforms
222+
223+
Use the `buttonStyle` prop to set [`textTransform`](https://reactnative.dev/docs/text-style-props#texttransform) styles for button titles.
224+
221225
#### How to integrate in your project
222226

223227
This sections covers how you should use the library in your project. Please note that there are numerous [example screens](https://github.com/vonovak/react-navigation-header-buttons/tree/master/example/screens).

example/screens/UsageWithIcons.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ const IoniconsHeaderButton = (props) => (
1515
<HeaderButton IconComponent={Ionicons} iconSize={23} color="blue" {...props} />
1616
);
1717

18-
const ReusableSelectItem = ({ onPress }) => <Item title="Edit" onPress={onPress} />;
18+
const ReusableCapitalizedEditItem = ({ onPress }) => (
19+
<Item title="edit" onPress={onPress} buttonStyle={{ textTransform: 'capitalize' }} />
20+
);
1921

2022
const ReusableItem = ({ onPress }) => <HiddenItem title="hidden2" onPress={onPress} />;
2123

@@ -27,7 +29,7 @@ export function UsageWithIcons({ navigation }) {
2729
headerRight: () => (
2830
<HeaderButtons HeaderButtonComponent={IoniconsHeaderButton}>
2931
<Item title="search" iconName="ios-search" onPress={() => alert('search')} />
30-
<ReusableSelectItem onPress={() => alert('Edit')} />
32+
<ReusableCapitalizedEditItem onPress={() => alert('Edit')} />
3133
<OverflowMenu
3234
style={{ marginHorizontal: 10 }}
3335
OverflowIcon={<Ionicons name="ios-more" size={23} color="blue" />}

src/HeaderItems.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,23 @@ export function renderVisibleButton(visibleButtonProps: VisibleButtonProps): Rea
4545
style={StyleSheet.compose(styles.button, buttonStyle)}
4646
/>
4747
) : (
48-
<Text style={[styles.text, { color }, buttonStyle]}>{textTransformer(title)}</Text>
48+
<Text style={[styles.text, { color }, buttonStyle]}>{title}</Text>
4949
);
5050
}
5151

52-
const textTransformer = (label: string) =>
53-
Platform.OS === 'ios' ? label.charAt(0).toUpperCase() + label.substr(1) : label.toUpperCase();
54-
5552
const styles = StyleSheet.create({
5653
text: {
5754
...Platform.select({
5855
android: {
5956
fontFamily: 'sans-serif-medium',
6057
fontSize: 14,
6158
marginHorizontal: 11,
59+
textTransform: 'uppercase',
6260
},
6361
default: {
6462
fontSize: 17,
6563
marginHorizontal: 10,
64+
textTransform: 'capitalize',
6665
},
6766
}),
6867
},

src/__tests__/HeaderButtons.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ describe('HeaderButtons', () => {
1212
it('renders button with pressable labels when Item is a direct or indirect child', () => {
1313
const searchOnPress = jest.fn();
1414
const deleteOnPress = jest.fn();
15-
const WrappedItem = () => <Item title="delete" onPress={deleteOnPress} />;
15+
const WrappedItem = () => <Item title="Delete" onPress={deleteOnPress} />;
1616
const { getByText } = render(
1717
<HeaderButtons>
18-
<Item title="search" onPress={searchOnPress} />
18+
<Item title="Search" onPress={searchOnPress} />
1919
<WrappedItem />
2020
</HeaderButtons>
2121
);

0 commit comments

Comments
 (0)