aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/molecules/forms
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-04-16 16:08:49 +0200
committerArmand Philippot <git@armandphilippot.com>2022-04-16 16:41:30 +0200
commit5a6e4eea16047083e2de0e91a1b3ed9be8d6eb68 (patch)
treeea0c5390aca73907aade5321f30cb7bf8d3ab1cb /src/components/molecules/forms
parentdaffe6e8b9e2021ffb9d006482143bc4db985f02 (diff)
refactor: support React 18
I replaced the deprecated VFC type with FC type and made all children explicits. Formatjs is still not compatible with React 18 so I need to skip type checking when comitting. There are some type errors because of IntlProvider in Storybook stories.
Diffstat (limited to 'src/components/molecules/forms')
-rw-r--r--src/components/molecules/forms/ackee-select.test.tsx16
-rw-r--r--src/components/molecules/forms/ackee-select.tsx6
-rw-r--r--src/components/molecules/forms/labelled-field.tsx4
-rw-r--r--src/components/molecules/forms/labelled-select.tsx15
-rw-r--r--src/components/molecules/forms/motion-toggle.tsx8
-rw-r--r--src/components/molecules/forms/prism-theme-toggle.tsx8
-rw-r--r--src/components/molecules/forms/select-with-tooltip.tsx10
-rw-r--r--src/components/molecules/forms/theme-toggle.tsx8
-rw-r--r--src/components/molecules/forms/toggle.tsx18
9 files changed, 44 insertions, 49 deletions
diff --git a/src/components/molecules/forms/ackee-select.test.tsx b/src/components/molecules/forms/ackee-select.test.tsx
index e1e6b2d..ec27922 100644
--- a/src/components/molecules/forms/ackee-select.test.tsx
+++ b/src/components/molecules/forms/ackee-select.test.tsx
@@ -1,5 +1,5 @@
-import userEvent from '@testing-library/user-event';
-import { render, screen } from '@test-utils';
+import user from '@testing-library/user-event';
+import { act, render, screen } from '@test-utils';
import AckeeSelect from './ackee-select';
describe('Select', () => {
@@ -9,13 +9,15 @@ describe('Select', () => {
expect(screen.queryByRole('combobox')).not.toHaveValue('partial');
});
- it('should correctly change value when user choose another option', () => {
+ it('should correctly change value when user choose another option', async () => {
render(<AckeeSelect initialValue="full" />);
- userEvent.selectOptions(
- screen.getByRole('combobox'),
- screen.getByRole('option', { name: 'Partial' })
- );
+ await act(async () => {
+ await user.selectOptions(
+ screen.getByRole('combobox'),
+ screen.getByRole('option', { name: 'Partial' })
+ );
+ });
expect(screen.getByRole('combobox')).toHaveValue('partial');
expect(screen.queryByRole('combobox')).not.toHaveValue('full');
diff --git a/src/components/molecules/forms/ackee-select.tsx b/src/components/molecules/forms/ackee-select.tsx
index 4a8410c..101e5b5 100644
--- a/src/components/molecules/forms/ackee-select.tsx
+++ b/src/components/molecules/forms/ackee-select.tsx
@@ -1,8 +1,8 @@
import { SelectOptions } from '@components/atoms/forms/select';
-import { Dispatch, SetStateAction, useState, VFC } from 'react';
+import { Dispatch, FC, SetStateAction, useState } from 'react';
import { useIntl } from 'react-intl';
import SelectWithTooltip, {
- SelectWithTooltipProps,
+ type SelectWithTooltipProps,
} from './select-with-tooltip';
export type AckeeOptions = 'full' | 'partial';
@@ -22,7 +22,7 @@ export type AckeeSelectProps = Pick<
*
* Render a select to set Ackee settings.
*/
-const AckeeSelect: VFC<AckeeSelectProps> = ({ initialValue, ...props }) => {
+const AckeeSelect: FC<AckeeSelectProps> = ({ initialValue, ...props }) => {
const intl = useIntl();
const [value, setValue] = useState<AckeeOptions>(initialValue);
diff --git a/src/components/molecules/forms/labelled-field.tsx b/src/components/molecules/forms/labelled-field.tsx
index 08d0126..ecc9255 100644
--- a/src/components/molecules/forms/labelled-field.tsx
+++ b/src/components/molecules/forms/labelled-field.tsx
@@ -1,6 +1,6 @@
import Field, { type FieldProps } from '@components/atoms/forms/field';
import Label from '@components/atoms/forms/label';
-import { VFC } from 'react';
+import { FC } from 'react';
import styles from './labelled-field.module.scss';
export type LabelledFieldProps = FieldProps & {
@@ -23,7 +23,7 @@ export type LabelledFieldProps = FieldProps & {
*
* Render a field tied to a label.
*/
-const LabelledField: VFC<LabelledFieldProps> = ({
+const LabelledField: FC<LabelledFieldProps> = ({
hideLabel = false,
id,
label,
diff --git a/src/components/molecules/forms/labelled-select.tsx b/src/components/molecules/forms/labelled-select.tsx
index 7d4237a..23057d0 100644
--- a/src/components/molecules/forms/labelled-select.tsx
+++ b/src/components/molecules/forms/labelled-select.tsx
@@ -1,6 +1,6 @@
-import Label, { LabelProps } from '@components/atoms/forms/label';
+import Label, { type LabelProps } from '@components/atoms/forms/label';
import Select, { type SelectProps } from '@components/atoms/forms/select';
-import { VFC } from 'react';
+import { FC } from 'react';
import styles from './labelled-select.module.scss';
export type LabelledSelectProps = Omit<
@@ -14,7 +14,7 @@ export type LabelledSelectProps = Omit<
/**
* Set additional classnames to the label.
*/
- labelClassName?: string;
+ labelClassName?: LabelProps['className'];
/**
* The label position. Default: top.
*/
@@ -26,10 +26,15 @@ export type LabelledSelectProps = Omit<
/**
* Set additional classnames to the select field.
*/
- selectClassName?: string;
+ selectClassName?: SelectProps['className'];
};
-const LabelledSelect: VFC<LabelledSelectProps> = ({
+/**
+ * LabelledSelect component
+ *
+ * Render a select with a label.
+ */
+const LabelledSelect: FC<LabelledSelectProps> = ({
id,
label,
labelClassName = '',
diff --git a/src/components/molecules/forms/motion-toggle.tsx b/src/components/molecules/forms/motion-toggle.tsx
index 9f30b42..24b54ae 100644
--- a/src/components/molecules/forms/motion-toggle.tsx
+++ b/src/components/molecules/forms/motion-toggle.tsx
@@ -1,8 +1,8 @@
import Toggle, {
- ToggleChoices,
- ToggleProps,
+ type ToggleChoices,
+ type ToggleProps,
} from '@components/molecules/forms/toggle';
-import { useState, VFC } from 'react';
+import { FC, useState } from 'react';
import { useIntl } from 'react-intl';
export type MotionToggleProps = Pick<ToggleProps, 'labelClassName' | 'value'>;
@@ -12,7 +12,7 @@ export type MotionToggleProps = Pick<ToggleProps, 'labelClassName' | 'value'>;
*
* Render a Toggle component to set reduce motion.
*/
-const MotionToggle: VFC<MotionToggleProps> = ({ value, ...props }) => {
+const MotionToggle: FC<MotionToggleProps> = ({ value, ...props }) => {
const intl = useIntl();
const [isDeactivated, setIsDeactivated] = useState<boolean>(value);
const reduceMotionLabel = intl.formatMessage({
diff --git a/src/components/molecules/forms/prism-theme-toggle.tsx b/src/components/molecules/forms/prism-theme-toggle.tsx
index daee6bd..0b9c447 100644
--- a/src/components/molecules/forms/prism-theme-toggle.tsx
+++ b/src/components/molecules/forms/prism-theme-toggle.tsx
@@ -1,10 +1,10 @@
import Moon from '@components/atoms/icons/moon';
import Sun from '@components/atoms/icons/sun';
import Toggle, {
- ToggleChoices,
- ToggleProps,
+ type ToggleChoices,
+ type ToggleProps,
} from '@components/molecules/forms/toggle';
-import { useState, VFC } from 'react';
+import { FC, useState } from 'react';
import { useIntl } from 'react-intl';
export type PrismThemeToggleProps = Pick<
@@ -17,7 +17,7 @@ export type PrismThemeToggleProps = Pick<
*
* Render a Toggle component to set code blocks theme.
*/
-const PrismThemeToggle: VFC<PrismThemeToggleProps> = ({ value, ...props }) => {
+const PrismThemeToggle: FC<PrismThemeToggleProps> = ({ value, ...props }) => {
const intl = useIntl();
const [isDarkTheme, setIsDarkTheme] = useState<boolean>(value);
const themeLabel = intl.formatMessage({
diff --git a/src/components/molecules/forms/select-with-tooltip.tsx b/src/components/molecules/forms/select-with-tooltip.tsx
index f537e1e..cf7b041 100644
--- a/src/components/molecules/forms/select-with-tooltip.tsx
+++ b/src/components/molecules/forms/select-with-tooltip.tsx
@@ -1,4 +1,4 @@
-import { useState, VFC } from 'react';
+import { FC, useState } from 'react';
import HelpButton from '../buttons/help-button';
import Tooltip, { type TooltipProps } from '../modals/tooltip';
import LabelledSelect, { type LabelledSelectProps } from './labelled-select';
@@ -10,13 +10,9 @@ export type SelectWithTooltipProps = Omit<
> &
Pick<TooltipProps, 'title' | 'content'> & {
/**
- * The select label.
- */
- label: string;
- /**
* Set additional classnames to the tooltip wrapper.
*/
- tooltipClassName?: string;
+ tooltipClassName?: TooltipProps['className'];
};
/**
@@ -24,7 +20,7 @@ export type SelectWithTooltipProps = Omit<
*
* Render a select with a button to display a tooltip about options.
*/
-const SelectWithTooltip: VFC<SelectWithTooltipProps> = ({
+const SelectWithTooltip: FC<SelectWithTooltipProps> = ({
title,
content,
id,
diff --git a/src/components/molecules/forms/theme-toggle.tsx b/src/components/molecules/forms/theme-toggle.tsx
index eb56ce9..10c6c47 100644
--- a/src/components/molecules/forms/theme-toggle.tsx
+++ b/src/components/molecules/forms/theme-toggle.tsx
@@ -1,10 +1,10 @@
import Moon from '@components/atoms/icons/moon';
import Sun from '@components/atoms/icons/sun';
import Toggle, {
- ToggleChoices,
- ToggleProps,
+ type ToggleChoices,
+ type ToggleProps,
} from '@components/molecules/forms/toggle';
-import { useState, VFC } from 'react';
+import { FC, useState } from 'react';
import { useIntl } from 'react-intl';
export type ThemeToggleProps = Pick<ToggleProps, 'labelClassName' | 'value'>;
@@ -14,7 +14,7 @@ export type ThemeToggleProps = Pick<ToggleProps, 'labelClassName' | 'value'>;
*
* Render a Toggle component to set theme.
*/
-const ThemeToggle: VFC<ThemeToggleProps> = ({ value, ...props }) => {
+const ThemeToggle: FC<ThemeToggleProps> = ({ value, ...props }) => {
const intl = useIntl();
const [isDarkTheme, setIsDarkTheme] = useState<boolean>(value);
const themeLabel = intl.formatMessage({
diff --git a/src/components/molecules/forms/toggle.tsx b/src/components/molecules/forms/toggle.tsx
index dff2d2d..288062d 100644
--- a/src/components/molecules/forms/toggle.tsx
+++ b/src/components/molecules/forms/toggle.tsx
@@ -1,6 +1,6 @@
-import Checkbox from '@components/atoms/forms/checkbox';
+import Checkbox, { type CheckboxProps } from '@components/atoms/forms/checkbox';
import Label, { type LabelProps } from '@components/atoms/forms/label';
-import { ReactNode, VFC } from 'react';
+import { FC, ReactNode } from 'react';
import styles from './toggle.module.scss';
export type ToggleChoices = {
@@ -14,32 +14,24 @@ export type ToggleChoices = {
right: ReactNode;
};
-export type ToggleProps = {
+export type ToggleProps = Pick<CheckboxProps, 'id' | 'name'> & {
/**
* The toggle choices.
*/
choices: ToggleChoices;
/**
- * The input id.
- */
- id: string;
- /**
* The toggle label.
*/
label: string;
/**
* Set additional classnames to the label.
*/
- labelClassName?: string;
+ labelClassName?: LabelProps['className'];
/**
* The label size.
*/
labelSize?: LabelProps['size'];
/**
- * The input name.
- */
- name: string;
- /**
* The toggle value. True if checked.
*/
value: boolean;
@@ -54,7 +46,7 @@ export type ToggleProps = {
*
* Render a toggle with a label and two choices.
*/
-const Toggle: VFC<ToggleProps> = ({
+const Toggle: FC<ToggleProps> = ({
choices,
id,
label,