Files
pezkuwi-apps/packages/react-params/src/Param/Amount.tsx
T
Satoshi Qazi Muhammed 45d479d2ec Move to @pezkuwi/dev 0.86.4 (eslint 10 toolchain)
- dev ^0.86.4 at the root and in packages/apps; directives renamed by
  pezkuwi-dev-migrate-eslint10.
- eslint --fix with the 0.86.4 config; one fix broke syntax
  (typescript-eslint prefer-optional-chain turned
  `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();`
  in TxButton) and is corrected by hand.
- The remaining findings fixed rather than disabled: optional chains
  (each suggestion reviewed), `cause` on rethrown errors, `if` instead of
  `a && promise` in effects, parallel instead of sequential awaits in
  Promise.all, Error rejections, no `delete` on arrays (the storage query
  cache is a Map; an upload's file list no longer keeps a hole), no
  `[object Object]` stringification (TFunction's second argument is text
  or options), dead code removed (referenda's never-shown expand button,
  react-query's unused CoreDescriptor).
2026-10-10 02:06:12 +04:00

87 lines
2.2 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BitLength } from '@pezkuwi/react-components/types';
import type { Registry, TypeDef } from '@pezkuwi/types/types';
import type { BN } from '@pezkuwi/util';
import type { Props } from '../types.js';
import React, { useCallback, useMemo } from 'react';
import { Input, InputNumber } from '@pezkuwi/react-components';
import { bnToBn, formatNumber, isUndefined } from '@pezkuwi/util';
import Bare from './Bare.js';
function getBitLength (registry: Registry, { type }: TypeDef): BitLength {
try {
return registry.createType(type as 'u32').bitLength() as BitLength;
} catch {
return 32;
}
}
function Amount ({ className = '', defaultValue: { value }, isDisabled, isError, label, onChange, onEnter, registry, type, withLabel }: Props): React.ReactElement<Props> {
const isSigned = useMemo(
// Allow signed inputs for i{8, 16, 32, 64, 128, ...} types
() => /^i\d*$/.test(type.type),
[type]
);
const defaultValue = useMemo(
() => isDisabled
? value instanceof registry.createClass('AccountIndex')
? value.toString()
: formatNumber(value as number)
: bnToBn((value as number) || 0).toString(),
[isDisabled, registry, value]
);
const bitLength = useMemo(
() => getBitLength(registry, type),
[registry, type]
);
const _onChange = useCallback(
(value?: BN) =>
onChange?.({
isValid: !isUndefined(value),
value
}),
[onChange]
);
return (
<Bare className={className}>
{isDisabled
? (
<Input
className='full'
defaultValue={defaultValue}
isDisabled
label={label}
withEllipsis
withLabel={withLabel}
/>
)
: (
<InputNumber
bitLength={bitLength}
className='full'
defaultValue={defaultValue}
isError={isError}
isSigned={isSigned}
isZeroable
label={label}
onChange={_onChange}
onEnter={onEnter}
withLabel={withLabel}
/>
)
}
</Bare>
);
}
export default React.memo(Amount);