Skip to content

Commit

Permalink
fix: auto-scroll to the first invalid form field #873
Browse files Browse the repository at this point in the history
  • Loading branch information
shuashuai committed Apr 25, 2024
1 parent eeb668e commit 76c55fc
Show file tree
Hide file tree
Showing 26 changed files with 187 additions and 38 deletions.
3 changes: 3 additions & 0 deletions ui/src/components/SchemaForm/index.tsx
Expand Up @@ -29,6 +29,7 @@ import { useTranslation } from 'react-i18next';
import isEmpty from 'lodash/isEmpty';
import classnames from 'classnames';

import { scrollToElementTop } from '@/utils';
import type * as Type from '@/common/interface';

import type {
Expand Down Expand Up @@ -201,6 +202,7 @@ const SchemaForm: ForwardRefRenderFunction<FormRef, FormProps> = (
if (onChange instanceof Function) {
onChange({ ...formData });
}
scrollToElementTop(document.getElementById(errors[0]));
return false;
}
const syncErrors = await syncValidator();
Expand All @@ -216,6 +218,7 @@ const SchemaForm: ForwardRefRenderFunction<FormRef, FormProps> = (
if (onChange instanceof Function) {
onChange({ ...formData });
}
scrollToElementTop(document.getElementById(syncErrors[0].key));
return false;
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Branding/index.tsx
Expand Up @@ -25,7 +25,7 @@ import { FormDataType } from '@/common/interface';
import { brandSetting, getBrandSetting } from '@/services';
import { brandingStore } from '@/stores';
import { useToast } from '@/hooks';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';

const uploadType = 'branding';
const Index: FC = () => {
Expand Down Expand Up @@ -145,6 +145,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/CssAndHtml/index.tsx
Expand Up @@ -24,7 +24,7 @@ import type * as Type from '@/common/interface';
import { getPageCustom, putPageCustom } from '@/services';
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
import { useToast } from '@/hooks';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { customizeStore } from '@/stores';

const Index: FC = () => {
Expand Down Expand Up @@ -124,6 +124,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/General/index.tsx
Expand Up @@ -26,7 +26,7 @@ import { useToast } from '@/hooks';
import { siteInfoStore } from '@/stores';
import { useGeneralSetting, updateGeneralSetting } from '@/services';
import Pattern from '@/common/pattern';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';

const General: FC = () => {
const { t } = useTranslation('translation', {
Expand Down Expand Up @@ -157,6 +157,8 @@ const General: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Interface/index.tsx
Expand Up @@ -39,7 +39,7 @@ import {
loadLanguageOptions,
setupAppTimeZone,
} from '@/utils/localize';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';

const Interface: FC = () => {
const { t } = useTranslation('translation', {
Expand Down Expand Up @@ -141,6 +141,8 @@ const Interface: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Legal/index.tsx
Expand Up @@ -26,7 +26,7 @@ import type * as Type from '@/common/interface';
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
import { useToast } from '@/hooks';
import { getLegalSetting, putLegalSetting } from '@/services';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';

const Legal: FC = () => {
const { t } = useTranslation('translation', {
Expand Down Expand Up @@ -90,6 +90,8 @@ const Legal: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Login/index.tsx
Expand Up @@ -24,7 +24,7 @@ import type * as Type from '@/common/interface';
import { getLoginSetting, putLoginSetting } from '@/services';
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
import { useToast } from '@/hooks';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { loginSettingStore } from '@/stores';

const Index: FC = () => {
Expand Down Expand Up @@ -132,6 +132,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
8 changes: 5 additions & 3 deletions ui/src/pages/Admin/Privileges/index.tsx
Expand Up @@ -29,7 +29,7 @@ import {
AdminSettingsPrivilege,
AdminSettingsPrivilegeReq,
} from '@/services';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { ADMIN_PRIVILEGE_CUSTOM_LEVEL } from '@/common/constants';

const Index: FC = () => {
Expand Down Expand Up @@ -83,10 +83,10 @@ const Index: FC = () => {
validator: (value: string) => {
const val = Number(value);
if (Number.isNaN(val)) {
return 'the input should be number';
return t('msg.should_be_number');
}
if (val < 1) {
return 'number should be equal or larger than 1';
return t('msg.number_larger_1');
}
return true;
},
Expand Down Expand Up @@ -148,6 +148,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Seo/index.tsx
Expand Up @@ -24,7 +24,7 @@ import type * as Type from '@/common/interface';
import { getSeoSetting, putSeoSetting } from '@/services';
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
import { useToast } from '@/hooks';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { seoSettingStore } from '@/stores';

const Index: FC = () => {
Expand Down Expand Up @@ -90,6 +90,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/SettingsUsers/index.tsx
Expand Up @@ -29,7 +29,7 @@ import {
putUsersSetting,
AdminSettingsUsers,
} from '@/services';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import * as Type from '@/common/interface';
import { siteInfoStore } from '@/stores';

Expand Down Expand Up @@ -168,6 +168,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Smtp/index.tsx
Expand Up @@ -25,7 +25,7 @@ import { useToast } from '@/hooks';
import { useSmtpSetting, updateSmtpSetting } from '@/services';
import pattern from '@/common/pattern';
import { SchemaForm, JSONSchema, UISchema, initFormData } from '@/components';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';

const Smtp: FC = () => {
const { t } = useTranslation('translation', {
Expand Down Expand Up @@ -182,6 +182,8 @@ const Smtp: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Themes/index.tsx
Expand Up @@ -24,7 +24,7 @@ import type * as Type from '@/common/interface';
import { getThemeSetting, putThemeSetting } from '@/services';
import { SchemaForm, JSONSchema, initFormData, UISchema } from '@/components';
import { useToast } from '@/hooks';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { themeSettingStore } from '@/stores';
import { setupAppTheme } from '@/utils/localize';

Expand Down Expand Up @@ -135,6 +135,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
4 changes: 3 additions & 1 deletion ui/src/pages/Admin/Write/index.tsx
Expand Up @@ -27,7 +27,7 @@ import {
getRequireAndReservedTag,
postRequireAndReservedTag,
} from '@/services';
import { handleFormError } from '@/utils';
import { handleFormError, scrollToElementTop } from '@/utils';
import { writeSettingStore } from '@/stores';

const Index: FC = () => {
Expand Down Expand Up @@ -121,6 +121,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down
9 changes: 8 additions & 1 deletion ui/src/pages/Install/index.tsx
Expand Up @@ -29,7 +29,12 @@ import {
installBaseInfo,
checkConfigFileExists,
} from '@/services';
import { Storage, handleFormError, scrollToDocTop } from '@/utils';
import {
Storage,
handleFormError,
scrollToDocTop,
scrollToElementTop,
} from '@/utils';
import { CURRENT_LANG_STORAGE_KEY } from '@/common/constants';

import {
Expand Down Expand Up @@ -201,6 +206,8 @@ const Index: FC = () => {
if (err.isError) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
} else {
handleErr(err);
}
Expand Down
15 changes: 13 additions & 2 deletions ui/src/pages/Questions/Ask/index.tsx
Expand Up @@ -40,7 +40,12 @@ import {
getTagsBySlugName,
saveQuestionWithAnswer,
} from '@/services';
import { handleFormError, SaveDraft, storageExpires } from '@/utils';
import {
handleFormError,
SaveDraft,
storageExpires,
scrollToElementTop,
} from '@/utils';
import { pathFactory } from '@/router/pathFactory';
import { useCaptchaPlugin } from '@/utils/pluginKit';

Expand Down Expand Up @@ -300,6 +305,8 @@ const Ask = () => {
editCaptcha?.handleCaptchaError(err.list);
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand All @@ -321,6 +328,8 @@ const Ask = () => {
if (!(captchaErr && err.list.length === 1)) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
}
});
Expand All @@ -331,6 +340,8 @@ const Ask = () => {
if (!(captchaErr && err.list.length === 1)) {
const data = handleFormError(err, formData);
setFormData({ ...data });
const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
}
});
Expand Down Expand Up @@ -443,7 +454,7 @@ const Ask = () => {
</Form.Control.Feedback>
{bool && <SearchQuestion similarQuestions={similarQuestions} />}
</Form.Group>
<Form.Group controlId="body">
<Form.Group controlId="content">
<Form.Label>{t('form.fields.body.label')}</Form.Label>
<Form.Control
defaultValue={formData.content.value}
Expand Down
13 changes: 13 additions & 0 deletions ui/src/pages/Review/components/EditPostModal/index.tsx
Expand Up @@ -32,6 +32,7 @@ import {
parseUserInfo,
handleFormError,
parseEditMentionUser,
scrollToElementTop,
} from '@/utils';
import type * as Type from '@/common/interface';

Expand Down Expand Up @@ -158,6 +159,15 @@ const Index: FC<Props> = ({
setFormData({
...formData,
});

if (!bol) {
const errObj = Object.keys(formData).filter(
(key) => formData[key].isInvalid,
);
const ele = document.getElementById(errObj[0]);
scrollToElementTop(ele);
}

return bol;
};

Expand Down Expand Up @@ -202,6 +212,9 @@ const Index: FC<Props> = ({
editCaptcha?.handleCaptchaError(err.list);
const data = handleFormError(err, formData);
setFormData({ ...data });

const ele = document.getElementById(err.list[0].error_field);
scrollToElementTop(ele);
}
});
};
Expand Down

0 comments on commit 76c55fc

Please sign in to comment.