Skip to content

Commit 8d40650

Browse files
committed
Added a value formatter prop.
1 parent bff7bce commit 8d40650

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

PROPS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| `buttonTextColor` | Custom color of the button of the Spinner | String | Auto | |
2727
| `buttonPressTextStyle` | Button Style on Pressed state (Plus and Minus buttons) | Object | | Could overwrite other props |
2828
| `buttonTextStyle` | Button text Style state (Plus and Minus buttons) | Object | | Could overwrite other props |
29-
| `colorAsBackground` | Use color as background | Bool | `false` |
29+
| `colorAsBackground` | Use color as background | Bool | `false` | |
3030
| `colorLeft` | Custom color of the Spinner left button | String | `#3E525F` | |
3131
| `colorMax` | Custom color of the Spinner when reach max value | String | | |
3232
| `colorMin` | Custom color of the Spinner when reach min value | String | | |
@@ -39,12 +39,13 @@
3939
| `emptied` | Set if input can be empty | Boolean | `false` | |
4040
| `fontFamily` | Custom fontFamily of the text input of the Spinner | String | System Default | |
4141
| `fontSize` | Custom fontSize of the text input of the Spinner | Number | `14` | |
42+
| `formatter` | Custom formatting of the Spinner text | Function | `null` | `(value) => { ...; return formattedValue }` |
4243
| `height` | Custom height of the Spinner | Number | `50` | |
4344
| `initialValue` | Initial value of the Spinner | String<br>Number | `0` | |
4445
| `inputStyle` | Input Style (Text number at middle) | Object | | Could overwrite other props |
4546
| `inputProps` | Customized TextInput Component props | Object | `null` | |
4647
| `leftButtonProps` | Customized left button (Touchable Component) props | Object | `null` | |
47-
| `longStep` | Value to increment or decrement the current spinner value `onLongPress` | String<br>Number | `step` |
48+
| `longStep` | Value to increment or decrement the current spinner value `onLongPress` | String<br>Number | `step` | |
4849
| `maxLength` | Limits the maximum number of characters that can be entered. | Number | | |
4950
| `max` | Max number permitted | String<br>Number | `null` | |
5051
| `min` | Min value permitted | String<br>Number | `0` | |

src/InputSpinner.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,22 @@ class InputSpinner extends Component {
484484
value = String(this._parseNum(value));
485485
}
486486
let hasPlaceholder = value === "0" && !isEmpty(this.props.placeholder);
487-
return hasPlaceholder
488-
? ""
489-
: value.replace(
490-
".",
491-
!isEmpty(this.props.decimalSeparator)
492-
? this.props.decimalSeparator
493-
: ".",
494-
);
487+
488+
let accountingForDecimals = hasPlaceholder
489+
? ""
490+
: value.replace(
491+
".",
492+
!isEmpty(this.props.decimalSeparator)
493+
? this.props.decimalSeparator
494+
: ".",
495+
);
496+
497+
if (this.props.formatter !== null && typeof(this.props.formatter) === 'function') {
498+
return this.props.formatter(value)
499+
}
500+
else {
501+
return accountingForDecimals
502+
}
495503
}
496504

497505
/**
@@ -1451,6 +1459,7 @@ InputSpinner.propTypes = {
14511459
leftButtonProps: PropTypes.object,
14521460
rightButtonProps: PropTypes.object,
14531461
buttonTextProps: PropTypes.object,
1462+
formatter: PropTypes.func,
14541463
};
14551464

14561465
InputSpinner.defaultProps = {
@@ -1512,6 +1521,7 @@ InputSpinner.defaultProps = {
15121521
leftButtonProps: null,
15131522
rightButtonProps: null,
15141523
buttonTextProps: null,
1524+
formatter: null,
15151525
};
15161526

15171527
export default InputSpinner;

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ export interface ReactNativeInputSpinnerProps {
7979
leftButtonProps?: object;
8080
rightButtonProps?: object;
8181
buttonTextProps?: TextProps;
82+
formatter?(...args: unknown[]): unknown;
8283
}
8384
export default class InputSpinner extends Component<ReactNativeInputSpinnerProps> {}

0 commit comments

Comments
 (0)