Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose new props to add custom color in Chip component #729

Merged
merged 1 commit into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion example/src/Examples/ChipExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ class ChipExample extends React.Component<Props> {
>
<List.Section title="Flat chip">
<View style={styles.row}>
<Chip onPress={() => {}} style={styles.chip}>
<Chip
mode="outlined"
selected
onPress={() => {}}
style={styles.chip}
>
Simple
</Chip>
<Chip onPress={() => {}} onClose={() => {}} style={styles.chip}>
Expand Down Expand Up @@ -137,6 +142,42 @@ class ChipExample extends React.Component<Props> {
</Chip>
</View>
</List.Section>
<List.Section title="Custom chip">
<View style={styles.row}>
<Chip
selected
onPress={() => {}}
style={[styles.chip, { backgroundColor: 'rgba(128,0,128,0.2)' }]}
selectedColor="#800080"
>
Flat selected chip with custom color
</Chip>
<Chip
onPress={() => {}}
style={styles.chip}
selectedColor="#800080"
>
Flat unselected chip with custom color
</Chip>
<Chip
selected
mode="outlined"
onPress={() => {}}
style={[styles.chip, { backgroundColor: 'rgba(128,0,128, 0.2)' }]}
selectedColor="#800080"
>
Outlined selected chip with custom color
</Chip>
<Chip
mode="outlined"
onPress={() => {}}
style={styles.chip}
selectedColor="#800080"
>
Outlined unselected chip with custom color
</Chip>
</View>
</List.Section>
</ScrollView>
);
}
Expand Down
26 changes: 21 additions & 5 deletions src/components/Chip.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ type Props = React.ElementConfig<typeof Surface> & {|
*/
avatar?: React.Node,
/**
* Whether to style the chip as selected.
* Whether chip is selected.
*/
selected?: boolean,
/**
* Whether to style the chip color as selected.
*/
selectedColor?: string,
/**
* Whether the chip is disabled. A disabled chip is greyed out and `onPress` is not called on touch.
*/
Expand Down Expand Up @@ -137,6 +141,7 @@ class Chip extends React.Component<Props, State> {
style,
theme,
testID,
selectedColor,
...rest
} = this.props;
const { dark, colors } = theme;
Expand All @@ -151,20 +156,24 @@ class Chip extends React.Component<Props, State> {

const borderColor =
mode === 'outlined'
? color(dark ? white : black)
? color(
selectedColor !== undefined
? selectedColor
: color(dark ? white : black)
)
.alpha(0.29)
.rgb()
.string()
: backgroundColor;
const textColor = disabled
? colors.disabled
: color(colors.text)
: color(selectedColor !== undefined ? selectedColor : colors.text)
.alpha(0.87)
.rgb()
.string();
const iconColor = disabled
? colors.disabled
: color(colors.text)
: color(selectedColor !== undefined ? selectedColor : colors.text)
.alpha(0.54)
.rgb()
.string();
Expand All @@ -175,6 +184,13 @@ class Chip extends React.Component<Props, State> {
.rgb()
.string();

const underlayColor = selectedColor
? color(selectedColor)
.fade(0.5)
.rgb()
.string()
: selectedBackgroundColor;

const accessibilityTraits = ['button'];
const accessibilityStates = [];

Expand Down Expand Up @@ -210,7 +226,7 @@ class Chip extends React.Component<Props, State> {
onPress={onPress}
onPressIn={this._handlePressIn}
onPressOut={this._handlePressOut}
underlayColor={selectedBackgroundColor}
underlayColor={underlayColor}
disabled={disabled}
accessibilityLabel={accessibilityLabel}
accessibilityTraits={accessibilityTraits}
Expand Down