Skip to content

Commit e8ada92

Browse files
committed
feat: provide default styles when overriding
1 parent c439cd9 commit e8ada92

28 files changed

Lines changed: 325 additions & 148 deletions

File tree

src/components/Alert/Alert.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as React from 'react';
22
import { TouchableOpacity, View } from 'react-native';
3+
import { DeepPartial } from 'ts-essentials';
34

45
import { Intent } from '../../constants/Intent';
56
import { Icon } from '../../icons';
67
import { Theme, withTheme } from '../../theme';
8+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
79
import { Spacing } from '../Layout';
810
import { Strong, Text } from '../Typography';
9-
import { GetAlertStyles, getAlertStyles } from './Alert.styles';
11+
import { AlertStyles, GetAlertStyles, getAlertStyles } from './Alert.styles';
1012

1113
export interface AlertProps {
1214
theme: Theme;
@@ -19,7 +21,7 @@ export interface AlertProps {
1921
icon?: React.ReactNode | null;
2022
intent?: Intent;
2123

22-
getStyles?: GetAlertStyles;
24+
getStyles?: ReplaceReturnType<GetAlertStyles, DeepPartial<AlertStyles>>;
2325
}
2426

2527
const resolveIcon = (intent: Intent, theme: Theme) => {
@@ -54,11 +56,14 @@ const AlertBase = (props: AlertProps) => {
5456
isCloseable = false,
5557
icon,
5658
intent = 'info',
57-
getStyles = getAlertStyles,
59+
getStyles,
5860
theme,
5961
} = props;
6062

61-
const { containerStyle, bodyStyle } = getStyles({ intent }, theme);
63+
const { containerStyle, bodyStyle } = mergeStyles(getAlertStyles, getStyles)(
64+
{ intent },
65+
theme,
66+
);
6267

6368
return (
6469
<View style={containerStyle}>

src/components/Avatar/Avatar.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import * as React from 'react';
22
import { Image, View } from 'react-native';
3+
import { DeepPartial } from 'ts-essentials';
34

45
import { Theme, withTheme } from '../../theme';
56
import { FillColors } from '../../theme/ThemeInterface';
7+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
68
import { Text } from '../Typography';
79
import { getTextStyles } from '../Typography/Text.styles';
8-
import { GetAvatarStyles, getAvatarStyles } from './Avatar.styles';
10+
import {
11+
AvatarStyles,
12+
GetAvatarStyles,
13+
getAvatarStyles,
14+
} from './Avatar.styles';
915

1016
// https://github.com/segmentio/evergreen/blob/master/source/avatar/README.md
1117
export type GetInitialsType = (name?: string, fallback?: string) => string;
@@ -77,7 +83,7 @@ export interface AvatarProps {
7783
*/
7884
theme: Theme;
7985

80-
getStyles?: GetAvatarStyles;
86+
getStyles?: ReplaceReturnType<GetAvatarStyles, DeepPartial<AvatarStyles>>;
8187
}
8288

8389
export const AvatarBase = (props: AvatarProps) => {
@@ -93,7 +99,7 @@ export const AvatarBase = (props: AvatarProps) => {
9399
color = 'automatic',
94100
forceShowInitials = false,
95101
sizeLimitOneCharacter = 20,
96-
getStyles = getAvatarStyles,
102+
getStyles,
97103
} = props;
98104

99105
const { imageHasFailedLoading } = { imageHasFailedLoading: false };
@@ -104,7 +110,10 @@ export const AvatarBase = (props: AvatarProps) => {
104110
initials = initials.substring(0, 1);
105111
}
106112

107-
const { boxStyle, textStyle, imageStyle } = getStyles(
113+
const { boxStyle, textStyle, imageStyle } = mergeStyles(
114+
getAvatarStyles,
115+
getStyles,
116+
)(
108117
{
109118
color,
110119
hashValue,

src/components/Badge/Badge.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import * as React from 'react';
2+
import { DeepPartial } from 'ts-essentials';
23

34
import { Theme, withTheme } from '../../theme';
45
import { FillColor } from '../../theme/ThemeInterface';
6+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
57
import Box, { Shape } from '../Layout/Box';
68
import { Strong } from '../Typography';
79
import { getTextStyles } from '../Typography/Text.styles';
8-
import { BadgeSize, GetBadgeStyles, getBadgeStyles } from './Badge.styles';
10+
import {
11+
BadgeSize,
12+
BadgeStyles,
13+
GetBadgeStyles,
14+
getBadgeStyles,
15+
} from './Badge.styles';
916

1017
export interface BadgeProps {
1118
children: React.ReactNode;
@@ -14,21 +21,24 @@ export interface BadgeProps {
1421
size?: BadgeSize;
1522
shape?: Shape;
1623
isSolid?: boolean;
17-
getStyles?: GetBadgeStyles;
24+
getStyles?: ReplaceReturnType<GetBadgeStyles, DeepPartial<BadgeStyles>>;
1825
}
1926

2027
const BadgeBase = (props: BadgeProps) => {
2128
const {
2229
children,
2330
color = 'neutral',
24-
getStyles = getBadgeStyles,
31+
getStyles,
2532
isSolid = false,
2633
shape = 'rounded',
2734
size = 'small',
2835
theme,
2936
} = props;
3037

31-
const { boxStyle, textStyle } = getStyles({ size, color, isSolid }, theme);
38+
const { boxStyle, textStyle } = mergeStyles(getBadgeStyles, getStyles)(
39+
{ size, color, isSolid },
40+
theme,
41+
);
3242

3343
return (
3444
<Box

src/components/Button/Button.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
TouchableHighlightProps,
55
View,
66
} from 'react-native';
7+
import { DeepPartial } from 'ts-essentials';
78

89
import { Theme, withTheme } from '../../theme';
10+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
911
import { Spacing } from '../Layout';
1012
import { LoadingDots } from '../Loading';
1113
import { Text } from '../Typography';
@@ -14,6 +16,7 @@ import {
1416
ButtonAppearance,
1517
ButtonColor,
1618
ButtonSize,
19+
ButtonStyles,
1720
GetButtonStyles,
1821
getButtonStyles,
1922
} from './Button.styles';
@@ -95,15 +98,15 @@ export interface ButtonProps extends TouchableHighlightProps {
9598
/**
9699
* Inline styles for components
97100
*/
98-
getStyles?: GetButtonStyles;
101+
getStyles?: ReplaceReturnType<GetButtonStyles, DeepPartial<ButtonStyles>>;
99102
}
100103

101104
const ButtonBase = (props: ButtonProps) => {
102105
const {
103106
appearance = 'primary',
104107
title,
105108
color = 'default',
106-
getStyles = getButtonStyles,
109+
getStyles,
107110
iconAfter,
108111
iconBefore,
109112
iconLoading,
@@ -120,7 +123,10 @@ const ButtonBase = (props: ButtonProps) => {
120123
...touchableHighlightProps
121124
} = props;
122125

123-
const { buttonStyle, textStyle, focusColor } = getStyles(
126+
const { buttonStyle, textStyle, focusColor } = mergeStyles(
127+
getButtonStyles,
128+
getStyles,
129+
)(
124130
{
125131
appearance,
126132
color,

src/components/Button/ButtonGroup.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as React from 'react';
33
import { Theme, withTheme } from '../../theme';
44
import { Box } from '../Layout';
55
import { ButtonProps } from './Button';
6-
import { ButtonStylesProps, getButtonStyles } from './Button.styles';
76

87
export type ButtonGroupDirection = 'vertical' | 'horizontal';
98

@@ -37,13 +36,9 @@ const ButtonGroup: React.SFC<ButtonGroupProps> = props => {
3736
const buttonBorderRadius = theme.controlBorderRadius[buttonSize];
3837

3938
return React.cloneElement(button, {
40-
getStyles: (styleProps: ButtonStylesProps) => {
41-
const defaultButtonStyles = getButtonStyles(styleProps, theme);
42-
39+
getStyles: () => {
4340
return {
44-
...defaultButtonStyles,
4541
buttonStyle: {
46-
...defaultButtonStyles.buttonStyle,
4742
...(direction === 'vertical'
4843
? {
4944
borderBottomWidth: 1,

src/components/Checkbox/Checkbox.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import {
55
TouchableHighlightProps,
66
View,
77
} from 'react-native';
8+
import { DeepPartial } from 'ts-essentials';
89

910
import { Icon } from '../../icons';
1011
import { Theme, withTheme } from '../../theme';
11-
import { GetCheckboxStyles, getCheckboxStyles } from './Checkbox.styles';
12+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
13+
import {
14+
CheckboxStyles,
15+
GetCheckboxStyles,
16+
getCheckboxStyles,
17+
} from './Checkbox.styles';
1218

1319
export type CheckboxShape = 'circle' | 'square';
1420

@@ -22,7 +28,7 @@ export interface CheckboxProps {
2228
/** @default square */
2329
shape?: CheckboxShape;
2430
onChange?: (e: GestureResponderEvent) => void | undefined;
25-
getStyles?: GetCheckboxStyles;
31+
getStyles?: ReplaceReturnType<GetCheckboxStyles, DeepPartial<CheckboxStyles>>;
2632
}
2733

2834
const CheckboxBase = (props: CheckboxProps & TouchableHighlightProps) => {
@@ -34,14 +40,14 @@ const CheckboxBase = (props: CheckboxProps & TouchableHighlightProps) => {
3440
onChange = () => null,
3541
shape = 'square',
3642
theme,
37-
getStyles = getCheckboxStyles,
43+
getStyles,
3844
...touchableHighlightProps
3945
} = props;
4046

41-
const { checkboxStyle, checkboxFocusBackgroundColor } = getStyles(
42-
{ isChecked, isDisabled, shape },
43-
theme,
44-
);
47+
const { checkboxStyle, checkboxFocusBackgroundColor } = mergeStyles(
48+
getCheckboxStyles,
49+
getStyles,
50+
)({ isChecked, isDisabled, shape }, theme);
4551

4652
return (
4753
<TouchableHighlight

src/components/Counter/Counter.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import * as React from 'react';
22
import { TouchableOpacity, View } from 'react-native';
3+
import { DeepPartial } from 'ts-essentials';
34

45
import { Icon } from '../../icons';
56
import { Theme, withTheme } from '../../theme';
7+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
68
import { Spacing } from '../Layout';
7-
import { GetCounterStyles, getCounterStyles } from './Counter.styles';
9+
import {
10+
CounterStyles,
11+
GetCounterStyles,
12+
getCounterStyles,
13+
} from './Counter.styles';
814

915
export interface CounterProps {
1016
theme: Theme;
@@ -18,7 +24,7 @@ export interface CounterProps {
1824
/**
1925
* Inline styles for components
2026
*/
21-
getStyles?: GetCounterStyles;
27+
getStyles?: ReplaceReturnType<GetCounterStyles, DeepPartial<CounterStyles>>;
2228
}
2329

2430
const CounterBase = (props: CounterProps) => {
@@ -29,13 +35,16 @@ const CounterBase = (props: CounterProps) => {
2935
min,
3036
onIncrement,
3137
onDecrement,
32-
getStyles = getCounterStyles,
38+
getStyles,
3339
theme,
3440
} = props;
3541

36-
const { containerStyle, counterStyle, countStyle, disabledStyle } = getStyles(
37-
theme,
38-
);
42+
const {
43+
containerStyle,
44+
counterStyle,
45+
countStyle,
46+
disabledStyle,
47+
} = mergeStyles(getCounterStyles, getStyles)(theme);
3948

4049
const isDecrementDisabled = min === count;
4150
const isIncrementDisabled = max === count;

src/components/Dialog/Dialog.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import * as React from 'react';
22
import { View } from 'react-native';
3+
import { DeepPartial } from 'ts-essentials';
34

45
import { Theme, withTheme } from '../../theme';
6+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
57
import { Modal } from '../Modal';
68
import { Overlay } from '../Overlay';
7-
import { GetDialogStyles, getDialogStyles } from './Dialog.styles';
9+
import {
10+
DialogStyles,
11+
GetDialogStyles,
12+
getDialogStyles,
13+
} from './Dialog.styles';
814

915
// TODO: Import from react-native when react-native-web implementation is ready
1016

@@ -22,7 +28,7 @@ export interface DialogProps {
2228
/**
2329
* Inline styles for components
2430
*/
25-
getStyles?: GetDialogStyles;
31+
getStyles?: ReplaceReturnType<GetDialogStyles, DeepPartial<DialogStyles>>;
2632
}
2733

2834
const DialogBase = (props: DialogProps) => {
@@ -33,10 +39,13 @@ const DialogBase = (props: DialogProps) => {
3339
isVisible,
3440
onClose = () => null,
3541
theme,
36-
getStyles = getDialogStyles,
42+
getStyles,
3743
} = props;
3844

39-
const { modalContainerStyle, containerStyle, bodyStyle } = getStyles(theme);
45+
const { modalContainerStyle, containerStyle, bodyStyle } = mergeStyles(
46+
getDialogStyles,
47+
getStyles,
48+
)(theme);
4049

4150
return (
4251
<Modal visible={isVisible} transparent onRequestClose={onClose}>

src/components/Divider/Divider.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import * as React from 'react';
22
import { View } from 'react-native';
3+
import { DeepPartial } from 'ts-essentials';
34

45
import { Theme, withTheme } from '../../theme';
5-
import { GetDividerStyles, getDividerStyles } from './Divider.styles';
6+
import { mergeStyles, ReplaceReturnType } from '../../utils/mergeStyles';
7+
import {
8+
DividerStyles,
9+
GetDividerStyles,
10+
getDividerStyles,
11+
} from './Divider.styles';
612

713
export interface DividerProps {
814
theme: Theme;
915
size?: number;
1016
color?: string;
1117
radius?: number;
12-
getStyles?: GetDividerStyles;
18+
getStyles?: ReplaceReturnType<GetDividerStyles, DeepPartial<DividerStyles>>;
1319
}
1420

1521
const DividerBase = (props: DividerProps) => {
16-
const { theme, size, color, radius, getStyles = getDividerStyles } = props;
17-
const { dividerStyle } = getStyles({ size, color, radius }, theme);
22+
const { theme, size, color, radius, getStyles } = props;
23+
const { dividerStyle } = mergeStyles(getDividerStyles, getStyles)(
24+
{ size, color, radius },
25+
theme,
26+
);
1827

1928
return <View style={dividerStyle} />;
2029
};

0 commit comments

Comments
 (0)