mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 01:45:59 +00:00
submit action - run with enter
This commit is contained in:
@@ -1,9 +1,26 @@
|
||||
import React from 'react';
|
||||
import keycodes from './keycodes';
|
||||
|
||||
const FormContext = React.createContext(null);
|
||||
|
||||
export function FormProvider({ children, initialValues }) {
|
||||
export function FormProvider({ children, initialValues = {} }) {
|
||||
const [values, setValues] = React.useState(initialValues);
|
||||
const [submitAction, setSubmitAction] = React.useState(null);
|
||||
const handleEnter = React.useCallback(
|
||||
(e) => {
|
||||
if (e.keyCode == keycodes.enter && submitAction && submitAction.action) {
|
||||
e.preventDefault();
|
||||
submitAction.action(values);
|
||||
}
|
||||
},
|
||||
[submitAction, values]
|
||||
);
|
||||
React.useEffect(() => {
|
||||
document.addEventListener('keyup', handleEnter);
|
||||
return () => {
|
||||
document.removeEventListener('keyup', handleEnter);
|
||||
};
|
||||
}, [handleEnter]);
|
||||
const setFieldValue = React.useCallback(
|
||||
(field, value) =>
|
||||
setValues((v) => ({
|
||||
@@ -16,6 +33,7 @@ export function FormProvider({ children, initialValues }) {
|
||||
values,
|
||||
setValues,
|
||||
setFieldValue,
|
||||
setSubmitAction,
|
||||
};
|
||||
return <FormContext.Provider value={provider}>{children}</FormContext.Provider>;
|
||||
}
|
||||
|
||||
@@ -106,7 +106,10 @@ export function FormSelectField({ label, name, children = null, ...other }) {
|
||||
}
|
||||
|
||||
export function FormSubmit({ onClick, value, ...other }) {
|
||||
const { values } = useForm();
|
||||
const { values, setSubmitAction } = useForm();
|
||||
React.useEffect(() => {
|
||||
setSubmitAction({ action: onClick });
|
||||
}, [onClick]);
|
||||
return <FormStyledButton type="submit" value={value} onClick={() => onClick(values)} {...other} />;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user