diff --git a/src/pages/MyEarnings/MultipleChainSelectV2/ApplyButton.tsx b/src/pages/MyEarnings/MultipleChainSelectV2/ApplyButton.tsx new file mode 100644 index 0000000000..4e30dd2aa0 --- /dev/null +++ b/src/pages/MyEarnings/MultipleChainSelectV2/ApplyButton.tsx @@ -0,0 +1,54 @@ +import { Trans } from '@lingui/macro' +import { MouseEventHandler } from 'react' +import { Flex } from 'rebass' + +import { ButtonPrimary } from 'components/Button' +import useTheme from 'hooks/useTheme' + +type ApplyButtonProps = { + disabled: boolean + onClick: MouseEventHandler + numOfChains: number +} + +export const ApplyButton: React.FC = ({ disabled, onClick, numOfChains }) => { + const theme = useTheme() + return ( + + + View Selected Chains + + {numOfChains ? String(numOfChains).padStart(2, '0') : 0} + + + + ) +} diff --git a/src/pages/MyEarnings/MultipleChainSelectV2/PopoverBody.tsx b/src/pages/MyEarnings/MultipleChainSelectV2/PopoverBody.tsx deleted file mode 100644 index ee24457ee5..0000000000 --- a/src/pages/MyEarnings/MultipleChainSelectV2/PopoverBody.tsx +++ /dev/null @@ -1,257 +0,0 @@ -import { Trans } from '@lingui/macro' -import { rgba } from 'polished' -import { MouseEventHandler, useEffect, useRef, useState } from 'react' -import { Box, Flex, Text } from 'rebass' -import styled from 'styled-components' - -import { ReactComponent as LogoKyber } from 'assets/svg/logo_kyber.svg' -import { ButtonPrimary } from 'components/Button' -import Checkbox from 'components/CheckBox' -import { MouseoverTooltip } from 'components/Tooltip' -import { NETWORKS_INFO } from 'constants/networks' -import useChainsConfig from 'hooks/useChainsConfig' -import useTheme from 'hooks/useTheme' - -import { MultipleChainSelectProps, StyledLogo } from '.' - -const ChainListWrapper = styled.div` - display: flex; - flex-direction: column; - gap: 12px; - max-height: 180px; - overflow: auto; - - /* width */ - ::-webkit-scrollbar { - display: unset; - width: 8px; - border-radius: 999px; - } - - /* Track */ - ::-webkit-scrollbar-track { - background: transparent; - border-radius: 999px; - } - - /* Handle */ - ::-webkit-scrollbar-thumb { - background: ${({ theme }) => rgba(theme.subText, 0.4)}; - border-radius: 999px; - } -` - -type ApplyButtonProps = { - disabled: boolean - onClick: MouseEventHandler - numOfChains: number -} - -export const ApplyButton: React.FC = ({ disabled, onClick, numOfChains }) => { - const theme = useTheme() - return ( - - - View Selected Chains - - {numOfChains ? String(numOfChains).padStart(2, '0') : 0} - - - - ) -} - -const PopoverBody: React.FC void }> = ({ - onClose, - comingSoonList = [], - chainIds, - selectedChainIds, - handleChangeChains, - onTracking, - menuStyle, -}) => { - const theme = useTheme() - const selectAllRef = useRef(null) - - const { activeChains } = useChainsConfig() - const selectedChains = selectedChainIds.filter(item => !comingSoonList.includes(item)) - - const [localSelectedChains, setLocalSelectedChains] = useState(() => selectedChains) - - const networkList = chainIds.filter( - item => !comingSoonList.includes(item) && activeChains.some(e => e.chainId === item), - ) - - const isAllSelected = localSelectedChains.length === networkList.length - - useEffect(() => { - setLocalSelectedChains(selectedChains) - // eslint-disable-next-line - }, [selectedChains.length]) - - useEffect(() => { - if (!selectAllRef.current) { - return - } - - const indeterminate = 0 < localSelectedChains.length && localSelectedChains.length < networkList.length - selectAllRef.current.indeterminate = indeterminate - }, [localSelectedChains, networkList.length]) - - const allNetworks = [...networkList, ...comingSoonList] - - const onChangeChain = () => { - if (isAllSelected) { - setLocalSelectedChains([]) - } else { - onTracking?.() - setLocalSelectedChains(networkList) - } - } - - return ( - - - - - - - - - - All Chains - - - - - {allNetworks.map((network, i) => { - const config = NETWORKS_INFO[network] - - const isComingSoon = comingSoonList.includes(network) - const isSelected = isComingSoon ? false : localSelectedChains.includes(network) - - const handleClick = () => { - if (isComingSoon) return - if (isSelected) { - setLocalSelectedChains(localSelectedChains.filter(chain => chain !== network)) - } else { - setLocalSelectedChains([...localSelectedChains, network]) - } - } - - return ( - - - - - - - - {config.name} - - - - ) - })} - - - - - { - handleChangeChains(localSelectedChains) - onClose() - }} - numOfChains={localSelectedChains.length} - /> - - ) -} - -export default PopoverBody diff --git a/src/pages/MyEarnings/MultipleChainSelectV2/SelectButton.tsx b/src/pages/MyEarnings/MultipleChainSelectV2/SelectButton.tsx index d96d28b34a..9c64e2af52 100644 --- a/src/pages/MyEarnings/MultipleChainSelectV2/SelectButton.tsx +++ b/src/pages/MyEarnings/MultipleChainSelectV2/SelectButton.tsx @@ -23,17 +23,8 @@ const Label = styled.span<{ labelColor?: string }>` overflow: hidden; text-overflow: ellipsis; ` -type Props = { - onClick: () => void -} & MultipleChainSelectProps -const SelectButton: React.FC = ({ - onClick, - selectedChainIds, - chainIds, - activeRender, - activeStyle, - labelColor, -}) => { +type Props = MultipleChainSelectProps +const SelectButton: React.FC = ({ selectedChainIds, chainIds, activeRender, activeStyle, labelColor }) => { const theme = useTheme() const renderButtonBody = () => { @@ -88,7 +79,6 @@ const SelectButton: React.FC = ({ cursor: 'pointer', ...activeStyle, }} - onClick={onClick} > {activeRender ? activeRender(renderButtonBody()) : renderButtonBody()} diff --git a/src/pages/MyEarnings/MultipleChainSelectV2/index.tsx b/src/pages/MyEarnings/MultipleChainSelectV2/index.tsx index a098609eec..3b9b3f6bc9 100644 --- a/src/pages/MyEarnings/MultipleChainSelectV2/index.tsx +++ b/src/pages/MyEarnings/MultipleChainSelectV2/index.tsx @@ -1,16 +1,19 @@ import { ChainId } from '@kyberswap/ks-sdk-core' -import { ReactNode, useRef, useState } from 'react' +import { Trans } from '@lingui/macro' +import { rgba } from 'polished' +import { ReactNode, useState } from 'react' import { Box, Flex, Text } from 'rebass' import styled, { CSSProperties } from 'styled-components' +import { ReactComponent as LogoKyber } from 'assets/svg/logo_kyber.svg' import Checkbox from 'components/CheckBox' import Select from 'components/SelectV2' import { MouseoverTooltip } from 'components/Tooltip' import { NETWORKS_INFO } from 'constants/networks' -import { useOnClickOutside } from 'hooks/useOnClickOutside' +import useChainsConfig from 'hooks/useChainsConfig' import useTheme from 'hooks/useTheme' -import { ApplyButton } from './PopoverBody' +import { ApplyButton } from './ApplyButton' import SelectButton from './SelectButton' export const StyledLogo = styled.img` @@ -31,25 +34,55 @@ export type MultipleChainSelectProps = { labelColor?: string activeRender?: (node: ReactNode) => ReactNode } -const MultipleChainSelect: React.FC = ({ className, style, ...props }) => { - const [expanded, setExpanded] = useState(false) - const ref = useRef(null) - const collapse = () => { - setExpanded(false) +const ChainWrapper = styled.div` + max-height: 180px; + overflow-y: scroll; + /* width */ + ::-webkit-scrollbar { + display: unset; + width: 8px; + border-radius: 999px; + } + + /* Track */ + ::-webkit-scrollbar-track { + background: transparent; + border-radius: 999px; } - useOnClickOutside(ref, expanded ? collapse : undefined) - const { comingSoonList = [], selectedChainIds = [], handleChangeChains } = props - const options = props.chainIds.map(id => ({ value: id, label: id })) + /* Handle */ + ::-webkit-scrollbar-thumb { + background: ${({ theme }) => rgba(theme.subText, 0.4)}; + border-radius: 999px; + } +` +const MultipleChainSelect: React.FC = ({ className, style, ...props }) => { + const { comingSoonList = [], selectedChainIds = [], handleChangeChains, chainIds = [], onTracking } = props + const options = chainIds.map(id => ({ value: id, label: id })) const theme = useTheme() const selectedChains = selectedChainIds.filter(item => !comingSoonList.includes(item)) const [localSelectedChains, setLocalSelectedChains] = useState(() => selectedChains) + const { activeChains } = useChainsConfig() + const networkList = chainIds.filter( + item => !comingSoonList.includes(item) && activeChains.some(e => e.chainId === item), + ) + const isAllSelected = localSelectedChains.length === networkList.length + + const onChangeChain = () => { + if (isAllSelected) { + setLocalSelectedChains([]) + } else { + onTracking?.() + setLocalSelectedChains(networkList) + } + } + return (