Skip to content

Commit ce72155

Browse files
authored
refactor: replace React.Children usage with context and typed props (#5076)
1 parent 4ffc20a commit ce72155

24 files changed

Lines changed: 600 additions & 291 deletions

‎docs/6.x/docs/guides/migration.md‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ When the dialog is `dismissable`, screen reader users can dismiss it with a visu
251251
- The default elevation changed from level `1` to level `3`.
252252
- The `style` prop no longer configures the background color or border radius. You can override `theme.colors.surfaceContainerHigh` and `theme.shapes.corner.extraLarge` using the `theme` prop instead.
253253
254+
#### `Dialog.Actions`
255+
256+
`Dialog.Actions` no longer injects `compact` on its buttons. To keep the previous behavior, you need to add the `compact` prop to each button:
257+
258+
```tsx
259+
// Before (v5)
260+
<Dialog.Actions>
261+
<Button onPress={hide}>Done</Button>
262+
</Dialog.Actions>
263+
264+
// After (v6)
265+
<Dialog.Actions>
266+
<Button compact onPress={hide}>
267+
Done
268+
</Button>
269+
</Dialog.Actions>
270+
```
271+
254272
### Menu
255273
256274
The `overlayAccessibilityLabel` prop was renamed to `dismissAccessibilityLabel`:
@@ -410,6 +428,124 @@ const theme = {
410428
/>
411429
```
412430

431+
### Card
432+
433+
#### `Card` layout
434+
435+
`Card` and its related components have been reworked. Instead of injecting padding to its children, the `Card` component now adds a padding (`16dp`) around its content as well as a gap (`16dp`) between its children.
436+
437+
For the related components:
438+
439+
- `Card.Cover` and `Card.Actions` now apply negative margins to extend into the Card's edges:
440+
- `Card.Cover` applies top, left and right margins for vertical cards and left, top, and bottom margins for horizontal cards.
441+
- `Card.Actions` applies bottom, left, and right margins for vertical cards and left, bottom, and right margins for horizontal cards.
442+
- `Card.Title` and `Card.Content` no longer apply padding around them.
443+
444+
This means, in a typical card layout, the Card will automatically apply the necessary spacing between these sections:
445+
446+
```tsx
447+
<Card>
448+
<Card.Cover source={cover} />
449+
<Card.Title title="Card title" />
450+
<Card.Content>
451+
<Text>Card content</Text>
452+
</Card.Content>
453+
<Card.Actions>
454+
<Button mode="contained">Action</Button>
455+
<Button mode="outlined">Another Action</Button>
456+
</Card.Actions>
457+
</Card>
458+
```
459+
460+
So your existing layouts with the following structure will continue to work as before:
461+
462+
- `Card` without `Card.Cover` or `Card.Actions`.
463+
- `Card` with `Card.Cover` at the top and/or `Card.Actions` at the bottom.
464+
465+
If you have `Card.Cover` or `Card.Actions` in positions other than the top and bottom respectively, you will need to adjust their margins. For example, for a `Card.Cover` in the middle of the Card, you would need to set its top and bottom margins to zero:
466+
467+
```tsx
468+
<Card>
469+
<Card.Title title="Card title" />
470+
<Card.Cover source={cover} style={{ marginTop: 0, marginBottom: 0 }} />
471+
<Card.Content>
472+
<Text>Card content</Text>
473+
</Card.Content>
474+
</Card>
475+
```
476+
477+
We have also added a `direction` prop to the `Card` component, which lets you arrange its items horizontally instead of the default vertical layout. So if you have custom `flexDirection` styles on the Card, you should replace them with the `direction` prop.
478+
479+
```tsx
480+
// Before (v5)
481+
<Card style={{ flexDirection: 'row' }}>
482+
<Card.Cover source={cover} style={{ width: 72, height: 72 }} />
483+
<Card.Title title="Card title" />
484+
<Card.Content>
485+
<Text>Card content</Text>
486+
</Card.Content>
487+
<Card.Actions>
488+
<Button mode="contained">Action</Button>
489+
<Button mode="outlined">Another Action</Button>
490+
</Card.Actions>
491+
</Card>
492+
493+
// After (v6)
494+
<Card direction="horizontal">
495+
<Card.Cover source={cover} style={{ width: 72, height: 72 }} />
496+
<Card.Title title="Card title" />
497+
<Card.Content>
498+
<Text>Card content</Text>
499+
</Card.Content>
500+
<Card.Actions>
501+
<Button mode="contained">Action</Button>
502+
<Button mode="outlined">Another Action</Button>
503+
</Card.Actions>
504+
</Card>
505+
```
506+
507+
#### `Card.Actions`
508+
509+
`Card.Actions` no longer injects `mode` on the buttons. To keep the previous behavior, you need to set `mode="outlined"` on the first button and `mode="contained"` on the rest:
510+
511+
```tsx
512+
// Before (v5)
513+
<Card.Actions>
514+
<Button>Cancel</Button>
515+
<Button>Ok</Button>
516+
</Card.Actions>
517+
518+
// After (v6)
519+
<Card.Actions>
520+
<Button mode="outlined">Cancel</Button>
521+
<Button mode="contained">Ok</Button>
522+
</Card.Actions>
523+
```
524+
525+
### List
526+
527+
#### `List.Accordion`
528+
529+
An accordion with a `left` element now indents only its `List.Item` children. Anything else you put inside keeps its own padding, so indent it yourself to line it up with the items.
530+
531+
```tsx
532+
// Before (v5)
533+
<List.Accordion title="Group" left={props => <List.Icon {...props} icon="folder" />}>
534+
<View>
535+
<Text>Custom row</Text>
536+
</View>
537+
</List.Accordion>
538+
539+
// After (v6)
540+
<List.Accordion title="Group" left={props => <List.Icon {...props} icon="folder" />}>
541+
<View style={{ paddingLeft: 40 }}>
542+
<Text>Custom row</Text>
543+
</View>
544+
</List.Accordion>
545+
```
546+
547+
`theme` set on `List.Accordion` no longer reaches its children either. Pass it to the child that needs the override.
548+
413549
### ToggleButton
414550

415551
`ToggleButton`, `ToggleButton.Group` and `ToggleButton.Row` were removed. For an

‎example/src/Examples/CardExample.tsx‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ const CardExample = () => {
7474
<Card style={styles.card} mode={selectedMode}>
7575
<Card.Cover source={require('../../assets/images/forest.jpg')} />
7676
<Card.Actions>
77-
<Button onPress={() => {}}>Share</Button>
78-
<Button onPress={() => {}}>Explore</Button>
77+
<Button mode="outlined" onPress={() => {}}>
78+
Share
79+
</Button>
80+
<Button mode="contained" onPress={() => {}}>
81+
Explore
82+
</Button>
7983
</Card.Actions>
8084
</Card>
8185
<Card style={styles.card} mode={selectedMode}>
@@ -104,12 +108,14 @@ const CardExample = () => {
104108
<Card.Title title="Custom Button styles" />
105109
<Card.Actions>
106110
<Button
111+
mode="outlined"
107112
theme={{ shapes: { corner: { largeIncreased: 12 } } }}
108113
onPress={() => {}}
109114
>
110115
Share
111116
</Button>
112117
<Button
118+
mode="contained"
113119
theme={{ shapes: { corner: { largeIncreased: 12 } } }}
114120
onPress={() => {}}
115121
>
@@ -128,7 +134,7 @@ const CardExample = () => {
128134
/>
129135
<Card.Cover
130136
source={require('../../assets/images/artist-2.jpg')}
131-
style={styles.customCoverRadius}
137+
style={[styles.customCoverRadius, styles.bottomCover]}
132138
/>
133139
</Card>
134140
<Card style={styles.card} mode={selectedMode}>
@@ -233,6 +239,10 @@ const styles = StyleSheet.create({
233239
borderTopRightRadius: 0,
234240
borderBottomRightRadius: 24,
235241
},
242+
bottomCover: {
243+
marginTop: 0,
244+
marginBottom: -16,
245+
},
236246
});
237247

238248
export default CardExample;

‎example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectRealCase.tsx‎

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ const SegmentedButtonMultiselectRealCase = () => {
5959
contentContainerStyle={styles.contentContainer}
6060
renderItem={({ item }) => {
6161
return (
62-
<Card mode="contained" style={styles.card}>
63-
<Card.Content style={styles.content}>
64-
<Card.Cover style={styles.cover} source={item.cover} />
65-
<Card.Title
66-
title={item.name}
67-
subtitle={'$'.repeat(item.price)}
68-
titleVariant="titleMedium"
69-
style={styles.title}
70-
right={() => <IconButton icon={'bookmark-outline'} />}
71-
/>
72-
</Card.Content>
62+
<Card mode="contained" direction="horizontal" style={styles.card}>
63+
<Card.Cover style={styles.cover} source={item.cover} />
64+
<Card.Title
65+
title={item.name}
66+
subtitle={'$'.repeat(item.price)}
67+
titleVariant="titleMedium"
68+
style={styles.title}
69+
right={() => <IconButton icon={'bookmark-outline'} />}
70+
/>
7371
</Card>
7472
);
7573
}}
@@ -90,12 +88,6 @@ const styles = StyleSheet.create({
9088
marginHorizontal: 16,
9189
marginTop: 16,
9290
},
93-
content: {
94-
flexDirection: 'row',
95-
alignItems: 'center',
96-
paddingHorizontal: 0,
97-
paddingVertical: 0,
98-
},
9991
cover: {
10092
width: 72,
10193
height: 72,

‎example/src/Examples/SegmentedButtons/SegmentedButtonRealCase.tsx‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,15 @@ const SegmentedButtonRealCase = () => {
3737
contentContainerStyle={styles.contentContainer}
3838
renderItem={({ item }) => {
3939
return (
40-
<Card mode="contained" style={styles.card}>
41-
<Card.Content style={styles.content}>
42-
<Card.Cover style={styles.cover} source={item.cover} />
43-
<Card.Title
44-
title={item.title}
45-
subtitle={item.artist}
46-
titleVariant="titleMedium"
47-
style={styles.title}
48-
right={() => <IconButton icon={'bookmark-outline'} />}
49-
/>
50-
</Card.Content>
40+
<Card mode="contained" direction="horizontal" style={styles.card}>
41+
<Card.Cover style={styles.cover} source={item.cover} />
42+
<Card.Title
43+
title={item.title}
44+
subtitle={item.artist}
45+
titleVariant="titleMedium"
46+
style={styles.title}
47+
right={() => <IconButton icon={'bookmark-outline'} />}
48+
/>
5149
</Card>
5250
);
5351
}}
@@ -68,15 +66,9 @@ const styles = StyleSheet.create({
6866
marginHorizontal: 16,
6967
marginTop: 16,
7068
},
71-
content: {
72-
flexDirection: 'row',
73-
alignItems: 'center',
74-
paddingHorizontal: 0,
75-
paddingVertical: 0,
76-
},
7769
cover: {
78-
width: 72,
79-
height: 72,
70+
width: 84,
71+
height: 84,
8072
},
8173
title: {
8274
flexShrink: 1,

‎example/src/Examples/TeamDetails.tsx‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ const News = () => {
9393
</Text>
9494
</Card.Content>
9595
<Card.Actions>
96-
<Button onPress={() => {}}>Share</Button>
97-
<Button onPress={() => {}}>Read more</Button>
96+
<Button mode="outlined" onPress={() => {}}>
97+
Share
98+
</Button>
99+
<Button mode="contained" onPress={() => {}}>
100+
Read more
101+
</Button>
98102
</Card.Actions>
99103
</Card>
100104
<Card style={styles.card} mode="contained">
@@ -110,8 +114,12 @@ const News = () => {
110114
</Text>
111115
</Card.Content>
112116
<Card.Actions>
113-
<Button onPress={() => {}}>Share</Button>
114-
<Button onPress={() => {}}>Read more</Button>
117+
<Button mode="outlined" onPress={() => {}}>
118+
Share
119+
</Button>
120+
<Button mode="contained" onPress={() => {}}>
121+
Read more
122+
</Button>
115123
</Card.Actions>
116124
</Card>
117125
</View>

0 commit comments

Comments
 (0)