// Copyright 2017-2026 @pezkuwi/react-params authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { Codec, TypeDef } from '@pezkuwi/types/types'; import type { ParamDef, Props, RawParamOnChangeValue } from '../types.js'; import React, { useCallback, useEffect, useState } from 'react'; import { Toggle } from '@pezkuwi/react-components'; import { Option } from '@pezkuwi/types'; import { isU8a, u8aConcat } from '@pezkuwi/util'; import Holder from '../Holder.js'; 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, 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( () => isActive || isDisabled ? _defaultValue && ( _defaultValue.value instanceof Option && _defaultValue.value.isSome ? { isValid: _defaultValue.isValid, value: (_defaultValue.value as Option).unwrap() } : DEF_VALUE ) : DEF_VALUE ); useEffect((): void => { !isActive && onChange && onChange({ isValid: true, value: null }); }, [isActive, onChange]); const _onChange = useCallback( (value: RawParamOnChangeValue) => onChange && onChange( value.isValid && isU8a(value.value) && !withOptionActive && isActive ? { isValid: true, value: u8aConcat(OPT_PREFIX, value.value) } : value ), [isActive, onChange, withOptionActive] ); return (
)} withLabel={withLabel} />
{isActive ? ( ) : ( ) }
); } export default React.memo(OptionDisplay);