generated from gridsuite/gridapp-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement round trip between redux and react hook form for expert filter
- Loading branch information
Showing
7 changed files
with
149 additions
and
94 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
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
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,27 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
import { usePrevious } from '@gridsuite/commons-ui'; | ||
import { useEffect } from 'react'; | ||
import isDeepEqualReact from 'fast-deep-equal/react'; | ||
import { FieldValues, UseFormReturn } from 'react-hook-form'; | ||
|
||
const useFormChange = ( | ||
formApi: UseFormReturn, | ||
onChange: (formData: FieldValues) => void | ||
) => { | ||
const formData = formApi.watch(); | ||
const prevFormData = usePrevious(formData); | ||
|
||
useEffect(() => { | ||
if (!isDeepEqualReact(prevFormData, formData)) { | ||
console.log('handleSubmit', { formData }); | ||
formApi.handleSubmit(onChange)(); | ||
} | ||
}, [formApi, onChange, formData, prevFormData]); | ||
}; | ||
|
||
export default useFormChange; |
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,28 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { usePrevious } from '@gridsuite/commons-ui'; | ||
import { useEffect } from 'react'; | ||
import isDeepEqualReact from 'fast-deep-equal/react'; | ||
import { UseFormReturn } from 'react-hook-form'; | ||
|
||
const useFormUpdate = (formApi: UseFormReturn, data: any) => { | ||
const prevData = usePrevious(data); | ||
|
||
useEffect(() => { | ||
if (!isDeepEqualReact(prevData, data)) { | ||
console.log('setValue', { data }); | ||
const setValueToKey = ([key, value]: [string, any]) => { | ||
formApi.setValue(key, value, { shouldDirty: false }); | ||
}; | ||
|
||
Object.entries(data).forEach(setValueToKey); | ||
} | ||
}, [formApi, data, prevData]); | ||
}; | ||
|
||
export default useFormUpdate; |
Oops, something went wrong.