Skip to content

Commit 22de99e

Browse files
committed
fix: isleading prop behaviour
1 parent dd68f60 commit 22de99e

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/components/Appbar/Appbar.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getAppbarBackgroundColor,
1919
modeAppbarHeight,
2020
renderAppbarContent,
21+
filterAppbarActions,
2122
} from './utils';
2223
import { useInternalTheme } from '../../core/theming';
2324
import type { MD3Elevation, ThemeProp } from '../../types';
@@ -228,15 +229,6 @@ const Appbar = ({
228229
shouldAddRightSpacing = shouldCenterContent && rightItemsCount === 0;
229230
}
230231

231-
const filterAppbarActions = React.useCallback(
232-
(isLeading = false) =>
233-
React.Children.toArray(children).filter((child) =>
234-
// @ts-expect-error: TypeScript complains about the type of type but it doesn't matter
235-
isLeading ? child.props.isLeading : !child.props.isLeading
236-
),
237-
[children]
238-
);
239-
240232
const spacingStyle = isV3 ? styles.v3Spacing : styles.spacing;
241233

242234
const insets = {
@@ -264,7 +256,11 @@ const Appbar = ({
264256
{shouldAddLeftSpacing ? <View style={spacingStyle} /> : null}
265257
{(!isV3 || isMode('small') || isMode('center-aligned')) &&
266258
renderAppbarContent({
267-
children,
259+
children: [
260+
// Filter appbar actions - first leading icons, then trailing icons
261+
...filterAppbarActions(children, true),
262+
...filterAppbarActions(children),
263+
],
268264
isDark,
269265
theme,
270266
isV3,
@@ -288,7 +284,7 @@ const Appbar = ({
288284
mode,
289285
})}
290286
{renderAppbarContent({
291-
children: filterAppbarActions(true),
287+
children: filterAppbarActions(children, true),
292288
isDark,
293289
isV3,
294290
renderOnly: ['Appbar.Action'],
@@ -297,7 +293,7 @@ const Appbar = ({
297293
{/* Right side of row container, can contain other AppbarAction if they are not leading icons */}
298294
<View style={styles.rightActionControls}>
299295
{renderAppbarContent({
300-
children: filterAppbarActions(false),
296+
children: filterAppbarActions(children),
301297
isDark,
302298
isV3,
303299
renderExcept: [
@@ -310,7 +306,6 @@ const Appbar = ({
310306
})}
311307
</View>
312308
</View>
313-
{/* Middle of the row, can contain only AppbarContent */}
314309
{renderAppbarContent({
315310
children,
316311
isDark,

src/components/Appbar/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,22 @@ export const modeTextVariant = {
113113
'center-aligned': 'titleLarge',
114114
} as const;
115115

116+
/**
117+
* Filtruje akcje w Appbarze na podstawie właściwości isLeading.
118+
* @param children - Dzieci komponentu Appbar do przefiltrowania
119+
* @param isLeading - Czy filtrować akcje wiodące (true) czy niewiodące (false). Domyślnie false.
120+
* @returns Przefiltrowana tablica elementów React
121+
*/
122+
export const filterAppbarActions = (
123+
children: React.ReactNode,
124+
isLeading = false
125+
) => {
126+
return React.Children.toArray(children).filter((child) => {
127+
if (!React.isValidElement(child)) return false;
128+
return isLeading ? child.props.isLeading : !child.props.isLeading;
129+
});
130+
};
131+
116132
export const renderAppbarContent = ({
117133
children,
118134
isDark,

0 commit comments

Comments
 (0)