Skip to content

Complete TS Migration #1927

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

Merged
merged 22 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
128 changes: 0 additions & 128 deletions generatedTypes/index.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions index.d.ts

This file was deleted.

4 changes: 0 additions & 4 deletions index.js

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "react-native-ui-lib",
"version": "5.0.0",
"main": "index.js",
"types": "index.d.ts",
"main": "src/index.ts",
"types": "src/index.d.ts",
"author": "Ethan Sharabi <ethan.shar@gmail.com>",
"homepage": "https://github.com/wix/react-native-ui-lib",
"license": "MIT",
Expand Down Expand Up @@ -31,7 +31,7 @@
"build:lib": "./node_modules/.bin/babel lib --out-dir lib --config-file ./src/.babelrc.json --extensions '.ts,.tsx'",
"build:exports": "npm run build:src && npm run build:lib",
"build:packages": "node scripts/buildPackages.js",
"build": "tsc --p tsconfig.build.json && npm run build:exports && npm run build:packages",
"build": "node scripts/build.js",
"prepush": "npm run build:dev && npm run test",
"log": "react-native log-ios | grep 'ethan -'",
"docs:deploy": "./scripts/deployDocs.sh",
Expand Down Expand Up @@ -122,7 +122,7 @@
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(@react-native|react-native|react-native-reanimated|react-native-redash)/)"
"node_modules/(?!(@react-native|react-native|react-native-reanimated|react-native-redash|react-native-color)/)"
],
"testPathIgnorePatterns": [
"/e2e/",
Expand Down
17 changes: 17 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const childProcess = require('child_process');

console.info('## Start RNUILib Build ##');

console.info('## Build Typescript ##');
childProcess.execSync('tsc --p tsconfig.build.json');

console.info('## Build src files ##');
childProcess.execSync(`./node_modules/.bin/babel src --out-dir src --config-file ./src/.babelrc.json --extensions '.ts,.tsx'`);

console.info('## Build lib (native component) files ##');
childProcess.execSync(`./node_modules/.bin/babel lib --out-dir lib --config-file ./src/.babelrc.json --extensions '.ts,.tsx'`);

console.info('## Build standalone components packages ##');
require('./buildPackages');

console.info('## Complete RNUILib Build ##');
25 changes: 14 additions & 11 deletions src/commons/baseComponent.js → src/commons/baseComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import React, {ComponentType} from 'react';
// import PropTypes from 'prop-types';
import {StyleSheet} from 'react-native';
import _ from 'lodash';
import {Colors} from '../style';
import * as Modifiers from './modifiers';

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

styles: any;
view: any;

static extractOwnProps = Modifiers.extractOwnProps;

constructor(props) {
constructor(props: any) {
super(props);
if (!this.styles) {
this.generateStyles();
Expand All @@ -28,7 +31,7 @@ export default function baseComponent(usePure) {
}

// TODO: remove this after migrating all components to use asBaseComponent HOC
UNSAFE_componentWillReceiveProps(nextProps) {
UNSAFE_componentWillReceiveProps(nextProps: any) {
this.updateModifiers(this.getThemeProps(), nextProps);
}

Expand Down Expand Up @@ -71,7 +74,7 @@ export default function baseComponent(usePure) {
}

// TODO: stop using this and remove it
extractContainerStyle(props) {
extractContainerStyle(props: any) {
let containerStyle = {};
if (props.containerStyle) {
containerStyle = _.pickBy(props.containerStyle, (_value, key) => {
Expand All @@ -82,12 +85,12 @@ export default function baseComponent(usePure) {
return containerStyle;
}

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

const options = {};
const options: any = {};
if (_.find(changedKeys, key => Modifiers.FLEX_KEY_PATTERN.test(key))) {
options.flex = true;
}
Expand Down Expand Up @@ -124,7 +127,7 @@ export default function baseComponent(usePure) {
flex: true
},
props = this.getThemeProps()) {
const style = {};
const style: any = {};

if (options.backgroundColor) {
style.backgroundColor = Modifiers.extractBackgroundColorValue(props);
Expand Down Expand Up @@ -154,10 +157,10 @@ export default function baseComponent(usePure) {
// }

// React Native Methods
setRef = r => (this.view = r);
setRef = (r: any) => (this.view = r);
getRef = () => this.view;
measureInWindow = (...args) => this.getRef().measureInWindow(...args);
measure = (...args) => this.getRef().measure(...args); // TODO: do we need this
measureInWindow = (...args: any) => this.getRef().measureInWindow(...args);
measure = (...args: any) => this.getRef().measure(...args); // TODO: do we need this
}

return BaseComponent;
Expand Down
4 changes: 2 additions & 2 deletions src/commons/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const createBaseComponentClass = usePure => require('./baseComponent').default(usePure);

let BaseComponentClass;
let PureBaseComponentClass;
let BaseComponentClass = createBaseComponentClass(false);
let PureBaseComponentClass = createBaseComponentClass(true);

module.exports = {
get BaseComponent() {
Expand Down
95 changes: 95 additions & 0 deletions src/components/baseInput/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {ReactElement, Component} from 'react';
import {
GestureResponderEvent,
ImageSourcePropType,
StyleProp,
TextInputProps as RNTextInputProps,
TextStyle,
ViewStyle,
ColorValue
} from 'react-native';
// import {BaseComponent} from '../commons';
// import {TopBarProps} from './Modal';

export type BaseInputDefaultValidator = 'required' | 'email' | 'url' | 'number' | 'price';
export type BaseInputCustomValidator = (value?: string) => boolean;
export type BaseInputValidator = BaseInputDefaultValidator | BaseInputCustomValidator;
export type BaseInputValidateProp = BaseInputValidator | BaseInputValidator[];

export interface BaseInputProps extends RNTextInputProps {
color?: ColorValue;
containerStyle?: StyleProp<ViewStyle>;
validate?: BaseInputValidateProp;
markRequired?: boolean;
errorMessage?: string | string[];
validateOnStart?: boolean;
validateOnChange?: boolean;
validateOnBlur?: boolean;
onChangeValidity?: (isValid: boolean) => void;
}

export type InputColorValue = ColorValue | {[key: string]: ColorValue};

export interface TextBaseInputProps {
migrate?: boolean;
floatingPlaceholder?: boolean;
floatingPlaceholderColor?: InputColorValue;
helperText?: string;
hideUnderline?: boolean;
underlineColor?: InputColorValue;
disabledColor?: ColorValue;
centered?: boolean;
error?: string;
enableErrors?: boolean;
expandable?: boolean;
transformer?: (text?: string) => string | undefined;
title?: string;
titleColor?: InputColorValue;
titleStyle?: StyleProp<TextStyle>;
showCharacterCounter?: boolean;
floatOnFocus?: boolean;
useTopErrors?: boolean;
rightIconSource?: ImageSourcePropType;
}

export interface TextFieldRightButtonProps {
iconSource?: ImageSourcePropType;
iconColor?: ColorValue;
onPress?: (event: GestureResponderEvent) => void;
style?: StyleProp<ViewStyle>;
}

export interface TextFieldProps extends BaseInputProps, TextBaseInputProps {
renderExpandableInput?: (props: TextFieldProps) => ReactElement;
renderExpandable?: (props: TextFieldProps, state: TextFieldState) => ReactElement;
onToggleExpandableModal?: (value?: string) => void;
// topBarProps?: TopBarProps;
rightButtonProps?: TextFieldRightButtonProps;
}

export type TextFieldState = any;

export class TextField extends BaseInput<TextFieldProps, TextFieldState> {}

export interface TextInputProps extends BaseInputProps, TextBaseInputProps {
renderExpandableInput?: (props: TextInputProps) => ReactElement | ReactElement[];
renderExpandable?: (props: TextInputProps, state: TextInputState) => ReactElement | ReactElement[];
}

export type TextInputState = any;

export class TextInput extends BaseInput<TextInputProps, TextInputState> {}

export interface MaskedInputProps extends TextFieldProps {
renderMaskedText?: (value?: string) => ReactElement | ReactElement[];
containerStyle?: StyleProp<ViewStyle>;
}

export class MaskedInput extends BaseInput<MaskedInputProps> {}

export default class BaseInput<Props extends BaseInputProps = BaseInputProps, State = {}> extends Component<
Props,
State
> {
onBlur(): void;
}
Loading