Skip to content

Commit d5c1aa9

Browse files
ArnonZethanshar
andauthored
Infra/TS - Checkbox (#789)
* Migrating CheckboxScreen to tsx * Adding Checkbox * Updating Checkbox generatedTypes * Migrating Checkbox to TS * Migrating Checkbox to TS * Updating HOC wrappers * Updating HOC wrappers Co-authored-by: Ethan Sharabi <[email protected]>
1 parent 676d186 commit d5c1aa9

File tree

5 files changed

+136
-60
lines changed

5 files changed

+136
-60
lines changed

demo/src/screens/componentScreens/CheckboxScreen.js renamed to demo/src/screens/componentScreens/CheckboxScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CheckboxScreen extends Component {
1010
value4: true,
1111
value5: false
1212
};
13-
13+
1414
render() {
1515
return (
1616
<View useSafeArea flex>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import { StyleProp, TouchableOpacityProps, ViewStyle } from 'react-native';
3+
interface CheckboxProps {
4+
/**
5+
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
6+
*/
7+
value?: boolean;
8+
/**
9+
* Invoked with the new value when the value changes.
10+
*/
11+
onValueChange?: Function;
12+
/**
13+
* Whether the checkbox should be disabled
14+
*/
15+
disabled?: boolean;
16+
/**
17+
* The Checkbox color
18+
*/
19+
color?: string;
20+
/**
21+
* The size of the checkbox. affect both width and height
22+
*/
23+
size?: number;
24+
/**
25+
* The Checkbox border radius
26+
*/
27+
borderRadius?: number;
28+
/**
29+
* The icon asset to use for the selected indication (accept only local assets)
30+
*/
31+
selectedIcon?: number;
32+
/**
33+
* The selected icon color
34+
*/
35+
iconColor?: string;
36+
/**
37+
* Additional styling
38+
*/
39+
style?: StyleProp<ViewStyle>;
40+
}
41+
declare type Props = CheckboxProps & TouchableOpacityProps;
42+
declare const _default: React.ComponentType<Props>;
43+
export default _default;

generatedTypes/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export {default as View} from './components/view';
88
export {default as Text} from './components/text';
99
export {default as TouchableOpacity} from './components/touchableOpacity';
1010
export {default as Button} from './components/button';
11+
export {default as Checkbox} from './components/checkbox';
1112
export {default as Image} from './components/image';
1213
export {default as Overlay} from './components/overlay';
1314
export {default as RadioButton} from './components/radioButton/RadioButton';
@@ -67,7 +68,6 @@ export {
6768
/* All components that are missing either manual or auto generated typings */
6869
export const AnimatedImage;
6970
export const AnimatedScanner;
70-
export const Checkbox;
7171
export const ColorPalette;
7272
export const ColorSwatch;
7373
export const DateTimePicker;
Lines changed: 89 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,98 @@
11
import _ from 'lodash';
2-
import PropTypes from 'prop-types';
3-
import React from 'react';
4-
import {Animated, Easing, StyleSheet} from 'react-native';
2+
import React, {Component} from 'react';
3+
import {Animated, Easing, StyleSheet, StyleProp, TouchableOpacityProps, ViewStyle} from 'react-native';
54
import {Colors} from '../../style';
5+
//@ts-ignore
66
import Assets from '../../assets';
7-
import {BaseComponent} from '../../commons';
7+
import {asBaseComponent} from '../../commons/new';
88
import TouchableOpacity from '../touchableOpacity';
99

1010
const DEFAULT_SIZE = 24;
1111
const DEFAULT_COLOR = Colors.blue30;
1212
const DEFAULT_ICON_COLOR = Colors.white;
1313
const DEFAULT_DISABLED_COLOR = Colors.dark70;
1414

15+
interface CheckboxProps {
16+
/**
17+
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
18+
*/
19+
value?: boolean;
20+
/**
21+
* Invoked with the new value when the value changes.
22+
*/
23+
onValueChange?: Function;
24+
/**
25+
* Whether the checkbox should be disabled
26+
*/
27+
disabled?: boolean;
28+
/**
29+
* The Checkbox color
30+
*/
31+
color?: string;
32+
/**
33+
* The size of the checkbox. affect both width and height
34+
*/
35+
size?: number;
36+
/**
37+
* The Checkbox border radius
38+
*/
39+
borderRadius?: number;
40+
/**
41+
* The icon asset to use for the selected indication (accept only local assets)
42+
*/
43+
selectedIcon?: number;
44+
/**
45+
* The selected icon color
46+
*/
47+
iconColor?: string;
48+
/**
49+
* Additional styling
50+
*/
51+
style?: StyleProp<ViewStyle>
52+
}
53+
54+
type CheckboxState = {
55+
isChecked: Animated.Value;
56+
};
57+
58+
type Props = CheckboxProps & TouchableOpacityProps;
59+
1560
/**
16-
* Checkbox component for toggling boolean value related to some context
61+
* @description: Checkbox component for toggling boolean value related to some context
62+
* @extends: TouchableOpacity
63+
* @extendslink: docs/TouchableOpacity
64+
* @gif: https://media.giphy.com/media/xULW8j5WzsuPytqklq/giphy.gif
65+
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/CheckboxScreen.tsx
1766
*/
18-
class Checkbox extends BaseComponent {
67+
class Checkbox extends Component<Props, CheckboxState> {
1968
static displayName = 'Checkbox';
20-
static propTypes = {
21-
/**
22-
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
23-
*/
24-
value: PropTypes.bool,
25-
/**
26-
* Invoked with the new value when the value changes.
27-
*/
28-
onValueChange: PropTypes.func,
29-
/**
30-
* Whether the checkbox should be disabled
31-
*/
32-
disabled: PropTypes.bool,
33-
/**
34-
* The Checkbox color
35-
*/
36-
color: PropTypes.string,
37-
/**
38-
* The size of the checkbox. affect both width and height
39-
*/
40-
size: PropTypes.number,
41-
/**
42-
* The Checkbox border radius
43-
*/
44-
borderRadius: PropTypes.number,
45-
/**
46-
* The icon asset to use for the selected indication (accept only local assets)
47-
*/
48-
selectedIcon: PropTypes.number,
49-
/**
50-
* The selected icon color
51-
*/
52-
iconColor: PropTypes.string
69+
70+
styles: {
71+
container: StyleProp<ViewStyle>;
72+
selectedIcon: StyleProp<ViewStyle>;
5373
};
5474

55-
constructor(props) {
75+
animationStyle: {
76+
opacity: CheckboxState['isChecked'];
77+
transform: [
78+
{
79+
scaleX: CheckboxState['isChecked'];
80+
},
81+
{
82+
scaleY: CheckboxState['isChecked'];
83+
}
84+
];
85+
};
86+
87+
constructor(props: Props) {
5688
super(props);
5789

5890
this.state = {
5991
isChecked: new Animated.Value(this.props.value ? 1 : 0)
6092
};
6193

94+
this.styles = createStyles(props);
95+
6296
this.animationStyle = {
6397
opacity: this.state.isChecked,
6498
transform: [
@@ -72,15 +106,15 @@ class Checkbox extends BaseComponent {
72106
};
73107
}
74108

75-
componentDidUpdate(prevProps) {
76-
const {value} = this.getThemeProps();
109+
componentDidUpdate(prevProps: Props) {
110+
const {value} = this.props;
77111
if (prevProps.value !== value) {
78112
this.animateCheckbox(value);
79113
}
80114
}
81115

82116
getAccessibilityProps() {
83-
const {accessibilityLabel, disabled, value} = this.getThemeProps();
117+
const {accessibilityLabel, disabled, value} = this.props;
84118
const checkedState = value ? 'checked' : 'unchecked';
85119

86120
return {
@@ -91,11 +125,7 @@ class Checkbox extends BaseComponent {
91125
};
92126
}
93127

94-
generateStyles() {
95-
this.styles = createStyles(this.getThemeProps());
96-
}
97-
98-
animateCheckbox(value) {
128+
animateCheckbox(value: CheckboxProps['value']) {
99129
const {isChecked} = this.state;
100130

101131
Animated.timing(isChecked, {
@@ -107,35 +137,35 @@ class Checkbox extends BaseComponent {
107137
}
108138

109139
onPress = () => {
110-
const {disabled} = this.getThemeProps();
140+
const {disabled} = this.props;
111141

112142
if (!disabled) {
113143
_.invoke(this.props, 'onValueChange', !this.props.value);
114144
}
115145
};
116146

117147
getColor() {
118-
const {color, disabled} = this.getThemeProps();
148+
const {color, disabled} = this.props;
119149
return disabled ? DEFAULT_DISABLED_COLOR : color || DEFAULT_COLOR;
120150
}
121151

122152
getBorderStyle() {
123-
const {style: propsStyle} = this.getThemeProps();
124153
const borderColor = {borderColor: this.getColor()};
125-
const style = [this.styles.container, {borderWidth: 2}, borderColor, propsStyle];
154+
const borderStyle = [this.styles.container, {borderWidth: 2}, borderColor];
126155

127-
return style;
156+
return borderStyle;
128157
}
129158

130159
render() {
131-
const {selectedIcon, color, iconColor, disabled, testID, ...others} = this.getThemeProps();
160+
const {selectedIcon, color, iconColor, disabled, testID, style, ...others} = this.props;
132161
return (
162+
// @ts-ignore
133163
<TouchableOpacity
134164
{...this.getAccessibilityProps()}
135165
activeOpacity={1}
136166
testID={testID}
137167
{...others}
138-
style={this.getBorderStyle()}
168+
style={[this.getBorderStyle(), style]}
139169
onPress={this.onPress}
140170
>
141171
{
@@ -159,7 +189,9 @@ class Checkbox extends BaseComponent {
159189
}
160190
}
161191

162-
function createStyles({color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, size = DEFAULT_SIZE, borderRadius}) {
192+
function createStyles(props: Props) {
193+
const {color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, size = DEFAULT_SIZE, borderRadius} = props;
194+
163195
return StyleSheet.create({
164196
container: {
165197
width: size,
@@ -177,4 +209,4 @@ function createStyles({color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, si
177209
});
178210
}
179211

180-
export default Checkbox;
212+
export default asBaseComponent<Props>(Checkbox);

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* This is a fake index.ts file, for auto-generating the types of all the inline required components.
3-
* The real index file that will be bundled is "src/index.js".
3+
* The real index file that will be bundled is "src/index.js".
44
* Please use this file for declaring all the exports, so they could be picked up by typescript's complier
55
*/
66

@@ -9,6 +9,7 @@ export {default as View} from './components/view';
99
export {default as Text} from './components/text';
1010
export {default as TouchableOpacity} from './components/touchableOpacity';
1111
export {default as Button} from './components/button';
12+
export {default as Checkbox} from './components/checkbox';
1213
export {default as Image} from './components/image';
1314
export {default as Overlay} from './components/overlay';
1415
export {default as RadioButton} from './components/radioButton/RadioButton';

0 commit comments

Comments
 (0)