Skip to content

Commit 8050d48

Browse files
feat: implemented suggestion chip
1 parent c127575 commit 8050d48

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

src/chips/assist-chip/AssistChip.component.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import {type IconProps} from '../../icons/icon-props';
55
import {getColorStyles, styles} from './assist-chip.styles';
66
import {BaseChip, type BaseChipProps} from '../base-chip/BaseChip.component';
77

8+
export enum LeadingIconType {
9+
COMMON = 'COMMON',
10+
FAVICON = 'FAVICON',
11+
BRANDED = 'BRANDED',
12+
}
13+
814
export interface AssistChipProps<T extends IconProps> extends BaseChipProps {
915
elevated?: boolean;
1016
LeadingIcon?: React.FC<T>;
1117
leadingIconType?: LeadingIconType;
1218
leadingIconProps?: T;
1319
}
1420

15-
export enum LeadingIconType {
16-
COMMON = 'COMMON',
17-
FAVICON = 'FAVICON',
18-
BRANDED = 'BRANDED',
19-
}
20-
2121
export const AssistChip = <T extends IconProps>({
2222
elevated = false,
2323
LeadingIcon,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React, {useMemo} from 'react';
2+
3+
import {useTheme} from '../../theme/useTheme.hook';
4+
import {type IconProps} from '../../icons/icon-props';
5+
import {getDynamicStyles, styles} from './suggestion-chip.styles';
6+
import {BaseChip, type BaseChipProps} from '../base-chip/BaseChip.component';
7+
8+
export interface SuggestionChipProps<T extends IconProps> extends BaseChipProps {
9+
elevated?: boolean;
10+
selected?: boolean;
11+
LeadingIcon?: React.FC<T>;
12+
leadingIconProps?: T;
13+
}
14+
15+
export const SuggestionChip = <T extends IconProps>({
16+
elevated = false,
17+
selected = false,
18+
LeadingIcon,
19+
leadingIconProps = {} as T,
20+
style,
21+
labelStyle,
22+
disabled = false,
23+
...props
24+
}: SuggestionChipProps<T>) => {
25+
const theme = useTheme();
26+
const dynamicStyles = useMemo(() => getDynamicStyles(selected, elevated, disabled, theme), [disabled, elevated, selected, theme]);
27+
28+
return (
29+
<BaseChip
30+
style={[elevated && !disabled ? styles.elevatedContainer : styles.outlinedContainer, dynamicStyles.container, style]}
31+
labelStyle={[dynamicStyles.label, labelStyle]}
32+
leadingIcon={LeadingIcon ? <LeadingIcon size={18} color={dynamicStyles.icon.color} {...leadingIconProps} /> : null}
33+
{...props}
34+
/>
35+
);
36+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {StyleSheet} from 'react-native';
2+
3+
import {type Theme} from '../../theme/theme.types';
4+
import {convertToRGBA} from '../../utils/convert-to-rgba';
5+
6+
export const styles = StyleSheet.create({
7+
outlinedContainer: {
8+
borderWidth: 1,
9+
},
10+
elevatedContainer: {
11+
shadowOffset: {
12+
width: 0,
13+
height: 1,
14+
},
15+
shadowOpacity: 0.15,
16+
shadowRadius: 3,
17+
elevation: 1,
18+
},
19+
});
20+
21+
export const getDynamicStyles = (selected: boolean, elevated: boolean, disabled: boolean, theme: Theme) => {
22+
const labelDisabledColor = convertToRGBA(theme.surface.text as string, 0.38);
23+
const containerDisabledColor = convertToRGBA(theme.surface.textVariant as string, 0.12);
24+
25+
const [disabledUnselectedBackgroundColorByType, disabledUnselectedBorderColorByType] = elevated
26+
? [containerDisabledColor, 'transparent']
27+
: ['transparent', containerDisabledColor];
28+
const enabledBackgroundColorByType = elevated ? theme.surfaceContainer.backgroundLow : 'transparent';
29+
30+
return StyleSheet.create(
31+
disabled
32+
? {
33+
label: {
34+
color: labelDisabledColor,
35+
},
36+
container: {
37+
borderColor: selected ? 'transparent' : disabledUnselectedBorderColorByType,
38+
backgroundColor: selected ? containerDisabledColor : disabledUnselectedBackgroundColorByType,
39+
},
40+
icon: {
41+
color: labelDisabledColor,
42+
},
43+
}
44+
: {
45+
label: {
46+
color: theme.surface.text,
47+
},
48+
container: {
49+
borderColor: theme.outline,
50+
borderWidth: Number(!selected && !elevated),
51+
backgroundColor: selected ? theme.secondaryContainer.background : enabledBackgroundColorByType,
52+
},
53+
icon: {
54+
color: theme.primary.background,
55+
},
56+
}
57+
);
58+
};

src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export {Badge, type BadgeProps} from './badge/Badge.component';
55
export {Divider, type DividerProps} from './divider/Divider.component';
66

77
export {InputChip, type InputChipProps} from './chips/input-chip/InputChip.component';
8-
export {AssistChip, type AssistChipProps} from './chips/assist-chip/AssistChip.component';
8+
export {SuggestionChip, type SuggestionChipProps} from './chips/suggestion-chip/SuggestionChip.component';
9+
export {AssistChip, type AssistChipProps, LeadingIconType} from './chips/assist-chip/AssistChip.component';
910

1011
export {
1112
CenterAlignedTopAppBar,

0 commit comments

Comments
 (0)