Skip to content

Commit

Permalink
fix: sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
web-mi committed Feb 5, 2024
1 parent f69df04 commit e8db026
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const AgencySelectionFormField = ({
<InputFormField
name="consultingTypeId"
type="hidden"
pattern={/^\d+$/}
rule={{
pattern: /^\d+$/
}}
/>
<InputFormField name="postCode" type="hidden" />
<InputFormField name="agencyId" type="hidden" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useDebouncedCallback } from 'use-debounce';
import './formAccordion.styles.scss';
import { FormAccordionItemProps } from './FormAccordionItem';
import { DebouncedState } from 'use-debounce/lib/useDebouncedCallback';
import { FormInstance } from 'rc-field-form';

Check warning on line 12 in src/extensions/components/registration/FormAccordion/FormAccordion.tsx

View workflow job for this annotation

GitHub Actions / ESLint

src/extensions/components/registration/FormAccordion/FormAccordion.tsx#L12

'FormInstance' is defined but never used (@typescript-eslint/no-unused-vars)

export interface FormAccordionChildProps {
activePanel: FormAccordionItemProps['id'];
Expand Down Expand Up @@ -43,43 +44,41 @@ export const FormAccordion = ({ children, onComplete }: FormAccordionProps) => {
>(childIds[0]);
const ref = useRef<HTMLDivElement>(null);

const onClickNext = useCallback(() => {
const handleScroll = useCallback((panel) => {
setTimeout(() => {
const scrollContainer =
document.getElementsByClassName(`stageLayout`)[0];
const element = document.getElementById(`panel-${panel}`);
const offsetPosition = element.offsetTop - scrollOffset;

scrollContainer.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}, 50);
}, []);

const handleNextStep = useCallback(() => {
const childIdIndex = childIds.indexOf(activePanel);
setActivePanel(childIds?.[childIdIndex + 1]);
// Call onComplete when next on last panel was clicked
if (childIdIndex === childIds.length - 1) {
onComplete?.();
if (childIdIndex !== childIds.length - 1) {
return handleScroll(childIds?.[childIdIndex + 1]);
}
}, [activePanel, childIds, onComplete]);
onComplete?.();
}, [activePanel, childIds, handleScroll, onComplete]);

const handlePanelClick = useCallback(
(
panel: FormAccordionChildProps['activePanel'],
isTabPressed?: boolean
) => {
console.log(
activePanel,
panel,
isTabPressed,
activePanel === panel && !isTabPressed ? undefined : panel
);
setActivePanel(
activePanel === panel && !isTabPressed ? undefined : panel
);

setTimeout(() => {
const scrollContainer =
document.getElementsByClassName(`stageLayout`)[0];
const element = document.getElementById(`panel-${panel}`);
const offsetPosition = element.offsetTop - scrollOffset;

scrollContainer.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}, 50);
handleScroll(panel);
},
[activePanel]
[activePanel, handleScroll]
);
const debouncedHandlePanelClick = useDebouncedCallback(
handlePanelClick,
Expand All @@ -92,7 +91,7 @@ export const FormAccordion = ({ children, onComplete }: FormAccordionProps) => {
{children({
activePanel,
handlePanelClick: debouncedHandlePanelClick,
handleNextStep: onClickNext
handleNextStep
})}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FieldContext, FormInstance } from 'rc-field-form';
import React from 'react';
import { FieldContext } from 'rc-field-form';
import React, { FC } from 'react';
import './formAccordion.styles.scss';
import { useTranslation } from 'react-i18next';
import {
Expand All @@ -9,20 +9,21 @@ import {
} from '../../../../components/button/Button';
import { InvalidIcon } from '../../../../resources/img/icons';
import { FormAccordionChildProps } from './FormAccordion';
import classNames from 'classnames';

export interface FormAccordionItemProps {
id?: string;
disableNextButton?: boolean;
form?: FormInstance;
stepNumber?: number;
title: string;
subTitle?: string;
formFields?: string[];
errorOnTouchExtraFields?: string[];
children: React.ReactChild | React.ReactChild[];
}

export const FormAccordionItem = ({
export const FormAccordionItem: FC<
FormAccordionItemProps & FormAccordionChildProps
> = ({
id,
stepNumber,
title,
Expand All @@ -33,12 +34,12 @@ export const FormAccordionItem = ({
handlePanelClick,
handleNextStep,
activePanel,
form,
disableNextButton
}: FormAccordionItemProps & FormAccordionChildProps) => {
}) => {
const formContext = React.useContext(FieldContext);
const fieldsToCheck = [...formFields, ...errorOnTouchExtraFields];
const isFieldsInValid = (form || formContext)

const isFieldsInValid = formContext
.getFieldsError(formFields)
.some((error) => error.errors.length !== 0);

Expand All @@ -58,7 +59,9 @@ export const FormAccordionItem = ({

return (
<div
className={`formAccordionDigi__Panel ${isActive ? 'active' : ''}`}
className={classNames('formAccordionDigi__Panel', {
active: isActive
})}
id={`panel-${id}`}
>
<div
Expand Down
44 changes: 21 additions & 23 deletions src/extensions/components/registration/InputFormField/index.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
import { Field } from 'rc-field-form';
import React from 'react';
import type { Rule, Store, StoreValue } from 'rc-field-form/es/interface';

Check warning on line 3 in src/extensions/components/registration/InputFormField/index.tsx

View workflow job for this annotation

GitHub Actions / ESLint

src/extensions/components/registration/InputFormField/index.tsx#L3

'Store' is defined but never used (@typescript-eslint/no-unused-vars)

Check warning on line 3 in src/extensions/components/registration/InputFormField/index.tsx

View workflow job for this annotation

GitHub Actions / ESLint

src/extensions/components/registration/InputFormField/index.tsx#L3

'StoreValue' is defined but never used (@typescript-eslint/no-unused-vars)
import { FieldProps } from 'rc-field-form/es/Field';

interface InputProps {
name?: string;
min?: number;
max?: number;
placeholder?: string;
normalize?: FieldProps['normalize'];
type?: string;
pattern?: RegExp;
rule?: Rule;
tabIndex?: number;
autoFocus?: boolean;
}

const LocalInput = ({
value,
type,
...rest
}: {
type?: string;
value?: string;
tabIndex?: number;
placeholder?: string;
}) => (
<input
className="registrationFormDigi__Input"
type={type}
value={value || ''}
{...rest}
/>
);

export const InputFormField = ({
type = 'text',
name,
placeholder,
pattern,
normalize,
rule,
...rest
}: InputProps) => {
const patternRules = pattern ? { pattern } : {};
return (
<Field name={name} rules={[{ required: true, ...patternRules }]}>
<LocalInput type={type} placeholder={placeholder} {...rest} />
<Field
name={name}
rules={[{ required: true, ...(rule || {}) }]}
normalize={normalize}
>
{({ value, ...props }) => (
<input
className="registrationFormDigi__Input"
type={type}
placeholder={placeholder}
value={value || ''}
{...rest}
{...props}
/>
)}
</Field>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface RadioBoxGroupProps {
options: Array<{ label: string; value: string }>;
preset?: string;
dependencies?: NamePath[];
normalize?: (value: string) => any;
}

interface RadioBoxProps
Expand Down Expand Up @@ -52,6 +53,7 @@ export const RadioBoxGroup: FC<RadioBoxGroupProps> = ({
dependencies,
options,
preset,
normalize,
children,
...props
}) => {
Expand All @@ -61,6 +63,7 @@ export const RadioBoxGroup: FC<RadioBoxGroupProps> = ({
rules={[{ required: true }]}
dependencies={dependencies}
initialValue={preset}
normalize={normalize}
>
{({ value, onChange }) =>
options.map(({ value: valueRadio, label }, i) => (
Expand Down
Loading

0 comments on commit e8db026

Please sign in to comment.