Skip to content

Commit

Permalink
refactor: Option to utilize useParamDefs and improve parameter handli…
Browse files Browse the repository at this point in the history
…ng (#11330)
  • Loading branch information
ap211unitech authored Feb 25, 2025
1 parent 2abc297 commit ab887f7
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/react-params/src/Param/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<Props> {
function OptionDisplay ({ className = '', defaultValue: _defaultValue, isDisabled, label, onChange, onEnter, onEscape, registry, type, withLabel }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const inputParams = useParamDefs(registry, type);
const [params] = useState<ParamDef[]>(() => 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(
Expand Down Expand Up @@ -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}
/>
)
: (
Expand Down

0 comments on commit ab887f7

Please sign in to comment.