Skip to content

Create a middleware TextField migrator #1113

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 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions generatedTypes/components/textField/TextFieldMigrator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';
declare const _default: React.ForwardRefExoticComponent<Pick<any, string | number | symbol> & React.RefAttributes<unknown>>;
export default _default;
64 changes: 64 additions & 0 deletions src/components/textField/TextFieldMigrator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React, {forwardRef} from 'react';
import {mapKeys} from 'lodash';
// @ts-ignore
import OldTextField from './index';
import NewTextField from '../../incubator/TextField';
import {LogService} from '../../services';

const propsMigrationMap: Dictionary<string> = {
/* LABEL */
helperText: 'hint',
title: 'label',
titleColor: 'labelColor',
titleStyle: 'labelStyle',
/* CHAR COUNTER */
showCharacterCounter: 'showCharCounter'
};

const specialMigrationMap: Dictionary<string> = {
prefix: 'leadingAccessory',
prefixStyle: 'leadingAccessory',
rightIconSource: 'trailingAccessory',
rightIconStyle: 'trailingAccessory',
rightButtonProps: 'trailingAccessory',
leadingIcon: 'leadingAccessory',
useTopErrors: 'validationMessagePosition'
};

const customMessageMap: Dictionary<string> = {
centered: `Pass textAlign to 'style' prop instead.`,
error: `Use 'validationMessage' with 'validate' props`,
expandable: 'This prop will not be supported anymore',
renderExpandableInput: 'This prop will not be supported anymore',
renderExpandable: 'This prop will not be supported anymore',
onToggleExpandableModal: 'This prop will not be supported anymore',
topBarProps: 'This prop will not be supported anymore',
transformer: 'This prop will not be supported anymore'
};

function migrateProps(props: any) {
const fixedProps = mapKeys(props, (_value, key) => {
if (propsMigrationMap[key]) {
LogService.deprecationWarn({component: 'TextField', oldProp: key, newProp: propsMigrationMap[key]});
return propsMigrationMap[key];
} else if (specialMigrationMap[key]) {
LogService.warn(`The new TextField implementation does not support the '${key}' prop. Please use the '${specialMigrationMap[key]}' instead`);
} else if (customMessageMap[key]) {
LogService.warn(`The new TextField implementation does not support the '${key}' prop. ${customMessageMap[key]}`);
}
return key;
});

return fixedProps;
}

export default forwardRef(({migrate = false, ...props}: any, ref) => {
if (migrate) {
const migratedProps = migrateProps(props);
// @ts-ignore
return <NewTextField {...migratedProps} ref={ref}/>;
} else {
LogService.warn(`RNUILib TextField component will soon be replaced with a new implementation, in order to start the migration - please pass the 'migrate' prop`);
return <OldTextField {...props} ref={ref}/>;
}
});
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export default {
return require('./components/textArea').default;
},
get TextField() {
return require('./components/textField').default;
return require('./components/textField/TextFieldMigrator').default;
// return require('./components/textField').default;
},
get MaskedInput() {
return require('./components/maskedInput').default;
Expand Down