Skip to content

Commit 1041485

Browse files
authored
Complete TS Migration (#1927)
* Create a build script * Include src/index.ts in the transpilation * Stop exporting manual typed components * Fix main index file export and remove old index files * add react-native-color to transformIgnorePatterns * Remove old index.d.ts file * Remove exclusion of src/index.ts file * Add missing exports * Add more missing exports * Add manual typings of BaseInput * Exclude legacy BaseInput from TS build * Remove redundant ts-expect-error * Revert some of recent changes * Fix TS for BaseInput component * Fix tsconfig * Revert "Fix TS for BaseInput component" This reverts commit 9d5b121. * Convert baseComponent to TS * Fix typings for baseComponent * Remove src/index.js file
1 parent ae8d77e commit 1041485

File tree

13 files changed

+188
-526
lines changed

13 files changed

+188
-526
lines changed

generatedTypes/index.d.ts

Lines changed: 0 additions & 128 deletions
This file was deleted.

index.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "react-native-ui-lib",
33
"version": "5.0.0",
4-
"main": "index.js",
5-
"types": "index.d.ts",
4+
"main": "src/index.ts",
5+
"types": "src/index.d.ts",
66
"author": "Ethan Sharabi <[email protected]>",
77
"homepage": "https://github.com/wix/react-native-ui-lib",
88
"license": "MIT",
@@ -31,7 +31,7 @@
3131
"build:lib": "./node_modules/.bin/babel lib --out-dir lib --config-file ./src/.babelrc.json --extensions '.ts,.tsx'",
3232
"build:exports": "npm run build:src && npm run build:lib",
3333
"build:packages": "node scripts/buildPackages.js",
34-
"build": "tsc --p tsconfig.build.json && npm run build:exports && npm run build:packages",
34+
"build": "node scripts/build.js",
3535
"prepush": "npm run build:dev && npm run test",
3636
"log": "react-native log-ios | grep 'ethan -'",
3737
"docs:deploy": "./scripts/deployDocs.sh",
@@ -122,7 +122,7 @@
122122
"jest": {
123123
"preset": "react-native",
124124
"transformIgnorePatterns": [
125-
"node_modules/(?!(@react-native|react-native|react-native-reanimated|react-native-redash)/)"
125+
"node_modules/(?!(@react-native|react-native|react-native-reanimated|react-native-redash|react-native-color)/)"
126126
],
127127
"testPathIgnorePatterns": [
128128
"/e2e/",

scripts/build.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const childProcess = require('child_process');
2+
3+
console.info('## Start RNUILib Build ##');
4+
5+
console.info('## Build Typescript ##');
6+
childProcess.execSync('tsc --p tsconfig.build.json');
7+
8+
console.info('## Build src files ##');
9+
childProcess.execSync(`./node_modules/.bin/babel src --out-dir src --config-file ./src/.babelrc.json --extensions '.ts,.tsx'`);
10+
11+
console.info('## Build lib (native component) files ##');
12+
childProcess.execSync(`./node_modules/.bin/babel lib --out-dir lib --config-file ./src/.babelrc.json --extensions '.ts,.tsx'`);
13+
14+
console.info('## Build standalone components packages ##');
15+
require('./buildPackages');
16+
17+
console.info('## Complete RNUILib Build ##');

src/commons/baseComponent.js renamed to src/commons/baseComponent.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from 'react';
1+
import React, {ComponentType} from 'react';
22
// import PropTypes from 'prop-types';
33
import {StyleSheet} from 'react-native';
44
import _ from 'lodash';
55
import {Colors} from '../style';
66
import * as Modifiers from './modifiers';
77

8-
export default function baseComponent(usePure) {
8+
export default function baseComponent(usePure: boolean): ComponentType {
99
const parent = usePure ? React.PureComponent : React.Component;
1010
class BaseComponent extends parent {
1111
// static propTypes = {
@@ -14,9 +14,12 @@ export default function baseComponent(usePure) {
1414
// useNativeDriver: PropTypes.bool,
1515
// };
1616

17+
styles: any;
18+
view: any;
19+
1720
static extractOwnProps = Modifiers.extractOwnProps;
1821

19-
constructor(props) {
22+
constructor(props: any) {
2023
super(props);
2124
if (!this.styles) {
2225
this.generateStyles();
@@ -28,7 +31,7 @@ export default function baseComponent(usePure) {
2831
}
2932

3033
// TODO: remove this after migrating all components to use asBaseComponent HOC
31-
UNSAFE_componentWillReceiveProps(nextProps) {
34+
UNSAFE_componentWillReceiveProps(nextProps: any) {
3235
this.updateModifiers(this.getThemeProps(), nextProps);
3336
}
3437

@@ -71,7 +74,7 @@ export default function baseComponent(usePure) {
7174
}
7275

7376
// TODO: stop using this and remove it
74-
extractContainerStyle(props) {
77+
extractContainerStyle(props: any) {
7578
let containerStyle = {};
7679
if (props.containerStyle) {
7780
containerStyle = _.pickBy(props.containerStyle, (_value, key) => {
@@ -82,12 +85,12 @@ export default function baseComponent(usePure) {
8285
return containerStyle;
8386
}
8487

85-
updateModifiers(currentProps, nextProps) {
88+
updateModifiers(currentProps: any, nextProps: any) {
8689
const ignoredKeys = ['children', 'forwardedRef', 'style', 'testID'];
8790
const allKeys = _.union([..._.keys(currentProps), ..._.keys(nextProps)]).filter(key => !ignoredKeys.includes(key));
8891
const changedKeys = _.filter(allKeys, key => !_.isEqual(currentProps[key], nextProps[key]));
8992

90-
const options = {};
93+
const options: any = {};
9194
if (_.find(changedKeys, key => Modifiers.FLEX_KEY_PATTERN.test(key))) {
9295
options.flex = true;
9396
}
@@ -124,7 +127,7 @@ export default function baseComponent(usePure) {
124127
flex: true
125128
},
126129
props = this.getThemeProps()) {
127-
const style = {};
130+
const style: any = {};
128131

129132
if (options.backgroundColor) {
130133
style.backgroundColor = Modifiers.extractBackgroundColorValue(props);
@@ -154,10 +157,10 @@ export default function baseComponent(usePure) {
154157
// }
155158

156159
// React Native Methods
157-
setRef = r => (this.view = r);
160+
setRef = (r: any) => (this.view = r);
158161
getRef = () => this.view;
159-
measureInWindow = (...args) => this.getRef().measureInWindow(...args);
160-
measure = (...args) => this.getRef().measure(...args); // TODO: do we need this
162+
measureInWindow = (...args: any) => this.getRef().measureInWindow(...args);
163+
measure = (...args: any) => this.getRef().measure(...args); // TODO: do we need this
161164
}
162165

163166
return BaseComponent;

src/commons/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const createBaseComponentClass = usePure => require('./baseComponent').default(usePure);
22

3-
let BaseComponentClass;
4-
let PureBaseComponentClass;
3+
let BaseComponentClass = createBaseComponentClass(false);
4+
let PureBaseComponentClass = createBaseComponentClass(true);
55

66
module.exports = {
77
get BaseComponent() {

src/components/baseInput/index.d.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import {ReactElement, Component} from 'react';
2+
import {
3+
GestureResponderEvent,
4+
ImageSourcePropType,
5+
StyleProp,
6+
TextInputProps as RNTextInputProps,
7+
TextStyle,
8+
ViewStyle,
9+
ColorValue
10+
} from 'react-native';
11+
// import {BaseComponent} from '../commons';
12+
// import {TopBarProps} from './Modal';
13+
14+
export type BaseInputDefaultValidator = 'required' | 'email' | 'url' | 'number' | 'price';
15+
export type BaseInputCustomValidator = (value?: string) => boolean;
16+
export type BaseInputValidator = BaseInputDefaultValidator | BaseInputCustomValidator;
17+
export type BaseInputValidateProp = BaseInputValidator | BaseInputValidator[];
18+
19+
export interface BaseInputProps extends RNTextInputProps {
20+
color?: ColorValue;
21+
containerStyle?: StyleProp<ViewStyle>;
22+
validate?: BaseInputValidateProp;
23+
markRequired?: boolean;
24+
errorMessage?: string | string[];
25+
validateOnStart?: boolean;
26+
validateOnChange?: boolean;
27+
validateOnBlur?: boolean;
28+
onChangeValidity?: (isValid: boolean) => void;
29+
}
30+
31+
export type InputColorValue = ColorValue | {[key: string]: ColorValue};
32+
33+
export interface TextBaseInputProps {
34+
migrate?: boolean;
35+
floatingPlaceholder?: boolean;
36+
floatingPlaceholderColor?: InputColorValue;
37+
helperText?: string;
38+
hideUnderline?: boolean;
39+
underlineColor?: InputColorValue;
40+
disabledColor?: ColorValue;
41+
centered?: boolean;
42+
error?: string;
43+
enableErrors?: boolean;
44+
expandable?: boolean;
45+
transformer?: (text?: string) => string | undefined;
46+
title?: string;
47+
titleColor?: InputColorValue;
48+
titleStyle?: StyleProp<TextStyle>;
49+
showCharacterCounter?: boolean;
50+
floatOnFocus?: boolean;
51+
useTopErrors?: boolean;
52+
rightIconSource?: ImageSourcePropType;
53+
}
54+
55+
export interface TextFieldRightButtonProps {
56+
iconSource?: ImageSourcePropType;
57+
iconColor?: ColorValue;
58+
onPress?: (event: GestureResponderEvent) => void;
59+
style?: StyleProp<ViewStyle>;
60+
}
61+
62+
export interface TextFieldProps extends BaseInputProps, TextBaseInputProps {
63+
renderExpandableInput?: (props: TextFieldProps) => ReactElement;
64+
renderExpandable?: (props: TextFieldProps, state: TextFieldState) => ReactElement;
65+
onToggleExpandableModal?: (value?: string) => void;
66+
// topBarProps?: TopBarProps;
67+
rightButtonProps?: TextFieldRightButtonProps;
68+
}
69+
70+
export type TextFieldState = any;
71+
72+
export class TextField extends BaseInput<TextFieldProps, TextFieldState> {}
73+
74+
export interface TextInputProps extends BaseInputProps, TextBaseInputProps {
75+
renderExpandableInput?: (props: TextInputProps) => ReactElement | ReactElement[];
76+
renderExpandable?: (props: TextInputProps, state: TextInputState) => ReactElement | ReactElement[];
77+
}
78+
79+
export type TextInputState = any;
80+
81+
export class TextInput extends BaseInput<TextInputProps, TextInputState> {}
82+
83+
export interface MaskedInputProps extends TextFieldProps {
84+
renderMaskedText?: (value?: string) => ReactElement | ReactElement[];
85+
containerStyle?: StyleProp<ViewStyle>;
86+
}
87+
88+
export class MaskedInput extends BaseInput<MaskedInputProps> {}
89+
90+
export default class BaseInput<Props extends BaseInputProps = BaseInputProps, State = {}> extends Component<
91+
Props,
92+
State
93+
> {
94+
onBlur(): void;
95+
}

0 commit comments

Comments
 (0)