Skip to content

Commit 31969ca

Browse files
Merge pull request #92 from robinmacharg/enhancement/value-formatter
Add a value formatter prop.
2 parents bff7bce + 5d55504 commit 31969ca

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-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 }`. `editable` must be `false` |
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` | |

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ Check the "[Props List](PROPS.md)" file to have the complete list of component p
145145
| `typingTime` | Time before debounce and trigger `onChange` event | Number | `750` | |
146146
| `type` | Type of spinner | String | `int` | Can be `int` or `real`/`float`... |
147147
| `value` | Controlled value of the Spinner | String<br>Number | `0` | |
148+
| `formatter` | An optional function that is called to format the value for display | Function | `null` | Should return a `string`. `editable` must be `false`. |
148149

149150
#### Screenshots
150151

src/InputSpinner.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,14 +484,23 @@ 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+
// Only apply the formatter if the field is not user-editable
498+
if (this.props.formatter !== null && typeof(this.props.formatter) === 'function' && !this.props.editable) {
499+
return this.props.formatter(value)
500+
}
501+
else {
502+
return accountingForDecimals
503+
}
495504
}
496505

497506
/**
@@ -1451,6 +1460,7 @@ InputSpinner.propTypes = {
14511460
leftButtonProps: PropTypes.object,
14521461
rightButtonProps: PropTypes.object,
14531462
buttonTextProps: PropTypes.object,
1463+
formatter: PropTypes.func,
14541464
};
14551465

14561466
InputSpinner.defaultProps = {
@@ -1512,6 +1522,7 @@ InputSpinner.defaultProps = {
15121522
leftButtonProps: null,
15131523
rightButtonProps: null,
15141524
buttonTextProps: null,
1525+
formatter: null,
15151526
};
15161527

15171528
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)