From ab887f7b149f056a456ebac4b6cc304f5d15a689 Mon Sep 17 00:00:00 2001 From: Arjun Porwal <65214523+ap211unitech@users.noreply.github.com> Date: Tue, 25 Feb 2025 18:19:45 +0530 Subject: [PATCH] refactor: Option to utilize useParamDefs and improve parameter handling (#11330) --- packages/react-params/src/Param/Option.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/react-params/src/Param/Option.tsx b/packages/react-params/src/Param/Option.tsx index aeaa4e5077..a0f273a674 100644 --- a/packages/react-params/src/Param/Option.tsx +++ b/packages/react-params/src/Param/Option.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Codec, TypeDef } from '@polkadot/types/types'; -import type { Props, RawParamOnChangeValue } from '../types.js'; +import type { ParamDef, Props, RawParamOnChangeValue } from '../types.js'; import React, { useCallback, useEffect, useState } from 'react'; @@ -15,12 +15,19 @@ import { useTranslation } from '../translate.js'; import Base from './Base.js'; import Param from './index.js'; import Static from './Static.js'; +import useParamDefs from './useParamDefs.js'; +import { getParams } from './Vector.js'; const DEF_VALUE = { isValid: true, value: undefined }; const OPT_PREFIX = new Uint8Array([1]); -function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisabled, label, onChange, onEnter, onEscape, registry, type: { sub, withOptionActive }, withLabel }: Props): React.ReactElement { +function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisabled, label, onChange, onEnter, onEscape, registry, type, withLabel }: Props): React.ReactElement { const { t } = useTranslation(); + const inputParams = useParamDefs(registry, type); + const [params] = useState(() => getParams(inputParams, [], (inputParams[0].length || 1))); + + const { sub, withOptionActive } = type; + const [isActive, setIsActive] = useState(() => withOptionActive || !!(_defaultValue && _defaultValue.value instanceof Option && _defaultValue.value.isSome) || false); const [defaultValue] = useState( @@ -76,7 +83,7 @@ function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisable onEnter={onEnter} onEscape={onEscape} registry={registry} - type={sub as TypeDef} + type={(sub ?? params.at(0)?.type) as TypeDef} /> ) : (