mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-04-22 22:57:57 +00:00
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-params authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Props } from '../types.js';
|
|
|
|
import React, { useCallback, useState } from 'react';
|
|
|
|
// circular dep :(
|
|
import InputBalance from '@pezkuwi/react-components/InputBalance';
|
|
import { BN } from '@pezkuwi/util';
|
|
|
|
import Bare from './Bare.js';
|
|
|
|
function Balance ({ className = '', defaultValue: { value }, isDisabled, isError, label, onChange, onEnter, onEscape, withLabel }: Props): React.ReactElement<Props> {
|
|
const [isValid, setIsValid] = useState(false);
|
|
const [defaultValue] = useState(() => new BN((value as BN || '0').toString()).toString(10));
|
|
|
|
const _onChange = useCallback(
|
|
(value?: BN): void => {
|
|
const isValid = !isError && !!value;
|
|
|
|
onChange && onChange({
|
|
isValid,
|
|
value
|
|
});
|
|
setIsValid(isValid);
|
|
},
|
|
[isError, onChange]
|
|
);
|
|
|
|
return (
|
|
<Bare className={className}>
|
|
<InputBalance
|
|
className='full'
|
|
defaultValue={defaultValue}
|
|
isDisabled={isDisabled}
|
|
isError={isError || !isValid}
|
|
label={label}
|
|
onChange={_onChange}
|
|
onEnter={onEnter}
|
|
onEscape={onEscape}
|
|
withEllipsis
|
|
withLabel={withLabel}
|
|
/>
|
|
</Bare>
|
|
);
|
|
}
|
|
|
|
export default React.memo(Balance);
|
|
|
|
export { Balance };
|