New source found from dndbeyond.com
This commit is contained in:
@@ -1,33 +1,34 @@
|
||||
import { HTMLAttributes, useState } from "react";
|
||||
import styles from '../../styles/errors.module.css';
|
||||
import { HTMLAttributes, JSX, useState } from "react";
|
||||
|
||||
import styles from "../../styles/errors.module.css";
|
||||
|
||||
export interface ErrorHandlerOptions {
|
||||
initialState: boolean;
|
||||
errMsg: string;
|
||||
errorMessageAttributes?: HTMLAttributes<HTMLDivElement>;
|
||||
};
|
||||
initialState: boolean;
|
||||
errMsg: string;
|
||||
errorMessageAttributes?: HTMLAttributes<HTMLDivElement>;
|
||||
}
|
||||
|
||||
export interface ErrorHandler {
|
||||
showError: boolean;
|
||||
setShowError: (value: boolean) => void;
|
||||
ErrorMessage: () => JSX.Element;
|
||||
};
|
||||
showError: boolean;
|
||||
setShowError: (value: boolean) => void;
|
||||
ErrorMessage: () => JSX.Element;
|
||||
}
|
||||
|
||||
export const useErrorHandling = (
|
||||
initialState: boolean,
|
||||
errMsg: string,
|
||||
errorMessageAttributes: HTMLAttributes<HTMLDivElement> | null = null
|
||||
initialState: boolean,
|
||||
errMsg: string,
|
||||
errorMessageAttributes: HTMLAttributes<HTMLDivElement> | null = null
|
||||
): ErrorHandler => {
|
||||
const [showError, setShowError] = useState(initialState);
|
||||
const ErrorMessage = (): JSX.Element => (
|
||||
<div className={styles.inputError} {...errorMessageAttributes}>
|
||||
{errMsg}
|
||||
</div>
|
||||
);
|
||||
const [showError, setShowError] = useState(initialState);
|
||||
const ErrorMessage = (): JSX.Element => (
|
||||
<div className={styles.inputError} {...errorMessageAttributes}>
|
||||
{errMsg}
|
||||
</div>
|
||||
);
|
||||
|
||||
return {
|
||||
showError,
|
||||
setShowError,
|
||||
ErrorMessage,
|
||||
};
|
||||
return {
|
||||
showError,
|
||||
setShowError,
|
||||
ErrorMessage,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,49 +1,49 @@
|
||||
import { HTMLAttributes } from "react";
|
||||
import { HTMLAttributes, JSX } from "react";
|
||||
|
||||
import { useErrorHandling } from "./useErrorHandling";
|
||||
|
||||
export interface MaxLengthErrorHandler {
|
||||
handleMaxLengthErrorMsg: (value: string) => void;
|
||||
hideError: () => void;
|
||||
MaxLengthErrorMessage: () => JSX.Element;
|
||||
};
|
||||
handleMaxLengthErrorMsg: (value: string) => void;
|
||||
hideError: () => void;
|
||||
MaxLengthErrorMessage: () => JSX.Element;
|
||||
}
|
||||
|
||||
export const useMaxLengthErrorHandling = (
|
||||
initialState: boolean,
|
||||
maxLength: number | null,
|
||||
errMsg: string = "",
|
||||
errorMessageAttributes?: HTMLAttributes<HTMLDivElement>
|
||||
initialState: boolean,
|
||||
maxLength: number | null,
|
||||
errMsg: string = "",
|
||||
errorMessageAttributes?: HTMLAttributes<HTMLDivElement>
|
||||
): MaxLengthErrorHandler => {
|
||||
errMsg ||= `The max length is ${maxLength} characters.`;
|
||||
errMsg ||= `The max length is ${maxLength} characters.`;
|
||||
|
||||
const {
|
||||
showError,
|
||||
setShowError,
|
||||
ErrorMessage,
|
||||
} = useErrorHandling(initialState, errMsg, errorMessageAttributes);
|
||||
const { showError, setShowError, ErrorMessage } = useErrorHandling(
|
||||
initialState,
|
||||
errMsg,
|
||||
errorMessageAttributes
|
||||
);
|
||||
|
||||
const handleMaxLengthErrorMsg = (value: string): void => {
|
||||
if (!maxLength) {
|
||||
// Skip if maxLength is 0 or not set.
|
||||
return;
|
||||
}
|
||||
|
||||
const isTooLong = (value?.length ?? 0) >= (maxLength ?? 0);
|
||||
if (isTooLong !== showError) {
|
||||
setShowError(isTooLong);
|
||||
}
|
||||
};
|
||||
|
||||
const hideError = () => {
|
||||
setShowError(false);
|
||||
const handleMaxLengthErrorMsg = (value: string): void => {
|
||||
if (!maxLength) {
|
||||
// Skip if maxLength is 0 or not set.
|
||||
return;
|
||||
}
|
||||
|
||||
const MaxLengthErrorMessage = (): JSX.Element => showError && errMsg
|
||||
? (<ErrorMessage />)
|
||||
: <></>;
|
||||
const isTooLong = (value?.length ?? 0) >= (maxLength ?? 0);
|
||||
if (isTooLong !== showError) {
|
||||
setShowError(isTooLong);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
handleMaxLengthErrorMsg,
|
||||
MaxLengthErrorMessage,
|
||||
hideError
|
||||
};
|
||||
}
|
||||
const hideError = () => {
|
||||
setShowError(false);
|
||||
};
|
||||
|
||||
const MaxLengthErrorMessage = (): JSX.Element =>
|
||||
showError && errMsg ? <ErrorMessage /> : <></>;
|
||||
|
||||
return {
|
||||
handleMaxLengthErrorMsg,
|
||||
MaxLengthErrorMessage,
|
||||
hideError,
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user