-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add component to disable validation updates (#9)
- Loading branch information
Showing
7 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@radiantguild/form-contexts": minor | ||
--- | ||
|
||
Added a `<NoValidationUpdates>` component that disables any validation update calls in its children. See `useValidationUpdate`’s docs for more info. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {createContext} from "react"; | ||
|
||
export const NoValidationUpdateContext = createContext<boolean>(false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
import {ValidateResult} from "@radiantguild/yoogi"; | ||
import {useContext, useEffect} from "react"; | ||
import {ValidationSetterContext} from "~/contexts/ValidationSetterContext"; | ||
import {NoValidationUpdateContext} from "~/contexts/NoValidationUpdateContext"; | ||
|
||
/** | ||
* Sets the validation result of this input so that `useValidation` can read it | ||
* Sets the validation result of this input so that `useValidation` can read it. | ||
* | ||
* If a composite form field includes sub-fields with `useValidationUpdate` (or `useInputState`), | ||
* the composite form field should wrap those elements in `<NoValidationUpdates>` to prevent clashing updates. | ||
* | ||
* @see ValidationProvider | ||
* @see useValidation | ||
* @see NoValidationUpdates | ||
*/ | ||
export default function useValidationUpdate(result: ValidateResult | null): void { | ||
const setValidateResult = useContext(ValidationSetterContext); | ||
const disabled = useContext(NoValidationUpdateContext); | ||
|
||
useEffect(() => { | ||
if (disabled) return; | ||
setValidateResult?.update(result); | ||
}, [setValidateResult, result]); | ||
}, [disabled, setValidateResult, result]); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {createElement, ReactNode} from "react"; | ||
import {NoValidationUpdateContext} from "~/contexts/NoValidationUpdateContext"; | ||
|
||
export interface NoValidationUpdatesProps { | ||
children: ReactNode; | ||
} | ||
|
||
/** | ||
* Nullifies any `useValidationUpdate` calls by child nodes | ||
* | ||
* @see useValidationUpdate | ||
*/ | ||
export function NoValidationUpdates({children}: NoValidationUpdatesProps) { | ||
return createElement( | ||
NoValidationUpdateContext.Provider, | ||
{value: true}, | ||
children | ||
) | ||
} |