|
|
|
@ -6,49 +6,43 @@ import {CustomTooltip, isNumeric} from '@remix-ui/helper' |
|
|
|
|
import {ValueProps} from '../types' |
|
|
|
|
|
|
|
|
|
export function ValueUI(props: ValueProps) { |
|
|
|
|
const [sendValue, setSendValue] = useState<string>(props.sendValue) |
|
|
|
|
const inputValue = useRef<HTMLInputElement>({} as HTMLInputElement)
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
sendValue !== props.sendValue && props.setSendValue(sendValue) |
|
|
|
|
}, [sendValue]) |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if(props.sendValue !== sendValue) { |
|
|
|
|
setSendValue(props.sendValue) |
|
|
|
|
if (props.sendValue !== inputValue.current.value) { |
|
|
|
|
inputValue.current.value = props.sendValue |
|
|
|
|
} |
|
|
|
|
},[props.sendValue]) |
|
|
|
|
|
|
|
|
|
const validateInputKey = (e) => { |
|
|
|
|
// preventing not numeric keys
|
|
|
|
|
// preventing 000 case
|
|
|
|
|
if (!isNumeric(e.key) || (e.key === '0' && !parseInt(inputValue.current.value) && inputValue.current.value.length > 0)) { |
|
|
|
|
e.preventDefault() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const validateValue = (e) => { |
|
|
|
|
console.log('validateValue', e.target.value) |
|
|
|
|
const value = e.target.value |
|
|
|
|
|
|
|
|
|
if (!value) { |
|
|
|
|
// assign 0 if given value is
|
|
|
|
|
// - empty
|
|
|
|
|
return setSendValue('0') |
|
|
|
|
inputValue.current.value = '0' |
|
|
|
|
props.setSendValue('0') |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let v |
|
|
|
|
try { |
|
|
|
|
v = new BN(value, 10) |
|
|
|
|
setSendValue(v.toString(10)) |
|
|
|
|
props.setSendValue(v.toString(10)) |
|
|
|
|
} catch (e) { |
|
|
|
|
// assign 0 if given value is
|
|
|
|
|
// - not valid (for ex 4345-54)
|
|
|
|
|
// - contains only '0's (for ex 0000) copy past or edit
|
|
|
|
|
setSendValue('0') |
|
|
|
|
inputValue.current.value = '0' |
|
|
|
|
props.setSendValue('0') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// if giveen value is negative(possible with copy-pasting) set to 0
|
|
|
|
|
if (v.lt(0)) setSendValue('0') |
|
|
|
|
if (v.lt(0)) { |
|
|
|
|
inputValue.current.value = '0' |
|
|
|
|
props.setSendValue('0') |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
@ -67,9 +61,8 @@ export function ValueUI(props: ValueProps) { |
|
|
|
|
className="form-control udapp_gasNval udapp_col2" |
|
|
|
|
id="value" |
|
|
|
|
data-id="dandrValue" |
|
|
|
|
onKeyPress={validateInputKey} |
|
|
|
|
onChange={validateValue} |
|
|
|
|
value={sendValue} |
|
|
|
|
value={props.sendValue} |
|
|
|
|
/> |
|
|
|
|
</CustomTooltip> |
|
|
|
|
|
|
|
|
|