Skip to content

Commit

Permalink
fix: enforce NumberInput to have number data type (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
raychanks authored and Daniel committed Feb 8, 2022
1 parent dfd2889 commit c2d9b0c
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions addons/ondevice-controls/src/types/Number.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { StyleSheet, View } from 'react-native';
import Slider from '@react-native-community/slider';
import styled from '@emotion/native';
Expand All @@ -15,29 +15,36 @@ const Input = styled.TextInput(({ theme }) => ({
export interface NumberProps {
arg: {
name: string;
value: any;
value: number;
step: number;
min: number;
max: number;
range: boolean;
defaultValue: number;
};

onChange: (value: any) => void;
onChange: (value: number) => void;
}

const NumberType = ({ arg, onChange = (value) => value }: NumberProps) => {
const allowComma = typeof arg.value === 'string' ? arg.value.trim().replace(/,/, '.') : arg.value;
const showError = Number.isNaN(Number(allowComma));
const showError = Number.isNaN(arg.value);
const [numStr, setNumStr] = useState(arg.value.toString());

const handleNormalChangeText = (text: string) => {
const commaReplaced = text.trim().replace(/,/, '.');

setNumStr(commaReplaced);
onChange(Number(commaReplaced));
};

const renderNormal = () => {
return (
<Input
autoCapitalize="none"
underlineColorAndroid="transparent"
value={arg.value.toString()}
value={numStr}
keyboardType="numeric"
onChangeText={onChange}
onChangeText={handleNormalChangeText}
style={showError && styles.errorBorder}
/>
);
Expand Down

0 comments on commit c2d9b0c

Please sign in to comment.