diff --git a/apps/console-lite/src/app/components/deal-ticket/deal-ticket-size.tsx b/apps/console-lite/src/app/components/deal-ticket/deal-ticket-size.tsx index 9f070f03b..7ad66a31c 100644 --- a/apps/console-lite/src/app/components/deal-ticket/deal-ticket-size.tsx +++ b/apps/console-lite/src/app/components/deal-ticket/deal-ticket-size.tsx @@ -7,13 +7,13 @@ import { SliderThumb, SliderTrack, SliderRange, - Input, FormGroup, Icon, Tooltip, } from '@vegaprotocol/ui-toolkit'; import { BigNumber } from 'bignumber.js'; import { DealTicketEstimates } from './deal-ticket-estimates'; +import { InputSetter } from '../input-setter'; import * as constants from './constants'; interface DealTicketSizeProps { @@ -61,7 +61,6 @@ export const DealTicketSize = ({ }: DealTicketSizeProps) => { const sizeRatios = [0, 25, 50, 75, 100]; const [inputValue, setInputValue] = useState(value); - const [isInputVisible, setIsInputVisible] = useState(false); const onInputValueChange = useCallback( (event: React.ChangeEvent) => { @@ -84,30 +83,21 @@ export const DealTicketSize = ({ const onButtonValueChange = useCallback( (size: number) => { - if (isInputVisible) { - setIsInputVisible(false); - } const newVal = new BigNumber(size) .decimalPlaces(positionDecimalPlaces) .toNumber(); onValueChange([newVal]); setInputValue(newVal); }, - [isInputVisible, onValueChange, positionDecimalPlaces] + [onValueChange, positionDecimalPlaces] ); - const toggleInput = useCallback(() => { - setIsInputVisible(!isInputVisible); - }, [isInputVisible]); - - const onInputEnter = useCallback( - (event: React.KeyboardEvent) => { - if (event.key === 'Enter') { - event.stopPropagation(); - toggleInput(); - } + const onSliderValueChange = useCallback( + (value: number[]) => { + setInputValue(value[0]); + onValueChange(value); }, - [toggleInput] + [onValueChange] ); return max === 0 ? ( @@ -121,7 +111,7 @@ export const DealTicketSize = ({ - {isInputVisible ? ( -
- - -
- ) : ( - - )} + diff --git a/apps/console-lite/src/app/components/input-setter/index.ts b/apps/console-lite/src/app/components/input-setter/index.ts new file mode 100644 index 000000000..158a6a2e7 --- /dev/null +++ b/apps/console-lite/src/app/components/input-setter/index.ts @@ -0,0 +1 @@ +export * from './input-setter'; diff --git a/apps/console-lite/src/app/components/input-setter/input-setter.spec.tsx b/apps/console-lite/src/app/components/input-setter/input-setter.spec.tsx new file mode 100644 index 000000000..da9e2f473 --- /dev/null +++ b/apps/console-lite/src/app/components/input-setter/input-setter.spec.tsx @@ -0,0 +1,64 @@ +import React from 'react'; +import { render, waitFor, fireEvent } from '@testing-library/react'; +import { InputSetter } from './index'; + +describe('InputSetter Component', () => { + it('should show the correct value and visibility based on props', async () => { + const value = 'Hello'; + const isInputVisible = true; + const { getByRole } = await render( + false} + /> + ); + const input = getByRole('spinbutton'); + const btn = getByRole('button'); + expect(input).toHaveAttribute('value', value); + expect(btn).toBeTruthy(); + }); + + it('should toggle the visibility of the input when the button is clicked', async () => { + const value = 'Hello'; + const isInputVisible = true; + const { getByRole } = await render( + false} + /> + ); + const btn = getByRole('button'); + fireEvent.click(btn); + await waitFor(() => getByRole('button')); + expect(getByRole('button')).toHaveTextContent(value); + }); + + it('should toggle the visibility of the input when the enter key is pressed', async () => { + const value = 'Hello'; + const isInputVisible = true; + const { getByRole } = await render( + false} + /> + ); + fireEvent.keyDown(getByRole('spinbutton'), { + key: 'Enter', + }); + await waitFor(() => getByRole('button')); + expect(getByRole('button')).toHaveTextContent(value); + }); +}); diff --git a/apps/console-lite/src/app/components/input-setter/input-setter.tsx b/apps/console-lite/src/app/components/input-setter/input-setter.tsx new file mode 100644 index 000000000..9e58f3f44 --- /dev/null +++ b/apps/console-lite/src/app/components/input-setter/input-setter.tsx @@ -0,0 +1,56 @@ +import React, { useCallback, useState } from 'react'; +import { t } from '@vegaprotocol/react-helpers'; +import { Input } from '@vegaprotocol/ui-toolkit'; +import type { InputProps } from '@vegaprotocol/ui-toolkit'; + +interface InputSetterProps { + buttonLabel?: string; + value: string | number; + isInputVisible?: boolean; + onValueChange?: () => string; +} + +export const InputSetter = ({ + buttonLabel = t('set'), + value = '', + isInputVisible = false, + onValueChange, + ...props +}: InputSetterProps & InputProps) => { + const [isInputToggled, setIsInputToggled] = useState(isInputVisible); + + const toggleInput = useCallback(() => { + setIsInputToggled(!isInputToggled); + }, [isInputToggled]); + + const onInputEnter = useCallback( + (event: React.KeyboardEvent) => { + if (event.key === 'Enter') { + event.stopPropagation(); + toggleInput(); + } + }, + [toggleInput] + ); + + return isInputToggled ? ( +
+ + +
+ ) : ( + + ); +}; diff --git a/apps/console-lite/src/app/components/portfolio/portfolio.tsx b/apps/console-lite/src/app/components/portfolio/portfolio.tsx index 89d81cd33..8e5431c52 100644 --- a/apps/console-lite/src/app/components/portfolio/portfolio.tsx +++ b/apps/console-lite/src/app/components/portfolio/portfolio.tsx @@ -11,7 +11,11 @@ import ConnectWallet from '../wallet-connector'; export const Portfolio = () => { const { keypair } = useVegaWallet(); if (!keypair) { - return ; + return ( +
+ +
+ ); } return ( diff --git a/libs/ui-toolkit/src/components/input/input.tsx b/libs/ui-toolkit/src/components/input/input.tsx index a6b8f81b4..c49fd3e3b 100644 --- a/libs/ui-toolkit/src/components/input/input.tsx +++ b/libs/ui-toolkit/src/components/input/input.tsx @@ -55,7 +55,7 @@ type InputAppend = NoPrepend & type AffixProps = InputPrepend | InputAppend; -type InputProps = InputRootProps & AffixProps; +export type InputProps = InputRootProps & AffixProps; export const inputStyle = ({ style,