Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 40 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,44 @@ const ViewHolder = () => {
return <View style={NumpadStyle.buttonContainer} />
}

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,
Expand All @@ -61,9 +96,9 @@ const NumericPad = React.forwardRef(
rightBottomAccessibilityLabel,
rightBottomButtonItemStyle,
onRightBottomButtonPress
},
ref
) => {
} = propsWithDefaults;


const [input, setInput] = useState('')
ref.current = {
clear: () => {
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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: {
Expand Down