diff --git a/index.js b/index.js index 5c3953f..a53177f 100644 --- a/index.js +++ b/index.js @@ -40,9 +40,44 @@ const ViewHolder = () => { return } +const defaultProps = { + buttonTextByKey: { + one: '1', + two: '2', + three: '3', + four: '4', + five: '5', + six: '6', + seven: '7', + eight: '8', + nine: '9', + dot: '.', + zero: '0' + }, + accessible: false, + onButtonPress: () => {}, + onRightButtonPress: () => {}, + style: { paddingVertical: 12 }, + activeOpacity: 0.9, + buttonTextStyle: { color: '#000', fontSize: 30, fontWeight: '400' }, + rightBottomAccessibilityLabel: 'right_bottom', + numericDisabled: false, + rightBottomButtonDisabled: false, + allowDecimal: true +} + const NumericPad = React.forwardRef( ( - { + props, + ref + ) => { + //Default props + const propsWithDefaults = { + ...defaultProps, + ...props, + } + + const { numLength, allowDecimal, onValueChange, @@ -61,9 +96,9 @@ const NumericPad = React.forwardRef( rightBottomAccessibilityLabel, rightBottomButtonItemStyle, onRightBottomButtonPress - }, - ref - ) => { + } = propsWithDefaults; + + const [input, setInput] = useState('') ref.current = { clear: () => { @@ -75,22 +110,6 @@ const NumericPad = React.forwardRef( if (input.length > 0) { setInput('') } - }, - setValue: (newValue) => { - // Check for valid value based on rules (e.g., respecting numLength and decimal handling) - if (typeof newValue === 'string' && newValue.length <= numLength) { - if (!allowDecimal && newValue.includes('.')) { - // Ignore setting the value if decimals are not allowed - return; - } - if (newValue.includes('.')) { - const parts = newValue.split('.'); - if (parts[1].length > 2) { // Assuming you want at most two digits after the decimal - newValue = parts[0] + '.' + parts[1].substring(0, 2); - } - } - setInput(newValue); - } } } @@ -248,31 +267,7 @@ const NumericPad = React.forwardRef( } ) -NumericPad.defaultProps = { - buttonTextByKey: { - one: '1', - two: '2', - three: '3', - four: '4', - five: '5', - six: '6', - seven: '7', - eight: '8', - nine: '9', - dot: '.', - zero: '0' - }, - accessible: false, - onButtonPress: () => {}, - onRightButtonPress: () => {}, - style: { paddingVertical: 12 }, - activeOpacity: 0.9, - buttonTextStyle: { color: '#000', fontSize: 30, fontWeight: '400' }, - rightBottomAccessibilityLabel: 'right_bottom', - numericDisabled: false, - rightBottomButtonDisabled: false, - allowDecimal: true -} + const NumpadStyle = StyleSheet.create({ container: {