Remove instanceof, adjust VoteThreshold rendering (#7222)

* Remove instanceof, adjust VoteThreshold rendering

* useMemo
This commit is contained in:
Jaco 2022-03-24 07:00:55 +01:00 committed by GitHub
parent 78f79a17db
commit b45a6ce8ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,12 +1,13 @@
// Copyright 2017-2022 @polkadot/react-params authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { BN } from '@polkadot/util';
import type { Props } from '../types';
import React, { useCallback } from 'react';
import React, { useCallback, useMemo } from 'react';
import { Dropdown } from '@polkadot/react-components';
import { bnToBn } from '@polkadot/util';
import { bnToBn, isFunction } from '@polkadot/util';
import Bare from './Bare';
@ -24,7 +25,7 @@ export const textMap = options.reduce((textMap, { text, value }): TextMap => {
return textMap;
}, {} as unknown as TextMap);
function VoteThresholdParam ({ className = '', defaultValue: { value }, isDisabled, isError, label, onChange, registry, withLabel }: Props): React.ReactElement<Props> {
function VoteThresholdParam ({ className = '', defaultValue: { value }, isDisabled, isError, label, onChange, withLabel }: Props): React.ReactElement<Props> {
const _onChange = useCallback(
(value: number) =>
onChange && onChange({
@ -34,9 +35,12 @@ function VoteThresholdParam ({ className = '', defaultValue: { value }, isDisabl
[onChange]
);
const defaultValue = value instanceof registry.createClass('VoteThreshold')
? value.toNumber()
: bnToBn(value as number).toNumber();
const defaultValue = useMemo(
() => isFunction((value as BN).toNumber)
? (value as BN).toNumber()
: bnToBn(value as number).toNumber(),
[value]
);
return (
<Bare className={className}>