Skip to content

Commit

Permalink
collapsible trade/single form (#134)
Browse files Browse the repository at this point in the history
* collapse transition with default hidden state for seldom used inputs
  • Loading branch information
aslink87 authored May 15, 2024
1 parent ecfaa58 commit 90e7b59
Show file tree
Hide file tree
Showing 15 changed files with 899 additions and 730 deletions.
1 change: 1 addition & 0 deletions app/_components/commodities/CommodityFormResponse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const CommodityFormResponse: React.FC<ICommodityFormResponseProps> = ({
'distance' as keyof ICommodityFormResponse,
);
const [ascending, setAscending] = useState(false);

const { isDark } = useColorMode();

return (
Expand Down
30 changes: 20 additions & 10 deletions app/_components/commodities/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
FormErrorMessage,
} from '@chakra-ui/react';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm, SubmitHandler } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import GetColor from '@/app/_hooks/colorSelector';
import {
CommoditiesField,
Expand Down Expand Up @@ -67,6 +67,13 @@ const Form: React.FC<FormProps> = ({
resolver: zodResolver(CommodityFormSchema),
});

const onSubmit: SubmitHandler<SubmitProps> = (data) => {
const submitData = data;
if (isBuying) submitData.minDemand = 0;
if (!isBuying) submitData.minSupply = 0;
onSubmitHandler(submitData);
};

const numberInputs = (
label: string,
registerName: 'minSupply' | 'minDemand',
Expand Down Expand Up @@ -95,13 +102,6 @@ const Form: React.FC<FormProps> = ({
</>
);

const onSubmit: SubmitHandler<SubmitProps> = (data) => {
const submitData = data;
if (isBuying) submitData.minDemand = 0;
if (!isBuying) submitData.minSupply = 0;
onSubmitHandler(submitData);
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Flex
Expand Down Expand Up @@ -154,7 +154,12 @@ const Form: React.FC<FormProps> = ({
</FormControl>
</Stack>
<Stack
direction={{ base: 'column', sm: 'column', md: 'column', lg: 'row' }}
direction={{
base: 'column',
sm: 'column',
md: 'column',
lg: 'row',
}}
spacing={4}
width="100%"
marginTop={4}
Expand All @@ -174,7 +179,12 @@ const Form: React.FC<FormProps> = ({
</FormControl>
</Stack>
<Stack
direction={{ base: 'column', sm: 'column', md: 'column', lg: 'row' }}
direction={{
base: 'column',
sm: 'column',
md: 'column',
lg: 'row',
}}
spacing={4}
width="100%"
marginTop={4}
Expand Down
4 changes: 2 additions & 2 deletions app/_components/form/Checkbox.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UseFormRegisterReturn } from 'react-hook-form';
import { Checkbox as ChakraCheckbox } from '@chakra-ui/react';
import { Checkbox as ChakraCheckbox, Text } from '@chakra-ui/react';

interface Props {
register: UseFormRegisterReturn;
Expand All @@ -9,7 +9,7 @@ interface Props {

const Checkbox = ({ register, label, checked }: Props) => (
<ChakraCheckbox {...register} defaultChecked={checked}>
{label}
<Text fontSize="sm">{label}</Text>
</ChakraCheckbox>
);

Expand Down
1 change: 1 addition & 0 deletions app/_components/form/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PropsWithChildren } from 'react';

const CheckboxGroup = ({ children }: PropsWithChildren<{}>) => (
<Stack
height={{ base: 'inherit', md: '40px', lg: '40px' }}
borderWidth="1px"
borderRadius="md"
borderColor={GetColor('border')}
Expand Down
4 changes: 2 additions & 2 deletions app/_components/form/Radio.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import GetColor from '@/app/_hooks/colorSelector';
import { UseFormRegisterReturn } from 'react-hook-form';
import { Radio as ChakraRadio } from '@chakra-ui/react';
import { Radio as ChakraRadio, Text } from '@chakra-ui/react';

interface Props {
register: UseFormRegisterReturn;
Expand All @@ -18,7 +18,7 @@ const Radio = ({ register, label, value, checked }: Props) => (
defaultChecked={checked}
value={value}
>
{label}
<Text fontSize="sm">{label}</Text>
</ChakraRadio>
);

Expand Down
3 changes: 2 additions & 1 deletion app/_components/form/RadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { PropsWithChildren } from 'react';
const RadioGroup = ({ children }: PropsWithChildren<{}>) => (
<ChackraRadioGroup colorScheme="gray">
<Stack
height={{ base: 'inherit', md: '40px', lg: '40px' }}
borderWidth="1px"
borderRadius="md"
borderColor={GetColor('border')}
padding={3}
spacing={5}
direction="row"
direction={{ base: 'column', sm: 'column', md: 'row', lg: 'row' }}
>
{children}
</Stack>
Expand Down
4 changes: 2 additions & 2 deletions app/_components/inputs/StationTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ interface Props {
}

export const checkboxValues = [
{ name: 'Orbital Station', value: 'includeOrbital', checked: true },
{ name: 'Orbital', value: 'includeOrbital', checked: true },
{ name: 'Planetary', value: 'includePlanetary' },
{ name: 'Odyssey', value: 'includeOdyssey' },
{ name: 'Fleet Carriers', value: 'includeFleetCarriers' },
{ name: 'Carriers', value: 'includeFleetCarriers' },
];

const StationTypesField = ({ register }: Props) => (
Expand Down
2 changes: 1 addition & 1 deletion app/_components/inputs/tests/StationTypes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Station Type Field', () => {
it('checks only Orbital Station by default', () => {
expect(screen.getAllByRole('checkbox').length).toEqual(4);

expect(checkboxValues[0].name === 'Orbital Station').toBeTruthy();
expect(checkboxValues[0].name === 'Orbital').toBeTruthy();

expect(screen.getByLabelText(checkboxValues[0].name)).toBeChecked();
expect(screen.getByLabelText(checkboxValues[1].name)).not.toBeChecked();
Expand Down
14 changes: 13 additions & 1 deletion app/_components/stations/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Input,
FormErrorMessage,
Checkbox,
Text,
} from '@chakra-ui/react';

import { z } from 'zod';
Expand Down Expand Up @@ -117,7 +118,17 @@ const Form: React.FC<FormProps> = ({
</FormErrorMessage>
</FormControl>
</GridItem>
</Grid>

<Grid
templateColumns={{
base: 'repeat(1, 1fr)',
md: 'repeat(2, 1fr)',
lg: 'repeat(4, 1fr)',
}}
gap={6}
marginBottom="10"
>
<GridItem colSpan={{ base: 1, md: 2 }}>
<FormControl isInvalid={!!(errors.ships && errors.ships.message)}>
<FormLabel>Ships</FormLabel>
Expand Down Expand Up @@ -324,13 +335,14 @@ const Form: React.FC<FormProps> = ({
isInvalid={
!!(errors.requiresPermit && errors.requiresPermit.message)
}
display="flex"
>
<Checkbox
colorScheme="orange"
{...register('requiresPermit')}
borderColor={GetColor('border')}
>
Requires Permit
<Text fontSize="sm">Requires Permit</Text>
</Checkbox>
<FormErrorMessage>
{errors.requiresPermit && errors.requiresPermit.message}
Expand Down
3 changes: 2 additions & 1 deletion app/_components/systems/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Input,
FormErrorMessage,
Checkbox,
Text,
} from '@chakra-ui/react';

import { z } from 'zod';
Expand Down Expand Up @@ -281,7 +282,7 @@ const Form: React.FC<FormProps> = ({ onSubmitHandler, isLoading }) => {
{...register('requiresPermit')}
borderColor={GetColor('border')}
>
Requires Permit
<Text fontSize="sm">Requires Permit</Text>
</Checkbox>
<FormErrorMessage>
{errors.requiresPermit && errors.requiresPermit.message}
Expand Down
32 changes: 17 additions & 15 deletions app/_components/trade-routes/multi/Form.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import powers from '@/app/_lib/power-list';
import governments from '@/app/_lib/government-list';
import allegiances from '@/app/_lib/allegiance-list';
import {
Button,
FormControl,
FormLabel,
Grid,
GridItem,
Input,
FormErrorMessage,
Checkbox,
} from '@chakra-ui/react';
import { useState } from 'react';
import { z } from 'zod';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm, SubmitHandler } from 'react-hook-form';
import powers from '@/app/_lib/power-list';
import governments from '@/app/_lib/government-list';
import allegiances from '@/app/_lib/allegiance-list';
import GetColor from '@/app/_hooks/colorSelector';
import {
PowersField,
Expand All @@ -25,8 +16,18 @@ import {
SystemsField,
} from '@/app/_components/inputs';
import CheckboxGroup from '../../form/CheckboxGroup';
import { useState } from 'react';
import Select from '../../inputs/form/Select';
import {
Button,
FormControl,
FormLabel,
Grid,
GridItem,
Input,
FormErrorMessage,
Checkbox,
Text,
} from '@chakra-ui/react';
import { ICommodity } from '@/app/_types';

export const MultiTradeRouteFormSchema = z.object({
Expand Down Expand Up @@ -447,13 +448,14 @@ const Form: React.FC<FormProps> = ({
isInvalid={
!!(errors.requiresPermit && errors.requiresPermit.message)
}
display="flex"
>
<Checkbox
colorScheme="orange"
{...register('requiresPermit')}
borderColor={GetColor('border')}
>
Requires Permit
<Text fontSize="sm">Requires Permit</Text>
</Checkbox>
<FormErrorMessage>
{errors.requiresPermit && errors.requiresPermit.message}
Expand Down
20 changes: 15 additions & 5 deletions app/_components/trade-routes/single/Form.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/react';
import { act, render, screen } from '@testing-library/react';
import Form from './Form';
import { ChakraProvider } from '@chakra-ui/react';

Expand Down Expand Up @@ -43,6 +43,20 @@ describe('Stations Form', () => {
</ChakraProvider>,
);

// filter button hides fields at start
expect(
screen.queryByRole('combobox', { name: 'Commodities' }),
).not.toBeInTheDocument();

const filtersButton = screen.getByRole('button', { name: 'Options' });

expect(filtersButton).toBeInTheDocument();

// @ts-ignore depecated warning relates to PR https://github.com/testing-library/react-testing-library/pull/1319
act(() => {
filtersButton.click();
});

expect(
screen.getByRole('combobox', { name: 'Buy from Station' }),
).toBeInTheDocument();
Expand All @@ -55,10 +69,6 @@ describe('Stations Form', () => {
screen.getByRole('spinbutton', { name: 'Max Route Distance' }),
).toBeInTheDocument();

expect(
screen.getByRole('combobox', { name: 'Commodities' }),
).toBeInTheDocument();

expect(
screen.getByRole('spinbutton', { name: 'Cargo Capacity' }),
).toBeInTheDocument();
Expand Down
Loading

0 comments on commit 90e7b59

Please sign in to comment.