-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #558 from research-software-directory/autosave-pro…
…ject-info Edit software and project improvements
- Loading branch information
Showing
100 changed files
with
3,696 additions
and
3,506 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2022 Dusan Mijatovic (dv4all) | ||
// SPDX-FileCopyrightText: 2022 dv4all | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import {useController} from 'react-hook-form' | ||
import ControlledTextField, {ControlledTextFieldOptions} from '~/components/form/ControlledTextField' | ||
|
||
export type OnSaveProps = { | ||
name: string, | ||
value: string | ||
} | ||
|
||
export type AutosaveControlledTextField = { | ||
control: any | ||
options: ControlledTextFieldOptions | ||
rules?: any | ||
onSaveField: ({name,value}: OnSaveProps) => void | ||
} | ||
|
||
export default function AutosaveControlledTextField({control,options,rules,onSaveField}:AutosaveControlledTextField) { | ||
const {field:{value},fieldState:{isDirty,error}} = useController({ | ||
control, | ||
name: options.name | ||
}) | ||
|
||
// add onBlur fn to muiProps | ||
if (options.muiProps) { | ||
options.muiProps['onBlur'] = onSaveInfo | ||
} else { | ||
// create muiProps | ||
options['muiProps'] = { | ||
onBlur: onSaveInfo | ||
} | ||
} | ||
|
||
/** | ||
* This function is passed to onBlur event of ControlledTextField component. | ||
* We use value from react-hook-form controller because the Controlled component | ||
* will pass a null value rather than an empty string when isNull prop is provided. | ||
*/ | ||
function onSaveInfo() { | ||
if (isDirty === false) return | ||
if (error) return | ||
// console.group('AutosaveControlledTextField') | ||
// console.log('onSaveInfo...', options.name) | ||
// console.log('value...', value) | ||
// console.log('isDirty...', isDirty) | ||
// console.groupEnd() | ||
// call provided save fn | ||
onSaveField({ | ||
name: options.name, | ||
value | ||
}) | ||
} | ||
|
||
return ( | ||
<ControlledTextField | ||
options={options} | ||
control={control} | ||
rules={rules} | ||
/> | ||
) | ||
} |
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
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
Oops, something went wrong.