Skip to content

Commit b013b81

Browse files
authored
Fix some types, add exports for components Props types (#833)
* Fix return type for asBaseComponent * Add getThemeProps and extractAccessibilityProps for PureBaseComponent * Export more types of component props
1 parent ce9c6d5 commit b013b81

File tree

6 files changed

+33
-39
lines changed

6 files changed

+33
-39
lines changed

src/commons/asBaseComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface BaseComponentInjectedProps {
1414
modifiers: ReturnType<typeof Modifiers.generateModifiersStyle>;
1515
}
1616

17-
function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentType<any>): React.ComponentType<PROPS> & STATICS {
17+
function asBaseComponent<PROPS, STATICS = {}>(WrappedComponent: React.ComponentType<any>): React.ComponentClass<PROPS> & STATICS {
1818
class BaseComponent extends UIComponent {
1919
state = Modifiers.generateModifiersStyle(undefined, BaseComponent.getThemeProps(this.props, this.context));
2020
static displayName: string | undefined;

src/components/checkbox/index.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const DEFAULT_COLOR = Colors.blue30;
1212
const DEFAULT_ICON_COLOR = Colors.white;
1313
const DEFAULT_DISABLED_COLOR = Colors.dark70;
1414

15-
interface CheckboxProps {
15+
export interface CheckboxPropTypes extends TouchableOpacityProps {
1616
/**
1717
* The value of the Checkbox. If true the switch will be turned on. Default value is false.
1818
*/
@@ -51,20 +51,18 @@ interface CheckboxProps {
5151
style?: StyleProp<ViewStyle>
5252
}
5353

54-
type CheckboxState = {
54+
interface CheckboxState {
5555
isChecked: Animated.Value;
5656
};
5757

58-
type Props = CheckboxProps & TouchableOpacityProps;
59-
6058
/**
6159
* @description: Checkbox component for toggling boolean value related to some context
6260
* @extends: TouchableOpacity
6361
* @extendslink: docs/TouchableOpacity
6462
* @gif: https://media.giphy.com/media/xULW8j5WzsuPytqklq/giphy.gif
6563
* @example: https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/CheckboxScreen.tsx
6664
*/
67-
class Checkbox extends Component<Props, CheckboxState> {
65+
class Checkbox extends Component<CheckboxPropTypes, CheckboxState> {
6866
static displayName = 'Checkbox';
6967

7068
styles: {
@@ -84,7 +82,7 @@ class Checkbox extends Component<Props, CheckboxState> {
8482
];
8583
};
8684

87-
constructor(props: Props) {
85+
constructor(props: CheckboxPropTypes) {
8886
super(props);
8987

9088
this.state = {
@@ -106,7 +104,7 @@ class Checkbox extends Component<Props, CheckboxState> {
106104
};
107105
}
108106

109-
componentDidUpdate(prevProps: Props) {
107+
componentDidUpdate(prevProps: CheckboxPropTypes) {
110108
const {value} = this.props;
111109
if (prevProps.value !== value) {
112110
this.animateCheckbox(value);
@@ -125,7 +123,7 @@ class Checkbox extends Component<Props, CheckboxState> {
125123
};
126124
}
127125

128-
animateCheckbox(value: CheckboxProps['value']) {
126+
animateCheckbox(value: CheckboxPropTypes['value']) {
129127
const {isChecked} = this.state;
130128

131129
Animated.timing(isChecked, {
@@ -189,7 +187,7 @@ class Checkbox extends Component<Props, CheckboxState> {
189187
}
190188
}
191189

192-
function createStyles(props: Props) {
190+
function createStyles(props: CheckboxPropTypes) {
193191
const {color = DEFAULT_COLOR, iconColor = DEFAULT_ICON_COLOR, size = DEFAULT_SIZE, borderRadius} = props;
194192

195193
return StyleSheet.create({
@@ -209,4 +207,4 @@ function createStyles(props: Props) {
209207
});
210208
}
211209

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

src/components/radioButton/RadioButton.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjec
1616
import TouchableOpacity from '../touchableOpacity';
1717
import View from '../view';
1818
import Text from '../text';
19-
const Image = require('../image').default; // TODO: change this once Image is written in typescript
19+
import Image from '../image';
2020
import asRadioGroupChild from './asRadioGroupChild';
2121
import {RadioGroupContextPropTypes} from './RadioGroupContext';
2222

2323
const DEFAULT_SIZE = 24;
2424
const DEFAULT_COLOR = Colors.blue30;
2525

26-
interface RadioButtonPropTypes {
26+
export type RadioButtonPropTypes = RadioGroupContextPropTypes & BaseComponentInjectedProps & ForwardRefInjectedProps & ViewProps & {
2727
/**
2828
* The identifier value of the radio button. must be different than other RadioButtons in the same group
2929
*/
@@ -79,16 +79,10 @@ interface RadioButtonState {
7979
scaleAnimationValue: Animated.Value;
8080
}
8181

82-
type Props = RadioButtonPropTypes &
83-
RadioGroupContextPropTypes &
84-
BaseComponentInjectedProps &
85-
ForwardRefInjectedProps &
86-
ViewProps;
87-
8882
/**
8983
* A Radio Button component, should be wrapped inside a RadioGroup
9084
*/
91-
class RadioButton extends PureComponent<Props, RadioButtonState> {
85+
class RadioButton extends PureComponent<RadioButtonPropTypes, RadioButtonState> {
9286
static displayName = 'RadioButton';
9387

9488
static defaultProps = {
@@ -101,7 +95,7 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
10195
image: StyleProp<ImageStyle>;
10296
};
10397

104-
constructor(props: Props) {
98+
constructor(props: RadioButtonPropTypes) {
10599
super(props);
106100
this.styles = createStyles(props);
107101
this.state = {
@@ -114,7 +108,7 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
114108
this.animate();
115109
}
116110

117-
componentDidUpdate(prevProps: Props) {
111+
componentDidUpdate(prevProps: RadioButtonPropTypes) {
118112
if (prevProps.selected !== this.props.selected) {
119113
this.animate();
120114
}
@@ -258,7 +252,7 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
258252
}
259253
}
260254

261-
function createStyles(props: Props) {
255+
function createStyles(props: RadioButtonPropTypes) {
262256
const {size = DEFAULT_SIZE, borderRadius = DEFAULT_SIZE / 2, color = DEFAULT_COLOR, disabled} = props;
263257
return StyleSheet.create({
264258
radioButtonOutline: {

src/components/radioButton/RadioGroup.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjec
44
import View from '../view';
55
import RadioGroupContext from './RadioGroupContext';
66

7-
interface RadioGroupPropTypes {
7+
export type RadioGroupPropTypes = BaseComponentInjectedProps & ForwardRefInjectedProps & {
88
/**
99
* The initial value of the selected radio button
1010
*/
@@ -13,21 +13,19 @@ interface RadioGroupPropTypes {
1313
* Invoked once when value changes, by selecting one of the radio buttons in the group
1414
*/
1515
onValueChange?: (value: string | number | boolean) => void;
16-
}
16+
};
1717

1818
interface RadioGroupState {
1919
value?: RadioGroupPropTypes['initialValue'];
2020
}
2121

22-
type Props = RadioGroupPropTypes & BaseComponentInjectedProps & ForwardRefInjectedProps;
23-
2422
/**
2523
* Wrap a group of Radio Buttons to automatically control their selection
2624
*/
27-
class RadioGroup extends PureComponent<Props, RadioGroupState> {
25+
class RadioGroup extends PureComponent<RadioGroupPropTypes, RadioGroupState> {
2826
static displayName = 'RadioGroup';
2927

30-
constructor(props: Props) {
28+
constructor(props: RadioGroupPropTypes) {
3129
super(props);
3230

3331
this.state = {

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
*/
66

77
export * from './style';
8-
export {default as Card} from './components/card';
9-
export {default as View} from './components/view';
10-
export {default as Text} from './components/text';
11-
export {default as TouchableOpacity} from './components/touchableOpacity';
12-
export {default as Button} from './components/button';
13-
export {default as Checkbox} from './components/checkbox';
14-
export {default as Image} from './components/image';
15-
export {default as Overlay} from './components/overlay';
16-
export {default as RadioButton} from './components/radioButton/RadioButton';
17-
export {default as RadioGroup} from './components/radioButton/RadioGroup';
8+
export {default as Card, CardPropTypes, CardSectionProps} from './components/card';
9+
export {default as View, ViewPropTypes} from './components/view';
10+
export {default as Text, TextPropTypes} from './components/text';
11+
export {default as TouchableOpacity, TouchableOpacityProps} from './components/touchableOpacity';
12+
export {default as Button, ButtonPropTypes} from './components/button';
13+
export {default as Checkbox, CheckboxPropTypes} from './components/checkbox';
14+
export {default as Image, ImageProps} from './components/image';
15+
export {default as Overlay, OverlayTypes} from './components/overlay';
16+
export {default as RadioButton, RadioButtonPropTypes} from './components/radioButton/RadioButton';
17+
export {default as RadioGroup, RadioGroupPropTypes} from './components/radioButton/RadioGroup';
1818

1919
//================ Manual typings (all those exports should be removed one day) ==========
2020
export {

typings/commons/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,9 @@ export type _B = Partial<Record<BaseComponentColorModifierVariations, boolean>>;
5050
export class PureBaseComponent<P = {}, S = {}, SS = any> extends React.PureComponent<P & _B, S, SS> {
5151
static propTypes?: any;
5252
static defaultProps?: Partial<P>;
53+
54+
// TODO: There's more methods here, need to create more precise types
55+
getThemeProps: () => P & ThemeProps;
56+
extractAccessibilityProps: () => AccessibilityProps;
5357
}
5458

0 commit comments

Comments
 (0)