Skip to content

Commit 010befc

Browse files
feat: added multiple color feature for tint shade
1 parent 1bf6e64 commit 010befc

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

index.js

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,29 @@ class OTPTextView extends Component {
4242
};
4343

4444
this.inputs = [];
45+
46+
this.checkTintColorCount();
4547
}
4648

49+
checkTintColorCount = () => {
50+
const { tintColor, offTintColor, inputCount } = this.props;
51+
52+
if (typeof tintColor !== "string" && tintColor.length !== inputCount) {
53+
throw new Error(
54+
"If tint color is an array it's length should be equal to input count"
55+
);
56+
}
57+
58+
if (
59+
typeof offTintColor !== "string" &&
60+
offTintColor.length !== inputCount
61+
) {
62+
throw new Error(
63+
"If off tint color is an array it's length should be equal to input count"
64+
);
65+
}
66+
};
67+
4768
basicValidation = (text) => {
4869
const validText = /^[0-9a-zA-Z]+$/;
4970
return text.match(validText);
@@ -92,26 +113,27 @@ class OTPTextView extends Component {
92113
const val = this.state.otpText[i] || "";
93114
const { handleTextChange, inputCellLength } = this.props;
94115
const { otpText } = this.state;
116+
95117
if (e.nativeEvent.key === "Backspace" && i !== 0) {
96-
if (val.length === 0) {
97-
if (otpText[i - 1].length === inputCellLength) {
98-
this.setState(
99-
(prevState) => {
100-
let { otpText } = prevState;
101-
otpText[i - 1] = otpText[i - 1]
102-
.split("")
103-
.splice(0, otpText[i - 1].length - 1)
104-
.join("");
105-
return {
106-
otpText,
107-
};
108-
},
109-
() => {
110-
handleTextChange(this.state.otpText.join(""));
111-
this.inputs[i - 1].focus();
112-
}
113-
);
114-
}
118+
if (!val.length && otpText[i - 1].length === inputCellLength) {
119+
this.setState(
120+
(prevState) => {
121+
let { otpText } = prevState;
122+
123+
otpText[i - 1] = otpText[i - 1]
124+
.split("")
125+
.splice(0, otpText[i - 1].length - 1)
126+
.join("");
127+
128+
return {
129+
otpText,
130+
};
131+
},
132+
() => {
133+
handleTextChange(this.state.otpText.join(""));
134+
this.inputs[i - 1].focus();
135+
}
136+
);
115137
}
116138
}
117139
};
@@ -169,11 +191,16 @@ class OTPTextView extends Component {
169191
const inputStyle = [
170192
styles.textInput,
171193
textInputStyle,
172-
{ borderColor: offTintColor },
194+
{
195+
borderColor:
196+
typeof offTintColor === "string" ? offTintColor : offTintColor[i],
197+
},
173198
];
174199

175200
if (focusedInput === i) {
176-
inputStyle.push({ borderColor: tintColor });
201+
inputStyle.push({
202+
borderColor: typeof tintColor === "string" ? tintColor : tintColor[i],
203+
});
177204
}
178205

179206
TextInputs.push(
@@ -208,8 +235,14 @@ OTPTextView.propTypes = {
208235
containerStyle: PropTypes.any,
209236
textInputStyle: PropTypes.any,
210237
inputCellLength: PropTypes.number,
211-
tintColor: PropTypes.string,
212-
offTintColor: PropTypes.string,
238+
tintColor: PropTypes.oneOfType([
239+
PropTypes.string,
240+
PropTypes.arrayOf(PropTypes.string),
241+
]),
242+
offTintColor: PropTypes.oneOfType([
243+
PropTypes.string,
244+
PropTypes.arrayOf(PropTypes.string),
245+
]),
213246
handleTextChange: PropTypes.func,
214247
inputType: PropTypes.string,
215248
keyboardType: PropTypes.string,

0 commit comments

Comments
 (0)