Skip to content

Commit 9d5b121

Browse files
committed
Fix TS for BaseInput component
1 parent c899d1a commit 9d5b121

File tree

4 files changed

+69
-122
lines changed

4 files changed

+69
-122
lines changed

src/components/baseInput/index.d.ts

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

src/components/baseInput/index.tsx

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// @ts-nocheck
21
import _ from 'lodash';
32
import PropTypes from 'prop-types';
4-
import 'react';
5-
import {ViewPropTypes, TextInput as RNTextInput} from 'react-native';
6-
import {Colors, Typography} from '../../style';
7-
import {BaseComponent} from '../../commons';
3+
import {Component} from 'react';
4+
import {ViewPropTypes, TextInput as RNTextInput, StyleProp, ViewStyle, TextInputProps} from 'react-native';
85
import Validators from './Validators';
6+
import {Colors, Typography} from '../../style';
7+
import * as Modifiers from '../../commons/modifiers';
98

109
const VALIDATORS = {
1110
REQUIRED: 'required',
@@ -15,10 +14,50 @@ const VALIDATORS = {
1514
PRICE: 'price'
1615
};
1716

18-
export default class BaseInput extends BaseComponent {
17+
interface BaseInputProps extends TextInputProps {
18+
/**
19+
* text color
20+
*/
21+
color: string;
22+
/**
23+
* text input container style
24+
*/
25+
containerStyle: StyleProp<ViewStyle>;
26+
/**
27+
* validator type or custom validator function
28+
*/
29+
validate: string | Function | (string | Function)[];
30+
/**
31+
* Whether to mark required field with an asterisk
32+
*/
33+
markRequired: boolean;
34+
/**
35+
* the message to be displayed when the validation fails
36+
*/
37+
errorMessage: string | string[];
38+
/**
39+
* whether to run the validation on mount
40+
*/
41+
validateOnStart: boolean;
42+
/**
43+
* whether to run the validation on text changed
44+
*/
45+
validateOnChange: boolean;
46+
/**
47+
* whether to run the validation on blur
48+
*/
49+
validateOnBlur: boolean;
50+
/**
51+
* callback for validity change
52+
*/
53+
onChangeValidity: Function;
54+
}
55+
56+
export default class BaseInput extends Component<BaseInputProps> {
1957
static displayName = 'BaseInput';
2058

2159
static propTypes = {
60+
// @ts-expect-error
2261
...RNTextInput.propTypes,
2362
// ...BaseComponent.propTypes,
2463
/**
@@ -67,17 +106,12 @@ export default class BaseInput extends BaseComponent {
67106
validateOnBlur: true
68107
};
69108

70-
constructor(props) {
71-
super(props);
72-
73-
this.state = {
74-
...this.state,
75-
value: props.value,
76-
focused: false,
77-
valid: false,
78-
error: undefined
79-
};
80-
}
109+
state = {
110+
value: this.props.value,
111+
focused: false,
112+
valid: false,
113+
error: undefined
114+
};
81115

82116
componentDidMount() {
83117
const {validateOnStart} = this.props;
@@ -87,12 +121,12 @@ export default class BaseInput extends BaseComponent {
87121
}
88122

89123
/** Events */
90-
onFocus = (...args) => {
124+
onFocus = (...args: any) => {
91125
_.invoke(this.props, 'onFocus', ...args);
92126
this.setState({focused: true});
93127
};
94128

95-
onBlur = (...args) => {
129+
onBlur = (...args: any) => {
96130
_.invoke(this.props, 'onBlur', ...args);
97131
this.setState({focused: false});
98132

@@ -102,11 +136,11 @@ export default class BaseInput extends BaseComponent {
102136
}
103137
};
104138

105-
onChange = event => {
139+
onChange = (event: any) => {
106140
_.invoke(this.props, 'onChange', event);
107141
};
108142

109-
onChangeText = text => {
143+
onChangeText = (text: string) => {
110144
_.invoke(this.props, 'onChangeText', text);
111145
this.setState({value: text});
112146

@@ -118,7 +152,8 @@ export default class BaseInput extends BaseComponent {
118152

119153
/** Actions */
120154
getTypography() {
121-
return this.extractTypographyValue() || Typography.text70;
155+
Modifiers;
156+
return Modifiers.extractTypographyValue(this.props) || Typography.text70;
122157
}
123158

124159
hasText() {
@@ -127,22 +162,27 @@ export default class BaseInput extends BaseComponent {
127162
}
128163

129164
isFocused() {
165+
// @ts-expect-error
130166
return this.input.isFocused();
131167
}
132168

133169
focus() {
170+
// @ts-expect-error
134171
this.input.focus();
135172
}
136173

137174
blur() {
175+
// @ts-expect-error
138176
this.input.blur();
139177
}
140178

141179
clear() {
180+
// @ts-expect-error
142181
this.input.clear();
143182
}
144183

145-
validate = (value = _.get(this, 'state.value'), dryRun) => {
184+
// @ts-expect-error
185+
validate = (value = _.get(this, 'state.value'), dryRun: boolean) => {
146186
// 'input.state.value'
147187
const {validate} = this.props;
148188
if (!validate) {
@@ -158,7 +198,9 @@ export default class BaseInput extends BaseComponent {
158198
let validatorFunction;
159199
if (_.isFunction(validator)) {
160200
validatorFunction = validator;
201+
// @ts-expect-error
161202
} else if (_.isString(validator) && !!Validators[validator]) {
203+
// @ts-expect-error
162204
validatorFunction = Validators[validator];
163205
}
164206

@@ -175,6 +217,7 @@ export default class BaseInput extends BaseComponent {
175217
if (!isValid) {
176218
const {errorMessage} = this.props;
177219
if (_.isArray(errorMessage)) {
220+
// @ts-expect-error
178221
error = errorMessage[failingValidatorIndex];
179222
} else {
180223
error = errorMessage;
@@ -203,8 +246,8 @@ export default class BaseInput extends BaseComponent {
203246
return validate === VALIDATORS.REQUIRED;
204247
}
205248

206-
getRequiredPlaceholder(placeholder) {
207-
const {markRequired} = this.getThemeProps();
249+
getRequiredPlaceholder(placeholder: string) {
250+
const {markRequired} = this.props;
208251
const shouldDisplayPlaceholderAsRequired = this.isRequiredField() && markRequired && placeholder;
209252

210253
if (shouldDisplayPlaceholderAsRequired) {
@@ -214,6 +257,7 @@ export default class BaseInput extends BaseComponent {
214257
}
215258

216259
getErrorMessage() {
260+
// @ts-expect-error
217261
const {error: propsError} = this.props;
218262
const {error: stateError} = this.state;
219263

src/components/dateTimePicker/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ class DateTimePicker extends Component<DateTimePickerPropsInternal, DateTimePick
286286
const {renderInput} = this.props;
287287

288288
return (
289-
// @ts-expect-error
290289
<TextField
291290
renderExpandableInput={renderInput}
292291
{...textInputProps}

src/components/textField/TextFieldMigrator.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ const TextFieldMigrator = forwardRef(({migrate = false, customWarning, ...props}
6666
// @ts-ignore
6767
return <NewTextField {...migratedProps} ref={ref}/>;
6868
} else {
69-
// @ts-expect-error
7069
return <OldTextField {...props} ref={ref}/>;
7170
}
7271
});

0 commit comments

Comments
 (0)