Skip to content

Commit 94479c7

Browse files
committed
feat: add text auto color detect and button text styles prop
1 parent e580f43 commit 94479c7

File tree

1 file changed

+82
-11
lines changed

1 file changed

+82
-11
lines changed

src/InputSpinner.js

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import {
99
import PropTypes from "prop-types";
1010
import {defaultColor, defaultTransparent, Style} from "./Style";
1111
import {
12+
colorsToHex,
1213
debounce,
1314
getColorContrast,
1415
isCallable,
1516
isEmpty,
1617
isNumeric,
1718
isTransparentColor,
1819
mergeViewStyle,
20+
parseColor,
1921
} from "./Utils";
2022

2123
/**
@@ -508,6 +510,23 @@ class InputSpinner extends Component {
508510
}
509511
}
510512

513+
/**
514+
* Get Placeholder
515+
* @returns {*}
516+
*/
517+
getPlaceholderColor() {
518+
if (this.props.placeholderTextColor) {
519+
return this.props.placeholderTextColor;
520+
}
521+
let textColor = this._getInputTextColor();
522+
let parse = parseColor(textColor);
523+
parse[3] = Math.round(0.6 * 255);
524+
525+
console.log(colorsToHex(parse));
526+
527+
return colorsToHex(parse);
528+
}
529+
511530
/**
512531
* Get Type
513532
* @returns {String}
@@ -858,14 +877,25 @@ class InputSpinner extends Component {
858877
* @returns {String|*}
859878
* @private
860879
*/
861-
_getColor() {
880+
_getMainColor() {
862881
let color = this.props.color;
863882
if (this.props.colorAsBackground) {
864883
color = defaultTransparent;
865884
} else if (isTransparentColor(color)) {
866885
color = defaultTransparent;
867886
}
868887

888+
return color;
889+
}
890+
891+
/**
892+
* Get button color
893+
* @returns {String|*}
894+
* @private
895+
*/
896+
_getColor() {
897+
const color = this._getMainColor();
898+
869899
return this.isMaxReached()
870900
? this._getColorMax()
871901
: this.isMinReached()
@@ -923,9 +953,15 @@ class InputSpinner extends Component {
923953
const pressColor = this.props.buttonPressTextColor
924954
? this.props.buttonPressTextColor
925955
: this._getColorText();
926-
return this.props.buttonPressTextColor !== this.props.buttonTextColor
927-
? pressColor
928-
: getColorContrast(color);
956+
let textColor =
957+
this.props.buttonPressTextColor !== this.props.buttonTextColor
958+
? pressColor
959+
: "auto";
960+
if (String(textColor).toLowerCase().trim() === "auto") {
961+
textColor = getColorContrast(color);
962+
}
963+
964+
return textColor;
929965
}
930966

931967
/**
@@ -937,9 +973,17 @@ class InputSpinner extends Component {
937973
const color = this.props.colorAsBackground
938974
? this._getColorBackground()
939975
: this._getColor();
940-
return this.props.buttonTextColor
941-
? this.props.buttonTextColor
942-
: getColorContrast(color);
976+
let textColor =
977+
this._getColor() !== this._getMainColor()
978+
? "auto"
979+
: this.props.buttonTextColor
980+
? this.props.buttonTextColor
981+
: "auto";
982+
if (String(textColor).toLowerCase().trim() === "auto") {
983+
textColor = getColorContrast(color);
984+
}
985+
986+
return textColor;
943987
}
944988

945989
/**
@@ -1010,6 +1054,18 @@ class InputSpinner extends Component {
10101054
];
10111055
}
10121056

1057+
/**
1058+
* Get input text color
1059+
* @returns {string|*}
1060+
* @private
1061+
*/
1062+
_getInputTextColor() {
1063+
const backgroundColor = this._getColorBackground();
1064+
return this.props.textColor
1065+
? this.props.textColor
1066+
: getColorContrast(backgroundColor);
1067+
}
1068+
10131069
/**
10141070
* Get input text style
10151071
* @returns {*[]}
@@ -1020,9 +1076,7 @@ class InputSpinner extends Component {
10201076
return [
10211077
Style.numberText,
10221078
{
1023-
color: this.props.textColor
1024-
? this.props.textColor
1025-
: getColorContrast(backgroundColor),
1079+
color: this._getInputTextColor(),
10261080
fontSize: this.props.fontSize,
10271081
fontFamily: this.props.fontFamily,
10281082
backgroundColor: backgroundColor,
@@ -1072,6 +1126,7 @@ class InputSpinner extends Component {
10721126
fontFamily: this.props.buttonFontFamily,
10731127
lineHeight: this.props.height,
10741128
},
1129+
this.props.buttonTextStyle ? this.props.buttonTextStyle : {},
10751130
];
10761131
}
10771132

@@ -1089,6 +1144,7 @@ class InputSpinner extends Component {
10891144
? this._getColorPressText()
10901145
: this._getColorText(),
10911146
},
1147+
this._isLeftButtonPressed() ? this.props.buttonPressTextStyle : {},
10921148
];
10931149
}
10941150

@@ -1106,6 +1162,7 @@ class InputSpinner extends Component {
11061162
? this._getColorPressText()
11071163
: this._getColorText(),
11081164
},
1165+
this._isRightButtonPressed() ? this.props.buttonPressTextStyle : {},
11091166
];
11101167
}
11111168

@@ -1250,7 +1307,7 @@ class InputSpinner extends Component {
12501307
style={this._getInputTextStyle()}
12511308
value={this.getValue()}
12521309
placeholder={this.getPlaceholder()}
1253-
placeholderTextColor={this.props.placeholderTextColor}
1310+
placeholderTextColor={this.getPlaceholderColor()}
12541311
selectionColor={this.props.selectionColor}
12551312
selectTextOnFocus={this.props.selectTextOnFocus}
12561313
returnKeyType={this.props.returnKeyType}
@@ -1346,12 +1403,24 @@ InputSpinner.propTypes = {
13461403
PropTypes.number,
13471404
PropTypes.string,
13481405
]),
1406+
buttonTextStyle: PropTypes.oneOfType([
1407+
PropTypes.object,
1408+
PropTypes.array,
1409+
PropTypes.number,
1410+
PropTypes.string,
1411+
]),
13491412
buttonPressStyle: PropTypes.oneOfType([
13501413
PropTypes.object,
13511414
PropTypes.array,
13521415
PropTypes.number,
13531416
PropTypes.string,
13541417
]),
1418+
buttonPressTextStyle: PropTypes.oneOfType([
1419+
PropTypes.object,
1420+
PropTypes.array,
1421+
PropTypes.number,
1422+
PropTypes.string,
1423+
]),
13551424
inputStyle: PropTypes.oneOfType([
13561425
PropTypes.object,
13571426
PropTypes.array,
@@ -1427,7 +1496,9 @@ InputSpinner.defaultProps = {
14271496
buttonLeftText: null,
14281497
buttonRightText: null,
14291498
buttonStyle: null,
1499+
buttonTextStyle: null,
14301500
buttonPressStyle: null,
1501+
buttonPressTextStyle: null,
14311502
inputStyle: null,
14321503
style: null,
14331504
decimalSeparator: ".",

0 commit comments

Comments
 (0)