Skip to content

Commit 4cd1ec4

Browse files
authored
Merge pull request #39 from BlueBaseJS/fix/checkboxchange
fix(CheckBox): Added onValueChange prop in checkbox
2 parents f381f49 + 9a1d5f6 commit 4cd1ec4

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/components/Checkbox/Checkbox.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Checkbox as RNPCheckbox } from 'react-native-paper';
21
import { CheckboxProps } from '@bluebase/components';
2+
// tslint:disable-next-line: sort-imports
3+
import { Checkbox as RNPCheckbox } from 'react-native-paper';
34
import { SelectionControl } from '../SelectionControl';
45
import { Theme } from '@bluebase/core';
56
import { componentMapper } from '@bluebase/component-mapper';
@@ -21,6 +22,14 @@ export const Checkbox = componentMapper<CheckboxProps>(SelectionControl, {
2122

2223
return color;
2324
},
25+
onPress: ({ onPress, onValueChange, value, checked }: any) => () => {
26+
if (onPress) {
27+
onPress();
28+
}
29+
if (onValueChange) {
30+
onValueChange(value, !checked);
31+
}
32+
},
2433

2534
status: ({ checked, indeterminate }: CheckboxProps) => {
2635
if (indeterminate === true) {

src/components/Checkbox/__stories__/Checkbox.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ stories
2323
color="blue"
2424
/>
2525
<Checkbox
26-
onValueChange={action('Checked')}
26+
value="test1"
27+
onValueChange={(a, b)=>{
28+
// console.log(a);
29+
// console.log(b);
30+
}}
2731
label="Unchecked"
2832
color="blue"
2933
checked={false}

src/components/Checkbox/__tests__/Checkbox.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BlueBaseApp, getComponent } from '@bluebase/core';
2+
23
import { Checkbox } from '../Checkbox';
34
import React from 'react';
45
import { mount } from 'enzyme';
@@ -110,6 +111,29 @@ describe.only('Checkbox', () => {
110111
expect(component.find('Checkbox Text').last().text()).toEqual('Foo');
111112
});
112113

114+
it('should check that the onPress and onValueChange is binded correctly', async () => {
115+
const onPressFunc = jest.fn();
116+
const onValueChangeFunc = jest.fn();
117+
118+
const BBCheckbox = getComponent('Checkbox');
119+
120+
const component = mount(
121+
<BlueBaseApp components={{ Checkbox }}>
122+
<BBCheckbox color="default" onPress={onPressFunc} value={1} onValueChange={onValueChangeFunc} />
123+
</BlueBaseApp>
124+
);
125+
126+
await waitForElement(component as any, BBCheckbox);
127+
128+
const onPress = component.find('TouchableHighlight').last().prop('onPress') as any;
129+
onPress();
130+
expect(onPressFunc).toBeCalled();
131+
132+
const onValueChange = component.find('TouchableHighlight').last().prop('onValueChange') as any;
133+
onValueChange();
134+
expect(onValueChangeFunc).toBeCalledWith(1, true);
135+
});
136+
113137
});
114138

115139

0 commit comments

Comments
 (0)