@lifi/widget 3.0.0-alpha.10 → 3.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AppProvider.tsx +4 -8
- package/_cjs/AppProvider.js +2 -2
- package/_cjs/AppProvider.js.map +1 -1
- package/_cjs/components/AmountInput/AmountInput.d.ts +1 -1
- package/_cjs/components/AmountInput/AmountInput.js +5 -11
- package/_cjs/components/AmountInput/AmountInput.js.map +1 -1
- package/_cjs/components/AmountInput/AmountInputEndAdornment.d.ts +1 -1
- package/_cjs/components/AmountInput/AmountInputEndAdornment.js +5 -11
- package/_cjs/components/AmountInput/AmountInputEndAdornment.js.map +1 -1
- package/_cjs/components/AmountInput/AmountInputStartAdornment.d.ts +1 -1
- package/_cjs/components/AmountInput/AmountInputStartAdornment.js +2 -8
- package/_cjs/components/AmountInput/AmountInputStartAdornment.js.map +1 -1
- package/_cjs/components/AmountInput/FormPriceHelperText.d.ts +1 -1
- package/_cjs/components/AmountInput/FormPriceHelperText.js +3 -11
- package/_cjs/components/AmountInput/FormPriceHelperText.js.map +1 -1
- package/_cjs/components/ChainSelect/ChainSelect.d.ts +1 -1
- package/_cjs/components/ChainSelect/ChainSelect.js +1 -5
- package/_cjs/components/ChainSelect/ChainSelect.js.map +1 -1
- package/_cjs/components/ChainSelect/useChainSelect.d.ts +1 -1
- package/_cjs/components/ChainSelect/useChainSelect.js +10 -14
- package/_cjs/components/ChainSelect/useChainSelect.js.map +1 -1
- package/_cjs/components/Header/NavigationTabs.js +4 -6
- package/_cjs/components/Header/NavigationTabs.js.map +1 -1
- package/_cjs/components/ListItemText.d.ts +1 -1
- package/_cjs/components/NFT/NFT.js +13 -14
- package/_cjs/components/NFT/NFT.js.map +1 -1
- package/_cjs/components/ReverseTokensButton/ReverseTokensButton.js +7 -14
- package/_cjs/components/ReverseTokensButton/ReverseTokensButton.js.map +1 -1
- package/_cjs/components/Routes/Routes.js +2 -2
- package/_cjs/components/Routes/Routes.js.map +1 -1
- package/_cjs/components/Routes/RoutesExpanded.js +1 -2
- package/_cjs/components/Routes/RoutesExpanded.js.map +1 -1
- package/_cjs/components/SelectChainAndToken.js +6 -13
- package/_cjs/components/SelectChainAndToken.js.map +1 -1
- package/_cjs/components/SelectTokenButton/SelectTokenButton.d.ts +1 -1
- package/_cjs/components/SelectTokenButton/SelectTokenButton.js +3 -5
- package/_cjs/components/SelectTokenButton/SelectTokenButton.js.map +1 -1
- package/_cjs/components/SendToWallet/SendToWallet.js +43 -32
- package/_cjs/components/SendToWallet/SendToWallet.js.map +1 -1
- package/_cjs/components/SendToWallet/SendToWalletButton.js +2 -3
- package/_cjs/components/SendToWallet/SendToWalletButton.js.map +1 -1
- package/_cjs/components/Step/StepProcess.style.d.ts +2 -2
- package/_cjs/components/Tabs/Tabs.style.d.ts +1 -1
- package/_cjs/components/TokenList/TokenList.js +3 -6
- package/_cjs/components/TokenList/TokenList.js.map +1 -1
- package/_cjs/components/TokenList/TokenList.style.d.ts +2 -2
- package/_cjs/components/TokenList/TokenNotFound.d.ts +1 -1
- package/_cjs/components/TokenList/TokenNotFound.js +2 -5
- package/_cjs/components/TokenList/TokenNotFound.js.map +1 -1
- package/_cjs/components/TokenList/types.d.ts +1 -1
- package/_cjs/components/TokenList/useTokenSelect.d.ts +1 -1
- package/_cjs/components/TokenList/useTokenSelect.js +15 -18
- package/_cjs/components/TokenList/useTokenSelect.js.map +1 -1
- package/_cjs/config/version.d.ts +1 -1
- package/_cjs/config/version.js +1 -1
- package/_cjs/hooks/useDebouncedWatch.d.ts +2 -1
- package/_cjs/hooks/useDebouncedWatch.js +3 -7
- package/_cjs/hooks/useDebouncedWatch.js.map +1 -1
- package/_cjs/hooks/useFromTokenSufficiency.js +1 -5
- package/_cjs/hooks/useFromTokenSufficiency.js.map +1 -1
- package/_cjs/hooks/useGasRefuel.js +2 -10
- package/_cjs/hooks/useGasRefuel.js.map +1 -1
- package/_cjs/hooks/useRequiredToAddress.js +2 -4
- package/_cjs/hooks/useRequiredToAddress.js.map +1 -1
- package/_cjs/hooks/useRoutes.js +2 -15
- package/_cjs/hooks/useRoutes.js.map +1 -1
- package/_cjs/index.d.ts +2 -2
- package/_cjs/index.js +5 -5
- package/_cjs/index.js.map +1 -1
- package/_cjs/pages/MainPage/ReviewButton.js +1 -2
- package/_cjs/pages/MainPage/ReviewButton.js.map +1 -1
- package/_cjs/pages/SelectChainPage/types.d.ts +1 -1
- package/_cjs/pages/SelectNativeTokenPage/SelectNativeTokenPage.d.ts +1 -1
- package/_cjs/pages/SelectTokenPage/SearchTokenInput.js +11 -6
- package/_cjs/pages/SelectTokenPage/SearchTokenInput.js.map +1 -1
- package/_cjs/pages/SelectTokenPage/SelectTokenPage.d.ts +1 -1
- package/_cjs/pages/TransactionPage/StatusBottomSheet.js +10 -11
- package/_cjs/pages/TransactionPage/StatusBottomSheet.js.map +1 -1
- package/_cjs/pages/TransactionPage/TransactionPage.js +4 -5
- package/_cjs/pages/TransactionPage/TransactionPage.js.map +1 -1
- package/_cjs/providers/index.d.ts +0 -1
- package/_cjs/providers/index.js +0 -1
- package/_cjs/providers/index.js.map +1 -1
- package/_cjs/stores/StoreProvider.js +4 -3
- package/_cjs/stores/StoreProvider.js.map +1 -1
- package/_cjs/stores/chains/ChainOrderStore.js +6 -10
- package/_cjs/stores/chains/ChainOrderStore.js.map +1 -1
- package/_cjs/stores/form/FormStore.d.ts +6 -0
- package/_cjs/stores/form/FormStore.js +44 -0
- package/_cjs/stores/form/FormStore.js.map +1 -0
- package/_cjs/stores/form/FormUpdater.d.ts +5 -0
- package/_cjs/stores/form/FormUpdater.js +62 -0
- package/_cjs/stores/form/FormUpdater.js.map +1 -0
- package/_cjs/{providers/FormProvider → stores/form}/URLSearchParamsBuilder.js +9 -12
- package/_cjs/stores/form/URLSearchParamsBuilder.js.map +1 -0
- package/_cjs/stores/form/createFormStore.d.ts +3 -0
- package/_cjs/stores/form/createFormStore.js +177 -0
- package/_cjs/stores/form/createFormStore.js.map +1 -0
- package/_cjs/stores/form/index.d.ts +10 -0
- package/_cjs/{providers/FormProvider → stores/form}/index.js +9 -2
- package/_cjs/stores/form/index.js.map +1 -0
- package/_cjs/stores/form/types.d.ts +108 -0
- package/_cjs/stores/form/types.js.map +1 -0
- package/_cjs/stores/form/useFieldActions.d.ts +2 -0
- package/_cjs/stores/form/useFieldActions.js +19 -0
- package/_cjs/stores/form/useFieldActions.js.map +1 -0
- package/_cjs/stores/form/useFieldController.d.ts +11 -0
- package/_cjs/stores/form/useFieldController.js +24 -0
- package/_cjs/stores/form/useFieldController.js.map +1 -0
- package/_cjs/stores/form/useFieldValues.d.ts +2 -0
- package/_cjs/stores/form/useFieldValues.js +11 -0
- package/_cjs/stores/form/useFieldValues.js.map +1 -0
- package/_cjs/stores/form/useTouchedFields.d.ts +1 -0
- package/_cjs/stores/form/useTouchedFields.js +11 -0
- package/_cjs/stores/form/useTouchedFields.js.map +1 -0
- package/_cjs/stores/form/useValidation.d.ts +2 -0
- package/_cjs/stores/form/useValidation.js +15 -0
- package/_cjs/stores/form/useValidation.js.map +1 -0
- package/_cjs/stores/form/useValidationActions.d.ts +2 -0
- package/_cjs/stores/form/useValidationActions.js +19 -0
- package/_cjs/stores/form/useValidationActions.js.map +1 -0
- package/_cjs/stores/index.d.ts +1 -0
- package/_cjs/stores/index.js +1 -0
- package/_cjs/stores/index.js.map +1 -1
- package/_esm/AppProvider.js +3 -3
- package/_esm/AppProvider.js.map +1 -1
- package/_esm/components/AmountInput/AmountInput.d.ts +1 -1
- package/_esm/components/AmountInput/AmountInput.js +5 -11
- package/_esm/components/AmountInput/AmountInput.js.map +1 -1
- package/_esm/components/AmountInput/AmountInputEndAdornment.d.ts +1 -1
- package/_esm/components/AmountInput/AmountInputEndAdornment.js +5 -11
- package/_esm/components/AmountInput/AmountInputEndAdornment.js.map +1 -1
- package/_esm/components/AmountInput/AmountInputStartAdornment.d.ts +1 -1
- package/_esm/components/AmountInput/AmountInputStartAdornment.js +2 -8
- package/_esm/components/AmountInput/AmountInputStartAdornment.js.map +1 -1
- package/_esm/components/AmountInput/FormPriceHelperText.d.ts +1 -1
- package/_esm/components/AmountInput/FormPriceHelperText.js +3 -11
- package/_esm/components/AmountInput/FormPriceHelperText.js.map +1 -1
- package/_esm/components/ChainSelect/ChainSelect.d.ts +1 -1
- package/_esm/components/ChainSelect/ChainSelect.js +2 -6
- package/_esm/components/ChainSelect/ChainSelect.js.map +1 -1
- package/_esm/components/ChainSelect/useChainSelect.d.ts +1 -1
- package/_esm/components/ChainSelect/useChainSelect.js +11 -15
- package/_esm/components/ChainSelect/useChainSelect.js.map +1 -1
- package/_esm/components/Header/NavigationTabs.js +5 -7
- package/_esm/components/Header/NavigationTabs.js.map +1 -1
- package/_esm/components/ListItemText.d.ts +1 -1
- package/_esm/components/NFT/NFT.js +13 -14
- package/_esm/components/NFT/NFT.js.map +1 -1
- package/_esm/components/ReverseTokensButton/ReverseTokensButton.js +7 -14
- package/_esm/components/ReverseTokensButton/ReverseTokensButton.js.map +1 -1
- package/_esm/components/Routes/Routes.js +2 -2
- package/_esm/components/Routes/Routes.js.map +1 -1
- package/_esm/components/Routes/RoutesExpanded.js +2 -3
- package/_esm/components/Routes/RoutesExpanded.js.map +1 -1
- package/_esm/components/SelectChainAndToken.js +7 -14
- package/_esm/components/SelectChainAndToken.js.map +1 -1
- package/_esm/components/SelectTokenButton/SelectTokenButton.d.ts +1 -1
- package/_esm/components/SelectTokenButton/SelectTokenButton.js +3 -5
- package/_esm/components/SelectTokenButton/SelectTokenButton.js.map +1 -1
- package/_esm/components/SendToWallet/SendToWallet.js +45 -34
- package/_esm/components/SendToWallet/SendToWallet.js.map +1 -1
- package/_esm/components/SendToWallet/SendToWalletButton.js +4 -5
- package/_esm/components/SendToWallet/SendToWalletButton.js.map +1 -1
- package/_esm/components/Step/StepProcess.style.d.ts +2 -2
- package/_esm/components/Tabs/Tabs.style.d.ts +1 -1
- package/_esm/components/TokenList/TokenList.js +3 -6
- package/_esm/components/TokenList/TokenList.js.map +1 -1
- package/_esm/components/TokenList/TokenList.style.d.ts +2 -2
- package/_esm/components/TokenList/TokenNotFound.d.ts +1 -1
- package/_esm/components/TokenList/TokenNotFound.js +2 -5
- package/_esm/components/TokenList/TokenNotFound.js.map +1 -1
- package/_esm/components/TokenList/types.d.ts +1 -1
- package/_esm/components/TokenList/useTokenSelect.d.ts +1 -1
- package/_esm/components/TokenList/useTokenSelect.js +15 -18
- package/_esm/components/TokenList/useTokenSelect.js.map +1 -1
- package/_esm/config/version.d.ts +1 -1
- package/_esm/config/version.js +1 -1
- package/_esm/hooks/useDebouncedWatch.d.ts +2 -1
- package/_esm/hooks/useDebouncedWatch.js +3 -7
- package/_esm/hooks/useDebouncedWatch.js.map +1 -1
- package/_esm/hooks/useFromTokenSufficiency.js +2 -6
- package/_esm/hooks/useFromTokenSufficiency.js.map +1 -1
- package/_esm/hooks/useGasRefuel.js +2 -10
- package/_esm/hooks/useGasRefuel.js.map +1 -1
- package/_esm/hooks/useRequiredToAddress.js +3 -5
- package/_esm/hooks/useRequiredToAddress.js.map +1 -1
- package/_esm/hooks/useRoutes.js +4 -17
- package/_esm/hooks/useRoutes.js.map +1 -1
- package/_esm/index.d.ts +2 -2
- package/_esm/index.js +2 -2
- package/_esm/index.js.map +1 -1
- package/_esm/pages/MainPage/ReviewButton.js +2 -3
- package/_esm/pages/MainPage/ReviewButton.js.map +1 -1
- package/_esm/pages/SelectChainPage/types.d.ts +1 -1
- package/_esm/pages/SelectNativeTokenPage/SelectNativeTokenPage.d.ts +1 -1
- package/_esm/pages/SelectTokenPage/SearchTokenInput.js +11 -6
- package/_esm/pages/SelectTokenPage/SearchTokenInput.js.map +1 -1
- package/_esm/pages/SelectTokenPage/SelectTokenPage.d.ts +1 -1
- package/_esm/pages/TransactionPage/StatusBottomSheet.js +12 -13
- package/_esm/pages/TransactionPage/StatusBottomSheet.js.map +1 -1
- package/_esm/pages/TransactionPage/TransactionPage.js +6 -7
- package/_esm/pages/TransactionPage/TransactionPage.js.map +1 -1
- package/_esm/providers/index.d.ts +0 -1
- package/_esm/providers/index.js +0 -1
- package/_esm/providers/index.js.map +1 -1
- package/_esm/stores/StoreProvider.js +4 -3
- package/_esm/stores/StoreProvider.js.map +1 -1
- package/_esm/stores/chains/ChainOrderStore.js +6 -10
- package/_esm/stores/chains/ChainOrderStore.js.map +1 -1
- package/_esm/stores/form/FormStore.d.ts +6 -0
- package/_esm/stores/form/FormStore.js +39 -0
- package/_esm/stores/form/FormStore.js.map +1 -0
- package/_esm/stores/form/FormUpdater.d.ts +5 -0
- package/_esm/stores/form/FormUpdater.js +58 -0
- package/_esm/stores/form/FormUpdater.js.map +1 -0
- package/_esm/{providers/FormProvider → stores/form}/URLSearchParamsBuilder.js +9 -12
- package/_esm/stores/form/URLSearchParamsBuilder.js.map +1 -0
- package/_esm/stores/form/createFormStore.d.ts +3 -0
- package/_esm/stores/form/createFormStore.js +173 -0
- package/_esm/stores/form/createFormStore.js.map +1 -0
- package/_esm/stores/form/index.d.ts +10 -0
- package/_esm/stores/form/index.js +11 -0
- package/_esm/stores/form/index.js.map +1 -0
- package/_esm/stores/form/types.d.ts +108 -0
- package/_esm/stores/form/types.js.map +1 -0
- package/_esm/stores/form/useFieldActions.d.ts +2 -0
- package/_esm/stores/form/useFieldActions.js +15 -0
- package/_esm/stores/form/useFieldActions.js.map +1 -0
- package/_esm/stores/form/useFieldController.d.ts +11 -0
- package/_esm/stores/form/useFieldController.js +20 -0
- package/_esm/stores/form/useFieldController.js.map +1 -0
- package/_esm/stores/form/useFieldValues.d.ts +2 -0
- package/_esm/stores/form/useFieldValues.js +7 -0
- package/_esm/stores/form/useFieldValues.js.map +1 -0
- package/_esm/stores/form/useTouchedFields.d.ts +1 -0
- package/_esm/stores/form/useTouchedFields.js +7 -0
- package/_esm/stores/form/useTouchedFields.js.map +1 -0
- package/_esm/stores/form/useValidation.d.ts +2 -0
- package/_esm/stores/form/useValidation.js +11 -0
- package/_esm/stores/form/useValidation.js.map +1 -0
- package/_esm/stores/form/useValidationActions.d.ts +2 -0
- package/_esm/stores/form/useValidationActions.js +15 -0
- package/_esm/stores/form/useValidationActions.js.map +1 -0
- package/_esm/stores/index.d.ts +1 -0
- package/_esm/stores/index.js +1 -0
- package/_esm/stores/index.js.map +1 -1
- package/components/AmountInput/AmountInput.tsx +12 -14
- package/components/AmountInput/AmountInputEndAdornment.tsx +10 -12
- package/components/AmountInput/AmountInputStartAdornment.tsx +6 -9
- package/components/AmountInput/FormPriceHelperText.tsx +7 -13
- package/components/ChainSelect/ChainSelect.tsx +4 -7
- package/components/ChainSelect/useChainSelect.ts +18 -18
- package/components/Header/NavigationTabs.tsx +6 -7
- package/components/NFT/NFT.tsx +14 -14
- package/components/ReverseTokensButton/ReverseTokensButton.tsx +8 -14
- package/components/Routes/Routes.tsx +2 -2
- package/components/Routes/RoutesExpanded.tsx +2 -3
- package/components/SelectChainAndToken.tsx +13 -14
- package/components/SelectTokenButton/SelectTokenButton.tsx +7 -6
- package/components/SendToWallet/SendToWallet.tsx +58 -38
- package/components/SendToWallet/SendToWalletButton.tsx +8 -5
- package/components/TokenList/TokenList.tsx +3 -6
- package/components/TokenList/TokenNotFound.tsx +3 -6
- package/components/TokenList/types.ts +1 -1
- package/components/TokenList/useTokenSelect.ts +21 -19
- package/config/version.ts +1 -1
- package/hooks/useDebouncedWatch.ts +7 -8
- package/hooks/useFromTokenSufficiency.ts +6 -6
- package/hooks/useGasRefuel.ts +7 -10
- package/hooks/useRequiredToAddress.ts +3 -5
- package/hooks/useRoutes.ts +14 -17
- package/index.ts +2 -2
- package/package.json +10 -11
- package/pages/MainPage/ReviewButton.tsx +6 -3
- package/pages/SelectChainPage/types.ts +1 -1
- package/pages/SelectNativeTokenPage/SelectNativeTokenPage.tsx +1 -1
- package/pages/SelectTokenPage/SearchTokenInput.tsx +11 -6
- package/pages/SelectTokenPage/SelectTokenPage.tsx +1 -1
- package/pages/TransactionPage/StatusBottomSheet.tsx +18 -15
- package/pages/TransactionPage/TransactionPage.tsx +10 -7
- package/providers/index.ts +0 -1
- package/stores/StoreProvider.tsx +18 -15
- package/stores/chains/ChainOrderStore.tsx +9 -10
- package/stores/form/FormStore.tsx +66 -0
- package/stores/form/FormUpdater.tsx +69 -0
- package/{providers/FormProvider → stores/form}/URLSearchParamsBuilder.tsx +11 -15
- package/stores/form/createFormStore.ts +225 -0
- package/stores/form/index.ts +10 -0
- package/stores/form/types.ts +138 -0
- package/stores/form/useFieldActions.ts +28 -0
- package/stores/form/useFieldController.ts +31 -0
- package/stores/form/useFieldValues.ts +12 -0
- package/stores/form/useTouchedFields.ts +8 -0
- package/stores/form/useValidation.ts +15 -0
- package/stores/form/useValidationActions.ts +24 -0
- package/stores/index.ts +1 -0
- package/_cjs/providers/FormProvider/FormProvider.d.ts +0 -12
- package/_cjs/providers/FormProvider/FormProvider.js +0 -40
- package/_cjs/providers/FormProvider/FormProvider.js.map +0 -1
- package/_cjs/providers/FormProvider/FormUpdater.d.ts +0 -5
- package/_cjs/providers/FormProvider/FormUpdater.js +0 -72
- package/_cjs/providers/FormProvider/FormUpdater.js.map +0 -1
- package/_cjs/providers/FormProvider/URLSearchParamsBuilder.js.map +0 -1
- package/_cjs/providers/FormProvider/index.d.ts +0 -3
- package/_cjs/providers/FormProvider/index.js.map +0 -1
- package/_cjs/providers/FormProvider/types.d.ts +0 -37
- package/_cjs/providers/FormProvider/types.js.map +0 -1
- package/_esm/providers/FormProvider/FormProvider.d.ts +0 -12
- package/_esm/providers/FormProvider/FormProvider.js +0 -36
- package/_esm/providers/FormProvider/FormProvider.js.map +0 -1
- package/_esm/providers/FormProvider/FormUpdater.d.ts +0 -5
- package/_esm/providers/FormProvider/FormUpdater.js +0 -68
- package/_esm/providers/FormProvider/FormUpdater.js.map +0 -1
- package/_esm/providers/FormProvider/URLSearchParamsBuilder.js.map +0 -1
- package/_esm/providers/FormProvider/index.d.ts +0 -3
- package/_esm/providers/FormProvider/index.js +0 -4
- package/_esm/providers/FormProvider/index.js.map +0 -1
- package/_esm/providers/FormProvider/types.d.ts +0 -37
- package/_esm/providers/FormProvider/types.js.map +0 -1
- package/providers/FormProvider/FormProvider.tsx +0 -55
- package/providers/FormProvider/FormUpdater.tsx +0 -86
- package/providers/FormProvider/index.ts +0 -3
- package/providers/FormProvider/types.ts +0 -44
- /package/_cjs/{providers/FormProvider → stores/form}/URLSearchParamsBuilder.d.ts +0 -0
- /package/_cjs/{providers/FormProvider → stores/form}/types.js +0 -0
- /package/_esm/{providers/FormProvider → stores/form}/URLSearchParamsBuilder.d.ts +0 -0
- /package/_esm/{providers/FormProvider → stores/form}/types.js +0 -0
package/stores/StoreProvider.tsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PropsWithChildren } from 'react';
|
|
2
2
|
import type { WidgetConfigProps } from '../types';
|
|
3
3
|
import { ChainOrderStoreProvider } from './chains';
|
|
4
|
+
import { FormStoreProvider } from './form';
|
|
4
5
|
import { HeaderStoreProvider } from './header';
|
|
5
6
|
import { RouteExecutionStoreProvider } from './routes';
|
|
6
7
|
import { SplitSubvariantStoreProvider } from './settings';
|
|
@@ -10,20 +11,22 @@ export const StoreProvider: React.FC<PropsWithChildren<WidgetConfigProps>> = ({
|
|
|
10
11
|
config,
|
|
11
12
|
}) => {
|
|
12
13
|
return (
|
|
13
|
-
<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
<
|
|
22
|
-
<
|
|
23
|
-
{
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
14
|
+
<FormStoreProvider>
|
|
15
|
+
<SplitSubvariantStoreProvider
|
|
16
|
+
state={
|
|
17
|
+
config.subvariant === 'split'
|
|
18
|
+
? config.subvariantOptions || 'swap'
|
|
19
|
+
: undefined
|
|
20
|
+
}
|
|
21
|
+
>
|
|
22
|
+
<HeaderStoreProvider namePrefix={config?.keyPrefix}>
|
|
23
|
+
<ChainOrderStoreProvider namePrefix={config?.keyPrefix}>
|
|
24
|
+
<RouteExecutionStoreProvider namePrefix={config?.keyPrefix}>
|
|
25
|
+
{children}
|
|
26
|
+
</RouteExecutionStoreProvider>
|
|
27
|
+
</ChainOrderStoreProvider>
|
|
28
|
+
</HeaderStoreProvider>
|
|
29
|
+
</SplitSubvariantStoreProvider>
|
|
30
|
+
</FormStoreProvider>
|
|
28
31
|
);
|
|
29
32
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { createContext, useContext, useEffect, useRef } from 'react';
|
|
2
|
-
import { useFormContext } from 'react-hook-form';
|
|
3
2
|
import type { StoreApi } from 'zustand';
|
|
4
3
|
import type { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
|
|
5
4
|
import { useChains } from '../../hooks';
|
|
6
|
-
import { FormKey } from '../../providers';
|
|
7
5
|
import type { PersistStoreProviderProps } from '../types';
|
|
8
6
|
import { createChainOrderStore } from './createChainOrderStore';
|
|
9
7
|
import type { ChainOrderState } from './types';
|
|
8
|
+
import { useFieldActions } from '../../stores';
|
|
10
9
|
|
|
11
10
|
export type ChainOrderStore = UseBoundStoreWithEqualityFn<
|
|
12
11
|
StoreApi<ChainOrderState>
|
|
@@ -22,7 +21,7 @@ export function ChainOrderStoreProvider({
|
|
|
22
21
|
}: PersistStoreProviderProps) {
|
|
23
22
|
const storeRef = useRef<ChainOrderStore>();
|
|
24
23
|
const { chains: filteredChains } = useChains();
|
|
25
|
-
const {
|
|
24
|
+
const { setFieldValue, getFieldValues } = useFieldActions();
|
|
26
25
|
|
|
27
26
|
if (!storeRef.current) {
|
|
28
27
|
storeRef.current = createChainOrderStore(props);
|
|
@@ -34,19 +33,19 @@ export function ChainOrderStoreProvider({
|
|
|
34
33
|
?.getState()
|
|
35
34
|
.initializeChains(filteredChains.map((chain) => chain.id));
|
|
36
35
|
if (chainOrder) {
|
|
37
|
-
const [fromChainValue, toChainValue] =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
const [fromChainValue, toChainValue] = getFieldValues(
|
|
37
|
+
'fromChain',
|
|
38
|
+
'toChain',
|
|
39
|
+
);
|
|
41
40
|
if (!fromChainValue) {
|
|
42
|
-
|
|
41
|
+
setFieldValue('fromChain', chainOrder[0]);
|
|
43
42
|
}
|
|
44
43
|
if (!toChainValue) {
|
|
45
|
-
|
|
44
|
+
setFieldValue('toChain', chainOrder[0]);
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
|
-
}, [filteredChains,
|
|
48
|
+
}, [filteredChains, getFieldValues, setFieldValue]);
|
|
50
49
|
|
|
51
50
|
return (
|
|
52
51
|
<ChainOrderStoreContext.Provider value={storeRef.current}>
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { PropsWithChildren } from 'react';
|
|
2
|
+
import { createContext, useContext, useEffect, useMemo, useRef } from 'react';
|
|
3
|
+
import { shallow } from 'zustand/shallow';
|
|
4
|
+
import { useWidgetConfig } from '../../providers';
|
|
5
|
+
import type { FormStoreStore, FormValuesState } from './types';
|
|
6
|
+
import { createFormStore, formDefaultValues } from './createFormStore';
|
|
7
|
+
import { FormUpdater } from './FormUpdater';
|
|
8
|
+
|
|
9
|
+
export const FormStoreContext = createContext<FormStoreStore | null>(null);
|
|
10
|
+
|
|
11
|
+
export const FormStoreProvider: React.FC<PropsWithChildren> = ({
|
|
12
|
+
children,
|
|
13
|
+
}) => {
|
|
14
|
+
const storeRef = useRef<FormStoreStore>();
|
|
15
|
+
|
|
16
|
+
if (!storeRef.current) {
|
|
17
|
+
storeRef.current = createFormStore();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const { fromChain, fromToken, fromAmount, toChain, toToken, toAddress } =
|
|
21
|
+
useWidgetConfig();
|
|
22
|
+
|
|
23
|
+
const defaultValues = useMemo(
|
|
24
|
+
() => ({
|
|
25
|
+
...formDefaultValues,
|
|
26
|
+
fromChain,
|
|
27
|
+
fromToken,
|
|
28
|
+
toChain,
|
|
29
|
+
toToken,
|
|
30
|
+
fromAmount:
|
|
31
|
+
(typeof fromAmount === 'number'
|
|
32
|
+
? fromAmount?.toPrecision()
|
|
33
|
+
: fromAmount) || formDefaultValues.fromAmount,
|
|
34
|
+
toAddress: toAddress || formDefaultValues.toAddress,
|
|
35
|
+
}),
|
|
36
|
+
[fromAmount, fromChain, fromToken, toAddress, toChain, toToken],
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (storeRef.current) {
|
|
41
|
+
storeRef.current.getState().setDefaultValues(defaultValues);
|
|
42
|
+
}
|
|
43
|
+
}, [defaultValues, storeRef]);
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<FormStoreContext.Provider value={storeRef.current}>
|
|
47
|
+
<FormUpdater defaultValues={defaultValues} />
|
|
48
|
+
{children}
|
|
49
|
+
</FormStoreContext.Provider>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const useFormStore = (
|
|
54
|
+
selector: (store: FormValuesState) => any,
|
|
55
|
+
equalityFunction = shallow,
|
|
56
|
+
) => {
|
|
57
|
+
const useStore = useContext(FormStoreContext);
|
|
58
|
+
|
|
59
|
+
if (!useStore) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
'You forgot to wrap your component in <FormStoreProvider>.',
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return useStore(selector, equalityFunction);
|
|
66
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { useEffect, useRef } from 'react';
|
|
3
|
+
import type { FormFieldNames } from './types';
|
|
4
|
+
import { useAccount } from '../../hooks';
|
|
5
|
+
import { useFieldActions } from './useFieldActions';
|
|
6
|
+
import { isItemAllowed, useWidgetConfig } from '../../providers';
|
|
7
|
+
import type { DefaultValues } from './types';
|
|
8
|
+
import { FormKey } from './types';
|
|
9
|
+
|
|
10
|
+
export const FormUpdater: React.FC<{
|
|
11
|
+
defaultValues: Partial<DefaultValues>;
|
|
12
|
+
}> = ({ defaultValues }) => {
|
|
13
|
+
const { fromChain, toChain, chains } = useWidgetConfig();
|
|
14
|
+
const { account } = useAccount();
|
|
15
|
+
const { isTouched, resetField, setFieldValue, getFieldValues } =
|
|
16
|
+
useFieldActions();
|
|
17
|
+
const previousDefaultValues = useRef(defaultValues);
|
|
18
|
+
|
|
19
|
+
// Set wallet chain as default if no chains are provided by config and if they were not changed during widget usage
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const chainAllowed =
|
|
22
|
+
account.chainId && isItemAllowed(account.chainId, chains);
|
|
23
|
+
if (!account.isConnected || !account.chainId || !chainAllowed) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!fromChain && !isTouched('fromChain') && !isTouched('fromToken')) {
|
|
28
|
+
resetField('fromChain', { defaultValue: account.chainId });
|
|
29
|
+
setFieldValue('fromToken', '');
|
|
30
|
+
if (isTouched('fromAmount')) {
|
|
31
|
+
setFieldValue('fromAmount', '');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!toChain && !isTouched('toChain') && !isTouched('toToken')) {
|
|
35
|
+
resetField('toChain', { defaultValue: account.chainId });
|
|
36
|
+
setFieldValue('toToken', '');
|
|
37
|
+
}
|
|
38
|
+
}, [
|
|
39
|
+
account.chainId,
|
|
40
|
+
account.isConnected,
|
|
41
|
+
chains,
|
|
42
|
+
fromChain,
|
|
43
|
+
toChain,
|
|
44
|
+
isTouched,
|
|
45
|
+
resetField,
|
|
46
|
+
setFieldValue,
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
// Makes widget config options reactive to changes
|
|
50
|
+
// should update userValues when defaultValues updates and includes additional logic for chains
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
(Object.keys(defaultValues) as FormFieldNames[]).forEach((key) => {
|
|
53
|
+
if (previousDefaultValues.current[key] !== defaultValues[key]) {
|
|
54
|
+
const value =
|
|
55
|
+
defaultValues[key] ||
|
|
56
|
+
// set the chain to the current user one if it is not present in the config
|
|
57
|
+
(key === FormKey.FromChain || key === FormKey.ToChain
|
|
58
|
+
? account.chainId || ''
|
|
59
|
+
: '');
|
|
60
|
+
setFieldValue(key, value);
|
|
61
|
+
resetField(key, { defaultValue: value });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
previousDefaultValues.current = defaultValues;
|
|
65
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
66
|
+
}, [defaultValues, getFieldValues, resetField, setFieldValue]);
|
|
67
|
+
|
|
68
|
+
return null;
|
|
69
|
+
};
|
|
@@ -1,24 +1,20 @@
|
|
|
1
1
|
import { useEffect } from 'react';
|
|
2
|
-
import { useFormState, useWatch } from 'react-hook-form';
|
|
3
2
|
import { useLocation } from 'react-router-dom';
|
|
4
|
-
import {
|
|
3
|
+
import type { FormFieldNames } from '../index';
|
|
4
|
+
import { useFieldValues, useTouchedFields } from '../index';
|
|
5
5
|
|
|
6
|
-
const formValueKeys = [
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
const formValueKeys: FormFieldNames[] = [
|
|
7
|
+
'fromAmount',
|
|
8
|
+
'fromChain',
|
|
9
|
+
'fromToken',
|
|
10
|
+
'toAddress',
|
|
11
|
+
'toChain',
|
|
12
|
+
'toToken',
|
|
13
13
|
];
|
|
14
|
-
|
|
15
14
|
export const URLSearchParamsBuilder = () => {
|
|
16
15
|
const { pathname } = useLocation();
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
touchedFields: { ...touchedFields },
|
|
20
|
-
} = useFormState();
|
|
21
|
-
const values = useWatch({ name: formValueKeys });
|
|
16
|
+
const touchedFields = useTouchedFields();
|
|
17
|
+
const values = useFieldValues(...formValueKeys);
|
|
22
18
|
|
|
23
19
|
useEffect(() => {
|
|
24
20
|
const url = new URL(window.location as any);
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { createWithEqualityFn } from 'zustand/traditional';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
DefaultValues,
|
|
5
|
+
FormFieldNames,
|
|
6
|
+
GenericFormValue,
|
|
7
|
+
FormValueControl,
|
|
8
|
+
FormValues,
|
|
9
|
+
FormValuesState,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
export const formDefaultValues: DefaultValues = {
|
|
13
|
+
fromAmount: '',
|
|
14
|
+
toAddress: '',
|
|
15
|
+
tokenSearchFilter: '',
|
|
16
|
+
contractOutputsToken: '',
|
|
17
|
+
toContractAddress: '',
|
|
18
|
+
toContractCallData: '',
|
|
19
|
+
toContractGasLimit: '',
|
|
20
|
+
toAmount: '',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const defaultValueToFormValue = (
|
|
24
|
+
value: GenericFormValue,
|
|
25
|
+
): FormValueControl<GenericFormValue> => ({
|
|
26
|
+
isTouched: false,
|
|
27
|
+
isDirty: false,
|
|
28
|
+
value,
|
|
29
|
+
});
|
|
30
|
+
const valuesToFormValues = (defaultValues: DefaultValues): FormValues => {
|
|
31
|
+
return (Object.keys(defaultValues) as FormFieldNames[]).reduce(
|
|
32
|
+
(accum, key) => {
|
|
33
|
+
return { ...accum, [key]: defaultValueToFormValue(defaultValues[key]) };
|
|
34
|
+
},
|
|
35
|
+
{},
|
|
36
|
+
) as FormValues;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const isString = (str: any) => typeof str === 'string' || str instanceof String;
|
|
40
|
+
|
|
41
|
+
const getUpdatedTouchedFields = (userValues: FormValues) => {
|
|
42
|
+
return Object.keys(userValues).reduce(
|
|
43
|
+
(accum, key) =>
|
|
44
|
+
userValues[key as FormFieldNames]?.isTouched
|
|
45
|
+
? { ...accum, [key]: true }
|
|
46
|
+
: accum,
|
|
47
|
+
{},
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
const mergeDefaultFormValues = (
|
|
51
|
+
userValues: FormValues,
|
|
52
|
+
defaultValues: FormValues,
|
|
53
|
+
) =>
|
|
54
|
+
(Object.keys(defaultValues) as FormFieldNames[]).reduce<FormValues>(
|
|
55
|
+
(accum, key) => {
|
|
56
|
+
const formValue = {
|
|
57
|
+
isTouched: !!(
|
|
58
|
+
userValues[key]?.isTouched || defaultValues[key]?.isTouched
|
|
59
|
+
),
|
|
60
|
+
isDirty: !!(userValues[key]?.isDirty || defaultValues[key]?.isTouched),
|
|
61
|
+
value:
|
|
62
|
+
userValues[key]?.value || Number.isFinite(userValues[key]?.value)
|
|
63
|
+
? userValues[key]?.value
|
|
64
|
+
: defaultValues[key]?.value,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
...accum,
|
|
69
|
+
[key]: formValue,
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
{ ...valuesToFormValues(formDefaultValues) },
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
export const createFormStore = () =>
|
|
76
|
+
createWithEqualityFn<FormValuesState>(
|
|
77
|
+
(set, get) => ({
|
|
78
|
+
defaultValues: valuesToFormValues(formDefaultValues),
|
|
79
|
+
userValues: valuesToFormValues(formDefaultValues),
|
|
80
|
+
touchedFields: {},
|
|
81
|
+
setDefaultValues: (defaultValue) => {
|
|
82
|
+
const defaultFormValues = valuesToFormValues(defaultValue);
|
|
83
|
+
set((state) => ({
|
|
84
|
+
defaultValues: defaultFormValues,
|
|
85
|
+
userValues: mergeDefaultFormValues(
|
|
86
|
+
state.userValues,
|
|
87
|
+
defaultFormValues,
|
|
88
|
+
),
|
|
89
|
+
}));
|
|
90
|
+
},
|
|
91
|
+
isTouched: (fieldName: FormFieldNames) =>
|
|
92
|
+
!!get().userValues[fieldName]?.isTouched,
|
|
93
|
+
setAsTouched: (fieldName: FormFieldNames) => {
|
|
94
|
+
const userValues = {
|
|
95
|
+
...get().userValues,
|
|
96
|
+
[fieldName]: {
|
|
97
|
+
...get().userValues[fieldName],
|
|
98
|
+
isTouched: true,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const touchedFields = getUpdatedTouchedFields(userValues);
|
|
103
|
+
|
|
104
|
+
set(() => ({
|
|
105
|
+
userValues,
|
|
106
|
+
touchedFields,
|
|
107
|
+
}));
|
|
108
|
+
},
|
|
109
|
+
resetField: (fieldName, { defaultValue } = {}) => {
|
|
110
|
+
if (defaultValue) {
|
|
111
|
+
const fieldValues = {
|
|
112
|
+
...get().defaultValues[fieldName],
|
|
113
|
+
value: defaultValue,
|
|
114
|
+
};
|
|
115
|
+
const defaultValues = {
|
|
116
|
+
...get().defaultValues,
|
|
117
|
+
[fieldName]: { ...fieldValues },
|
|
118
|
+
};
|
|
119
|
+
const userValues = {
|
|
120
|
+
...get().userValues,
|
|
121
|
+
[fieldName]: { ...fieldValues },
|
|
122
|
+
};
|
|
123
|
+
const touchedFields = getUpdatedTouchedFields(userValues);
|
|
124
|
+
|
|
125
|
+
set(() => {
|
|
126
|
+
return {
|
|
127
|
+
defaultValues,
|
|
128
|
+
userValues,
|
|
129
|
+
touchedFields,
|
|
130
|
+
};
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
const userValues = {
|
|
134
|
+
...get().userValues,
|
|
135
|
+
[fieldName]: { ...get().defaultValues[fieldName] },
|
|
136
|
+
};
|
|
137
|
+
const touchedFields = getUpdatedTouchedFields(userValues);
|
|
138
|
+
|
|
139
|
+
set(() => ({
|
|
140
|
+
userValues,
|
|
141
|
+
touchedFields,
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
setFieldValue: (fieldName, value, { isDirty, isTouched } = {}) => {
|
|
146
|
+
const userValues = {
|
|
147
|
+
...get().userValues,
|
|
148
|
+
[fieldName]: {
|
|
149
|
+
value,
|
|
150
|
+
isDirty:
|
|
151
|
+
isDirty === undefined
|
|
152
|
+
? get().userValues[fieldName]?.isDirty
|
|
153
|
+
: isDirty,
|
|
154
|
+
isTouched:
|
|
155
|
+
isTouched === undefined
|
|
156
|
+
? get().userValues[fieldName]?.isTouched
|
|
157
|
+
: isTouched,
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const touchedFields = getUpdatedTouchedFields(userValues);
|
|
162
|
+
|
|
163
|
+
set(() => ({
|
|
164
|
+
userValues,
|
|
165
|
+
touchedFields,
|
|
166
|
+
}));
|
|
167
|
+
},
|
|
168
|
+
getFieldValues: (...names) =>
|
|
169
|
+
names.map((name) => get().userValues[name]?.value),
|
|
170
|
+
isValid: true,
|
|
171
|
+
isValidating: false,
|
|
172
|
+
errors: {},
|
|
173
|
+
validation: {},
|
|
174
|
+
addFieldValidation: (name, validationFn) => {
|
|
175
|
+
set((state) => ({
|
|
176
|
+
validation: {
|
|
177
|
+
...state.validation,
|
|
178
|
+
[name]: validationFn,
|
|
179
|
+
},
|
|
180
|
+
}));
|
|
181
|
+
},
|
|
182
|
+
triggerFieldValidation: async (name) => {
|
|
183
|
+
try {
|
|
184
|
+
let valid = true;
|
|
185
|
+
set(() => ({ isValid: false, isValidating: true }));
|
|
186
|
+
|
|
187
|
+
const validationFn = get().validation[name];
|
|
188
|
+
|
|
189
|
+
if (validationFn) {
|
|
190
|
+
const result = await validationFn(get().userValues?.[name]?.value);
|
|
191
|
+
if (isString(result)) {
|
|
192
|
+
valid = false;
|
|
193
|
+
set((state) => ({
|
|
194
|
+
errors: {
|
|
195
|
+
...state.errors,
|
|
196
|
+
[name]: result,
|
|
197
|
+
},
|
|
198
|
+
}));
|
|
199
|
+
} else {
|
|
200
|
+
valid = result as boolean;
|
|
201
|
+
if (valid) {
|
|
202
|
+
get().clearErrors(name);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
set(() => ({ isValid: valid, isValidating: false }));
|
|
208
|
+
return valid;
|
|
209
|
+
} catch (err) {
|
|
210
|
+
set(() => ({ isValidating: false }));
|
|
211
|
+
throw err;
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
clearErrors: (name) => {
|
|
215
|
+
const newErrors = { ...get().errors };
|
|
216
|
+
|
|
217
|
+
delete newErrors[name];
|
|
218
|
+
|
|
219
|
+
set(() => ({
|
|
220
|
+
errors: newErrors,
|
|
221
|
+
}));
|
|
222
|
+
},
|
|
223
|
+
}),
|
|
224
|
+
Object.is,
|
|
225
|
+
);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './createFormStore';
|
|
2
|
+
export * from './FormStore';
|
|
3
|
+
export * from './types';
|
|
4
|
+
export * from './useFieldActions';
|
|
5
|
+
export * from './useFieldController';
|
|
6
|
+
export * from './useFieldValues';
|
|
7
|
+
export * from './useTouchedFields';
|
|
8
|
+
export * from './useValidation';
|
|
9
|
+
export * from './useValidationActions';
|
|
10
|
+
export * from './URLSearchParamsBuilder';
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { UseBoundStoreWithEqualityFn } from 'zustand/traditional';
|
|
2
|
+
import type { StoreApi } from 'zustand';
|
|
3
|
+
|
|
4
|
+
export interface DefaultValues {
|
|
5
|
+
toAddress: string;
|
|
6
|
+
tokenSearchFilter: string;
|
|
7
|
+
contractOutputsToken: string;
|
|
8
|
+
toContractAddress: string;
|
|
9
|
+
toContractCallData: string;
|
|
10
|
+
toContractGasLimit: string;
|
|
11
|
+
toAmount: string;
|
|
12
|
+
fromAmount: string;
|
|
13
|
+
toChain?: number;
|
|
14
|
+
fromChain?: number;
|
|
15
|
+
fromToken?: string;
|
|
16
|
+
toToken?: string | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type GenericFormValue = string | number | undefined;
|
|
20
|
+
export interface FormValueControl<T> {
|
|
21
|
+
isTouched: boolean;
|
|
22
|
+
isDirty: boolean;
|
|
23
|
+
value: T;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FormValues {
|
|
27
|
+
toAddress: FormValueControl<string>;
|
|
28
|
+
tokenSearchFilter: FormValueControl<string>;
|
|
29
|
+
contractOutputsToken: FormValueControl<string>;
|
|
30
|
+
toContractAddress: FormValueControl<string>;
|
|
31
|
+
toContractCallData: FormValueControl<string>;
|
|
32
|
+
toContractGasLimit: FormValueControl<string>;
|
|
33
|
+
toAmount: FormValueControl<string>;
|
|
34
|
+
fromAmount: FormValueControl<string>;
|
|
35
|
+
fromToken?: FormValueControl<string | undefined>;
|
|
36
|
+
toToken?: FormValueControl<string | undefined>;
|
|
37
|
+
toChain?: FormValueControl<number | undefined>;
|
|
38
|
+
fromChain?: FormValueControl<number | undefined>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type FormFieldNames = keyof FormValues;
|
|
42
|
+
|
|
43
|
+
export type TouchedFields = { [key in FormFieldNames]?: boolean };
|
|
44
|
+
|
|
45
|
+
type ValidationFn = (value: any) => Promise<boolean | string>;
|
|
46
|
+
export interface ValidationProps {
|
|
47
|
+
isValid: boolean;
|
|
48
|
+
isValidating: boolean;
|
|
49
|
+
errors: {
|
|
50
|
+
[key in FormFieldNames]?: string;
|
|
51
|
+
};
|
|
52
|
+
validation: {
|
|
53
|
+
[key in FormFieldNames]?: ValidationFn;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface ValidationActions {
|
|
58
|
+
addFieldValidation: (
|
|
59
|
+
name: FormFieldNames,
|
|
60
|
+
validationFn: ValidationFn,
|
|
61
|
+
) => void;
|
|
62
|
+
triggerFieldValidation: (name: FormFieldNames) => Promise<boolean>;
|
|
63
|
+
clearErrors: (name: FormFieldNames) => void;
|
|
64
|
+
}
|
|
65
|
+
export type ValidationActionNames = keyof ValidationActions;
|
|
66
|
+
export type ValidationActionFunctions = Array<
|
|
67
|
+
ValidationActions[ValidationActionNames]
|
|
68
|
+
>;
|
|
69
|
+
|
|
70
|
+
export interface FormProps {
|
|
71
|
+
defaultValues: FormValues;
|
|
72
|
+
userValues: FormValues;
|
|
73
|
+
touchedFields: { [key in FormFieldNames]?: boolean };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface ResetOptions {
|
|
77
|
+
defaultValue?: GenericFormValue;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface FormActions {
|
|
81
|
+
setDefaultValues: (formValues: DefaultValues) => void;
|
|
82
|
+
isTouched: (fieldName: FormFieldNames) => boolean;
|
|
83
|
+
setAsTouched: (fieldName: FormFieldNames) => void;
|
|
84
|
+
resetField: (fieldName: FormFieldNames, resetOptions?: ResetOptions) => void;
|
|
85
|
+
setFieldValue: (
|
|
86
|
+
fieldName: FormFieldNames,
|
|
87
|
+
value: GenericFormValue,
|
|
88
|
+
options?: SetOptions,
|
|
89
|
+
) => void;
|
|
90
|
+
getFieldValues: (...names: FormFieldNames[]) => Array<any>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export type FormActionNames = keyof FormActions;
|
|
94
|
+
export type FormActionFunctions = Array<FormActions[FormActionNames]>;
|
|
95
|
+
|
|
96
|
+
export type FormValuesState = FormProps &
|
|
97
|
+
FormActions &
|
|
98
|
+
ValidationProps &
|
|
99
|
+
ValidationActions;
|
|
100
|
+
|
|
101
|
+
export type FormStoreStore = UseBoundStoreWithEqualityFn<
|
|
102
|
+
StoreApi<FormValuesState>
|
|
103
|
+
>;
|
|
104
|
+
|
|
105
|
+
interface SetOptions {
|
|
106
|
+
isDirty?: boolean;
|
|
107
|
+
isTouched?: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export enum FormKey {
|
|
111
|
+
ContractOutputsToken = 'contractOutputsToken',
|
|
112
|
+
FromAmount = 'fromAmount',
|
|
113
|
+
FromChain = 'fromChain',
|
|
114
|
+
FromToken = 'fromToken',
|
|
115
|
+
ToAddress = 'toAddress',
|
|
116
|
+
ToAmount = 'toAmount',
|
|
117
|
+
ToChain = 'toChain',
|
|
118
|
+
ToContractAddress = 'toContractAddress',
|
|
119
|
+
ToContractCallData = 'toContractCallData',
|
|
120
|
+
ToContractGasLimit = 'ToContractGasLimit',
|
|
121
|
+
ToToken = 'toToken',
|
|
122
|
+
TokenSearchFilter = 'tokenSearchFilter',
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type FormType = 'from' | 'to';
|
|
126
|
+
|
|
127
|
+
export interface FormTypeProps {
|
|
128
|
+
formType: FormType;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export const FormKeyHelper = {
|
|
132
|
+
getChainKey: (formType: FormType): 'fromChain' | 'toChain' =>
|
|
133
|
+
`${formType}Chain`,
|
|
134
|
+
getTokenKey: (formType: FormType): 'fromToken' | 'toToken' =>
|
|
135
|
+
`${formType}Token`,
|
|
136
|
+
getAmountKey: (formType: FormType): 'fromAmount' | 'toAmount' =>
|
|
137
|
+
`${formType}Amount`,
|
|
138
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { shallow } from 'zustand/shallow';
|
|
2
|
+
import type {
|
|
3
|
+
FormActions,
|
|
4
|
+
FormActionNames,
|
|
5
|
+
FormActionFunctions,
|
|
6
|
+
} from './types';
|
|
7
|
+
import { useFormStore } from './FormStore';
|
|
8
|
+
|
|
9
|
+
const actionFunctions: FormActionNames[] = [
|
|
10
|
+
'setDefaultValues',
|
|
11
|
+
'isTouched',
|
|
12
|
+
'setAsTouched',
|
|
13
|
+
'resetField',
|
|
14
|
+
'setFieldValue',
|
|
15
|
+
'getFieldValues',
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
export const useFieldActions = () => {
|
|
19
|
+
const actions: FormActionFunctions = useFormStore(
|
|
20
|
+
(store) => actionFunctions.map((actionName) => store[actionName]),
|
|
21
|
+
shallow,
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
return actions.reduce(
|
|
25
|
+
(accum, actionName, i) => ({ ...accum, [actionFunctions[i]]: actionName }),
|
|
26
|
+
{},
|
|
27
|
+
) as FormActions;
|
|
28
|
+
};
|