Skip to content

Commit 6a96792

Browse files
committed
feat: button/icon on per element specification
1 parent a7c3fa2 commit 6a96792

7 files changed

Lines changed: 57 additions & 31 deletions

File tree

src/components/KeyboardToolbar/compound/components/Done.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ import { useCallback, useMemo } from "react";
33
import { StyleSheet, Text } from "react-native";
44

55
import { KeyboardController } from "../../../../module";
6+
import Button from "../../Button";
67
import { TEST_ID_KEYBOARD_TOOLBAR_DONE } from "../../constants";
78
import { useToolbarContext } from "../context";
89

910
import type { ButtonSubProps } from "./types";
1011
import type { ReactNode } from "react";
1112
import type { GestureResponderEvent } from "react-native";
1213

13-
const Done: React.FC<ButtonSubProps & { text?: ReactNode }> = ({
14+
const Done: React.FC<Omit<ButtonSubProps, "icon"> & { text?: ReactNode }> = ({
1415
children,
1516
onPress,
1617
rippleRadius = 28,
1718
text,
19+
button: ButtonContainer = Button,
1820
}) => {
1921
const context = useToolbarContext();
20-
const { theme, colorScheme, buttonContainer: ButtonContainer } = context;
22+
const { theme, colorScheme } = context;
2123

2224
const doneStyle = useMemo(
2325
() => [styles.doneButton, { color: theme[colorScheme].primary }],

src/components/KeyboardToolbar/compound/components/Next.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React, { useCallback } from "react";
22

33
import { KeyboardController } from "../../../../module";
4+
import Arrow from "../../Arrow";
5+
import Button from "../../Button";
46
import { TEST_ID_KEYBOARD_TOOLBAR_NEXT } from "../../constants";
57
import { useToolbarContext } from "../context";
68

@@ -13,14 +15,11 @@ const Next: React.FC<ButtonSubProps> = ({
1315
disabled,
1416
rippleRadius,
1517
style,
18+
button: ButtonContainer = Button,
19+
icon: IconContainer = Arrow,
1620
}) => {
1721
const context = useToolbarContext();
18-
const {
19-
theme,
20-
buttonContainer: ButtonContainer,
21-
iconContainer: IconContainer,
22-
isNextDisabled,
23-
} = context;
22+
const { theme, isNextDisabled } = context;
2423

2524
const isDisabled = disabled ?? isNextDisabled;
2625

src/components/KeyboardToolbar/compound/components/Prev.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from "react";
22
import { useCallback } from "react";
33

44
import { KeyboardController } from "../../../../module";
5+
import Arrow from "../../Arrow";
6+
import Button from "../../Button";
57
import { TEST_ID_KEYBOARD_TOOLBAR_PREVIOUS } from "../../constants";
68
import { useToolbarContext } from "../context";
79

@@ -14,14 +16,11 @@ const Prev: React.FC<ButtonSubProps> = ({
1416
disabled,
1517
rippleRadius,
1618
style,
19+
button: ButtonContainer = Button,
20+
icon: IconContainer = Arrow,
1721
}) => {
1822
const context = useToolbarContext();
19-
const {
20-
theme,
21-
buttonContainer: ButtonContainer,
22-
iconContainer: IconContainer,
23-
isPrevDisabled,
24-
} = context;
23+
const { theme, isPrevDisabled } = context;
2524

2625
const isDisabled = disabled ?? isPrevDisabled;
2726

src/components/KeyboardToolbar/compound/components/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type Arrow from "../../Arrow";
2+
import type Button from "../../Button";
13
import type { ReactNode } from "react";
24
import type { GestureResponderEvent, ViewStyle } from "react-native";
35

@@ -8,4 +10,6 @@ export type ButtonSubProps = {
810
testID?: string;
911
rippleRadius?: number;
1012
style?: ViewStyle;
13+
button?: typeof Button;
14+
icon?: typeof Arrow;
1115
};

src/components/KeyboardToolbar/compound/context.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { createContext, useContext } from "react";
22

3-
import type Arrow from "../Arrow";
4-
import type Button from "../Button";
53
import type { KeyboardToolbarTheme } from "../types";
64

75
type ToolbarContextType = {
86
theme: KeyboardToolbarTheme;
97
colorScheme: "light" | "dark";
10-
buttonContainer: typeof Button;
11-
iconContainer: typeof Arrow;
128
isPrevDisabled: boolean;
139
isNextDisabled: boolean;
1410
};

src/components/KeyboardToolbar/index.tsx

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
6767
});
6868
const isPrevDisabled = inputs.current === 0;
6969
const isNextDisabled = inputs.current === inputs.count - 1;
70+
const buttonContainer = button ?? Button;
71+
const iconContainer = icon ?? Arrow;
7072

7173
useEffect(() => {
7274
const subscription = FocusedInputEvents.addListener("focusDidSet", (e) => {
@@ -110,7 +112,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
110112
[closed, opened],
111113
);
112114

113-
let blurElement: ReactNode = null;
115+
let backgroundElement: ReactNode = null;
114116
let arrowsElement: ReactNode = null;
115117
let contentContainer: ReactNode = null;
116118
let doneElement: ReactNode = null;
@@ -120,7 +122,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
120122
let nextChild: ReactNode = null;
121123
let contentChild: ReactNode = null;
122124
let doneChild: ReactNode = null;
123-
let blurChild: ReactNode = null;
125+
let backgroundChild: ReactNode = null;
124126

125127
React.Children.forEach(children, (child) => {
126128
if (!React.isValidElement(child)) {
@@ -129,7 +131,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
129131
const type = child.type;
130132

131133
if (type === Background) {
132-
blurChild = child;
134+
backgroundChild = child;
133135
} else if (type === Content) {
134136
contentChild = child;
135137
} else if (type === Prev) {
@@ -141,7 +143,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
141143
}
142144
});
143145

144-
blurElement = blurChild;
146+
backgroundElement = backgroundChild;
145147
doneElement = doneChild;
146148
arrowsElement =
147149
prevChild || nextChild ? (
@@ -152,25 +154,31 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
152154
) : null;
153155
contentContainer = contentChild ?? <Content>{contentChild}</Content>;
154156
} else {
155-
blurElement = blur;
157+
backgroundElement = blur;
156158
arrowsElement = showArrows ? (
157159
<View style={styles.arrows}>
158-
<Prev onPress={onPrevCallback} />
159-
<Next onPress={onNextCallback} />
160+
<Prev
161+
button={buttonContainer}
162+
icon={iconContainer}
163+
onPress={onPrevCallback}
164+
/>
165+
<Next
166+
button={buttonContainer}
167+
icon={iconContainer}
168+
onPress={onNextCallback}
169+
/>
160170
</View>
161171
) : null;
162172
contentContainer = <Content>{content}</Content>;
163173
doneElement = doneText ? (
164-
<Done text={doneText} onPress={onDoneCallback} />
174+
<Done button={buttonContainer} text={doneText} onPress={onDoneCallback} />
165175
) : null;
166176
}
167177

168178
const contextValue = useMemo(
169179
() => ({
170180
theme,
171181
colorScheme,
172-
buttonContainer: button ?? Button,
173-
iconContainer: icon ?? Arrow,
174182
isPrevDisabled,
175183
isNextDisabled,
176184
}),
@@ -185,7 +193,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> & {
185193
style={containerStyle}
186194
>
187195
<View {...rest} style={toolbarStyle} testID={TEST_ID_KEYBOARD_TOOLBAR}>
188-
{blurElement}
196+
{backgroundElement}
189197
{arrowsElement}
190198
{contentContainer}
191199
{doneElement}

src/components/KeyboardToolbar/types.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,27 @@ export type KeyboardToolbarProps = Omit<
7070
* @deprecated Use compound API with `<KeyboardToolbar.Done />` component and `text` prop instead.
7171
*/
7272
doneText?: ReactNode;
73-
/** Custom touchable component for toolbar (used for prev/next/done buttons). */
73+
/**
74+
* Custom touchable component for toolbar (used for prev/next/done buttons).
75+
*
76+
* @deprecated Use `button` property for corresponding element instead:
77+
* ```tsx
78+
* <KeyboardToolbar>
79+
* <KeyboardToolbar.Prev button={MyCustomButton} />
80+
* </KeyboardToolbar>
81+
* ```.
82+
*/
7483
button?: typeof Button;
75-
/** Custom icon component used to display next/prev buttons. */
84+
/**
85+
* Custom icon component used to display next/prev buttons.
86+
*
87+
* @deprecated Use `icon` property for corresponding element instead:
88+
* ```tsx
89+
* <KeyboardToolbar>
90+
* <KeyboardToolbar.Prev icon={MyCustomIcon} />
91+
* </KeyboardToolbar>
92+
* ```.
93+
*/
7694
icon?: typeof Arrow;
7795
/**
7896
* Whether to show next and previous buttons. Can be useful to set it to `false` if you have only one input

0 commit comments

Comments
 (0)