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(ui): checkbox component #238

Merged
merged 3 commits into from
Jan 28, 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
95 changes: 95 additions & 0 deletions src/framework/ui/checkbox/checkbox.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import React from 'react';
import {
TouchableOpacity,
View,
StyleSheet,
TouchableOpacityProps,
} from 'react-native';
import {
StyledComponentProps,
StyleType,
Interaction,
} from '@kitten/theme';
import { CheckMark } from '../drawable/checkmark/checkmark.component';

interface CheckBoxProps {
checked?: boolean;
status?: string;
size?: string;
onChange?: (checked: boolean) => void;
}

export type Props = CheckBoxProps & StyledComponentProps & TouchableOpacityProps;

export class CheckBox extends React.Component<Props> {

private onPress = () => {
if (this.props.onChange) {
this.props.onChange(!this.props.checked);
}
};

private onPressIn = () => {
this.props.dispatch([Interaction.ACTIVE]);
};

private onPressOut = () => {
this.props.dispatch([]);
};

private getComponentStyle = (style: StyleType): StyleType => {
return {
border: {
width: style.size,
height: style.size,
borderRadius: style.borderRadius,
borderWidth: style.borderWidth,
borderColor: style.borderColor,
backgroundColor: style.backgroundColor,
},
icon: {
width: style.size / 2,
height: style.size / 8,
backgroundColor: style.selectColor,
},
highlight: {
width: style.highlightSize,
height: style.highlightSize,
borderRadius: style.highlightBorderRadius,
backgroundColor: style.highlightColor,
opacity: style.highlightOpacity,
},
};
};

public render(): React.ReactNode {
const componentStyle: StyleType = this.getComponentStyle(this.props.themedStyle);

return (
<TouchableOpacity
{...this.props}
activeOpacity={1.0}
onPress={this.onPress}
onPressIn={this.onPressIn}
onPressOut={this.onPressOut}>
<View style={styles.border}>
<View style={[styles.highlight, componentStyle.highlight]}/>
<View style={[styles.border, componentStyle.border]}>
<CheckMark style={componentStyle.icon}/>
</View>
</View>
</TouchableOpacity>
);
}
}

const styles = StyleSheet.create({
border: {
justifyContent: 'center',
alignItems: 'center',
},
highlight: {
position: 'absolute',
alignSelf: 'center',
},
});
106 changes: 106 additions & 0 deletions src/framework/ui/checkbox/checkbox.spec.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { ThemeMappingType } from 'eva/packages/common';
import { ThemeType } from '@kitten/theme';

export const mapping: ThemeMappingType = {
CheckBox: {
meta: {
variants: {
status: [
'error',
],
size: [
'small',
'big',
],
},
states: [
'checked',
'disabled',
'active',
],
},
appearance: {
'default': {
mapping: {
size: 30,
highlightSize: 50,
borderWidth: 2,
borderRadius: 4,
highlightBorderRadius: 8,
borderColor: 'gray-primary',
backgroundColor: 'gray-light',
selectColor: 'transparent',
highlightColor: 'transparent',
state: {
active: {
borderColor: 'gray-dark',
highlightColor: 'gray-light',
},
checked: {
borderColor: 'transparent',
selectColor: '#FFFFFF',
backgroundColor: 'blue-primary',
},
disabled: {
borderColor: 'gray-light',
backgroundColor: '#F1F5F5',
},
'checked.active': {
borderColor: 'blue-dark',
},
'checked.disabled': {
borderColor: 'transparent',
backgroundColor: 'gray-primary',
},
},
},
variant: {
status: {
error: {
mapping: {
borderColor: 'pink-primary',
backgroundColor: '#FFC9D9',
state: {
active: {
borderColor: 'pink-primary',
},
checked: {
borderColor: 'pink-primary',
backgroundColor: 'pink-primary',
},
'checked.active': {
borderColor: 'pink-primary',
},
},
},
},
},
size: {
big: {
mapping: {
size: 36,
highlightSize: 60,
},
},
small: {
mapping: {
size: 24,
highlightSize: 40,
},
},
},
},
},
},
},
};

export const theme: ThemeType = {
'blue-primary': '#3366FF',
'blue-dark': '#2541CC',
'gray-light': '#DDE1EB',
'gray-primary': '#A6AEBD',
'gray-dark': '#8992A3',
'gray-highlight': '#EDF0F5',
'pink-primary': '#FF3D71',
};
44 changes: 44 additions & 0 deletions src/framework/ui/checkbox/checkbox.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { TouchableOpacity } from 'react-native';
import {
render,
fireEvent,
RenderAPI,
} from 'react-native-testing-library';
import {
styled,
StyleProvider,
StyleProviderProps,
} from '@kitten/theme';
import {
CheckBox as CheckBoxComponent,
Props as CheckBoxProps,
} from './checkbox.component';
import * as config from './checkbox.spec.config';

const CheckBox = styled<CheckBoxComponent, CheckBoxProps>(CheckBoxComponent);

const Mock = (props?: CheckBoxProps): React.ReactElement<StyleProviderProps> => (
<StyleProvider mapping={config.mapping} theme={config.theme} styles={{}}>
<CheckBox {...props} />
</StyleProvider>
);

const renderComponent = (props?: CheckBoxProps): RenderAPI => render(<Mock {...props}/>);

describe('@checkbox: component checks', () => {

it('* emits onChange with correct args', () => {
const onChange = jest.fn();

const component: RenderAPI = renderComponent({
checked: true,
onChange: onChange,
});

fireEvent.press(component.getByType(TouchableOpacity));

expect(onChange).toBeCalledWith(false);
});

});
18 changes: 18 additions & 0 deletions src/framework/ui/drawable/checkmark/checkmark.component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import {
View,
ViewProps,
} from 'react-native';

type Props = ViewProps;

// TODO(ui): checkmark component

export class CheckMark extends React.Component<Props> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is such demo component in the "toggle" PR


render() {
return (
<View {...this.props}/>
);
}
}
7 changes: 7 additions & 0 deletions src/framework/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import {
RadioGroup as RadioGroupComponent,
Props as RadioGroupProps,
} from './radioGroup/radioGroup.component';
import {
CheckBox as CheckBoxComponent,
Props as CheckBoxProps,
} from './checkbox/checkbox.component';

const Radio = styled<RadioComponent, RadioProps>(RadioComponent);
const RadioGroup = styled<RadioGroupComponent, RadioGroupProps>(RadioGroupComponent);
const CheckBox = styled<CheckBoxComponent, CheckBoxProps>(CheckBoxComponent);

export {
Radio,
RadioGroup,
CheckBox,
RadioProps,
RadioGroupProps,
CheckBoxProps,
};

Loading