@@ -40,6 +40,41 @@ import {
4040 parseCaipChainId ,
4141} from '@metamask/utils' ;
4242
43+ /**
44+ * A list of stateful component types.
45+ */
46+ const STATEFUL_COMPONENT_TYPES = [
47+ 'Input' ,
48+ 'Dropdown' ,
49+ 'RadioGroup' ,
50+ 'FileInput' ,
51+ 'Checkbox' ,
52+ 'Selector' ,
53+ 'AssetSelector' ,
54+ 'AddressInput' ,
55+ ] as const ;
56+
57+ /**
58+ * Type for stateful component types.
59+ */
60+ type StatefulComponentType = ( typeof STATEFUL_COMPONENT_TYPES ) [ number ] ;
61+
62+ /**
63+ * Check if a component is a stateful component.
64+ *
65+ * @param component - The component to check.
66+ * @param component.type - The type of the component.
67+ *
68+ * @returns Whether the component is a stateful component.
69+ */
70+ export function isStatefulComponent ( component : { type : string } ) : component is {
71+ type : StatefulComponentType ;
72+ } {
73+ return STATEFUL_COMPONENT_TYPES . includes (
74+ component . type as StatefulComponentType ,
75+ ) ;
76+ }
77+
4378/**
4479 * A function to get the MultichainAssetController state.
4580 *
@@ -366,18 +401,7 @@ export function constructState(
366401 }
367402
368403 // Stateful components inside a form
369- // TODO: This is becoming a bit of a mess, we should consider refactoring this.
370- if (
371- currentForm &&
372- ( component . type === 'Input' ||
373- component . type === 'Dropdown' ||
374- component . type === 'RadioGroup' ||
375- component . type === 'FileInput' ||
376- component . type === 'Checkbox' ||
377- component . type === 'Selector' ||
378- component . type === 'AssetSelector' ||
379- component . type === 'AddressInput' )
380- ) {
404+ if ( currentForm && isStatefulComponent ( component ) ) {
381405 const formState = newState [ currentForm . name ] as FormState ;
382406 assertNameIsUnique ( formState , component . props . name ) ;
383407 formState [ component . props . name ] = constructInputState (
@@ -390,17 +414,7 @@ export function constructState(
390414 }
391415
392416 // Stateful components outside a form
393- // TODO: This is becoming a bit of a mess, we should consider refactoring this.
394- if (
395- component . type === 'Input' ||
396- component . type === 'Dropdown' ||
397- component . type === 'RadioGroup' ||
398- component . type === 'FileInput' ||
399- component . type === 'Checkbox' ||
400- component . type === 'Selector' ||
401- component . type === 'AssetSelector' ||
402- component . type === 'AddressInput'
403- ) {
417+ if ( isStatefulComponent ( component ) ) {
404418 assertNameIsUnique ( newState , component . props . name ) ;
405419 newState [ component . props . name ] = constructInputState (
406420 oldState ,
0 commit comments