Skip to content

checkbox variant - outline #1067

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

Merged
merged 3 commits into from
Dec 15, 2020
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
4 changes: 4 additions & 0 deletions generatedTypes/components/checkbox/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export interface CheckboxProps extends TouchableOpacityProps {
* The Checkbox color
*/
color?: string;
/**
* alternative Checkbox outline style
*/
outline?: boolean;
/**
* The size of the checkbox. affect both width and height
*/
Expand Down
45 changes: 35 additions & 10 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const DEFAULT_COLOR = Colors.blue30;
const DEFAULT_ICON_COLOR = Colors.white;
const DEFAULT_DISABLED_COLOR = Colors.grey50;

const DEFAULT_BORDER_WIDTH = 2;
const DEFAULT_BORDER_RADIUS = 8;

export interface CheckboxProps extends TouchableOpacityProps {
/**
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
Expand All @@ -32,6 +35,10 @@ export interface CheckboxProps extends TouchableOpacityProps {
* The Checkbox color
*/
color?: string;
/**
* alternative Checkbox outline style
*/
outline?: boolean;
/**
* The size of the checkbox. affect both width and height
*/
Expand Down Expand Up @@ -159,14 +166,24 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
}
};

getColor() {
const {color, disabled} = this.props;
return disabled ? DEFAULT_DISABLED_COLOR : color || DEFAULT_COLOR;
}
getColor = () => (this.props.disabled ? DEFAULT_DISABLED_COLOR : this.props.color || DEFAULT_COLOR);

getBackgroundColor = () => (this.props.outline ? 'transparent' : this.getColor());

getTintColor = () => {
const {outline, disabled, iconColor} = this.props;
if (outline) {
if (disabled) return DEFAULT_DISABLED_COLOR;
else return iconColor || DEFAULT_COLOR;
} else {
if (disabled) return Colors.white;
else return iconColor || Colors.white;
}
};

getBorderStyle() {
const borderColor = {borderColor: this.getColor()};
const borderStyle = [this.styles.container, {borderWidth: 2}, borderColor];
const borderStyle = [this.styles.container, {borderWidth: DEFAULT_BORDER_WIDTH}, borderColor];

return borderStyle;
}
Expand All @@ -186,14 +203,17 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
>
{
<Animated.View
style={[this.styles.container, {backgroundColor: this.getColor()}, {opacity: this.animationStyle.opacity}]}
style={[
this.styles.container,
{opacity: this.animationStyle.opacity},
{backgroundColor: this.getBackgroundColor()}
]}
>
<Animated.Image
style={[
this.styles.selectedIcon,
color && {tintColor: iconColor},
{transform: this.animationStyle.transform},
disabled && {tintColor: DEFAULT_ICON_COLOR}
{tintColor: this.getTintColor()}
]}
source={selectedIcon || Assets.icons.checkSmall}
testID={`${testID}.selected`}
Expand All @@ -220,13 +240,18 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
}

function createStyles(props: CheckboxProps) {
const {color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, size = DEFAULT_SIZE, borderRadius} = props;
const {
color = DEFAULT_COLOR,
iconColor = DEFAULT_ICON_COLOR,
size = DEFAULT_SIZE,
borderRadius = DEFAULT_BORDER_RADIUS
} = props;

return StyleSheet.create({
container: {
width: size,
height: size,
borderRadius: borderRadius || 8,
borderRadius: borderRadius,
alignItems: 'center',
justifyContent: 'center',
borderColor: color
Expand Down