Skip to content

Commit

Permalink
fix: correct list accordion layout (#3856)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak committed May 15, 2023
1 parent 46d1201 commit a475b09
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 39 deletions.
53 changes: 46 additions & 7 deletions src/components/List/ListAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import * as React from 'react';
import {
GestureResponderEvent,
I18nManager,
NativeSyntheticEvent,
StyleProp,
StyleSheet,
TextStyle,
TextLayoutEventData,
View,
ViewProps,
ViewStyle,
Expand All @@ -16,7 +18,8 @@ import MaterialCommunityIcon from '../MaterialCommunityIcon';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
import { ListAccordionGroupContext } from './ListAccordionGroup';
import { getAccordionColors } from './utils';
import type { Style } from './utils';
import { getAccordionColors, getLeftStyles } from './utils';

export type Props = {
/**
Expand All @@ -30,7 +33,7 @@ export type Props = {
/**
* Callback which returns a React element to display on the left side.
*/
left?: (props: { color: string }) => React.ReactNode;
left?: (props: { color: string; style: Style }) => React.ReactNode;
/**
* Callback which returns a React element to display on the right side.
*/
Expand Down Expand Up @@ -169,6 +172,17 @@ const ListAccordion = ({
const [expanded, setExpanded] = React.useState<boolean>(
expandedProp || false
);
const [alignToTop, setAlignToTop] = React.useState(false);

const onDescriptionTextLayout = (
event: NativeSyntheticEvent<TextLayoutEventData>
) => {
if (!theme.isV3) {
return;
}
const { nativeEvent } = event;
setAlignToTop(nativeEvent.lines.length >= 2);
};

const handlePressAction = (e: GestureResponderEvent) => {
onPress?.(e);
Expand Down Expand Up @@ -206,7 +220,7 @@ const ListAccordion = ({
<View>
<View style={{ backgroundColor: theme?.colors?.background }}>
<TouchableRipple
style={[styles.container, style]}
style={[theme.isV3 ? styles.containerV3 : styles.container, style]}
onPress={handlePress}
onLongPress={onLongPress}
delayLongPress={delayLongPress}
Expand All @@ -218,13 +232,19 @@ const ListAccordion = ({
theme={theme}
borderless
>
<View style={styles.row} pointerEvents={pointerEvents}>
<View
style={theme.isV3 ? styles.rowV3 : styles.row}
pointerEvents={pointerEvents}
>
{left
? left({
color: isExpanded ? theme.colors?.primary : descriptionColor,
style: getLeftStyles(alignToTop, description, theme.isV3),
})
: null}
<View style={[styles.item, styles.content]}>
<View
style={[theme.isV3 ? styles.itemV3 : styles.item, styles.content]}
>
<Text
selectable={false}
numberOfLines={titleNumberOfLines}
Expand All @@ -249,6 +269,7 @@ const ListAccordion = ({
},
descriptionStyle,
]}
onTextLayout={onDescriptionTextLayout}
>
{description}
</Text>
Expand Down Expand Up @@ -283,7 +304,10 @@ const ListAccordion = ({
!child.props.right
) {
return React.cloneElement(child as React.ReactElement<any>, {
style: [styles.child, child.props.style],
style: [
theme.isV3 ? styles.childV3 : styles.child,
child.props.style,
],
theme,
});
}
Expand All @@ -301,10 +325,18 @@ const styles = StyleSheet.create({
container: {
padding: 8,
},
containerV3: {
paddingVertical: 8,
paddingRight: 24,
},
row: {
flexDirection: 'row',
alignItems: 'center',
},
rowV3: {
flexDirection: 'row',
marginVertical: 6,
},
multiline: {
height: 40,
alignItems: 'center',
Expand All @@ -317,11 +349,18 @@ const styles = StyleSheet.create({
fontSize: 14,
},
item: {
margin: 8,
marginVertical: 6,
paddingLeft: 8,
},
itemV3: {
paddingLeft: 16,
},
child: {
paddingLeft: 64,
},
childV3: {
paddingLeft: 40,
},
content: {
flex: 1,
justifyContent: 'center',
Expand Down
10 changes: 1 addition & 9 deletions src/components/List/ListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as React from 'react';
import {
FlexAlignType,
GestureResponderEvent,
NativeSyntheticEvent,
StyleProp,
Expand All @@ -17,7 +16,7 @@ import { useInternalTheme } from '../../core/theming';
import type { $RemoveChildren, EllipsizeProp, ThemeProp } from '../../types';
import TouchableRipple from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
import { getLeftStyles, getRightStyles } from './utils';
import { Style, getLeftStyles, getRightStyles } from './utils';

type Title =
| React.ReactNode
Expand All @@ -37,13 +36,6 @@ type Description =
fontSize: number;
}) => React.ReactNode);

interface Style {
marginLeft?: number;
marginRight?: number;
marginVertical?: number;
alignSelf?: FlexAlignType;
}

export type Props = $RemoveChildren<typeof TouchableRipple> & {
/**
* Title text for the list item.
Expand Down
9 changes: 8 additions & 1 deletion src/components/List/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StyleSheet } from 'react-native';
import { FlexAlignType, StyleSheet } from 'react-native';

import color from 'color';
import type { EllipsizeProp, InternalTheme } from 'src/types';
Expand All @@ -12,6 +12,13 @@ type Description =
fontSize: number;
}) => React.ReactNode);

export type Style = {
marginLeft?: number;
marginRight?: number;
marginVertical?: number;
alignSelf?: FlexAlignType;
};

export const getLeftStyles = (
alignToTop: boolean,
description: Description,
Expand Down
Loading

0 comments on commit a475b09

Please sign in to comment.