Support rest of form components in pc.form (#1070)

This commit is contained in:
Nikhil Rao 2023-05-24 10:34:22 -07:00 committed by GitHub
parent f4d5a08faf
commit 2fa6859035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -289,3 +289,16 @@ export const preventDefault = (event) => {
event.preventDefault();
}
};
/**
* Get the value from a ref.
* @param ref The ref to get the value from.
* @returns The value.
*/
export const getRefValue = (ref) => {
if (ref.current.type == "checkbox") {
return ref.current.checked;
} else {
return ref.current.value;
}
}

View File

@ -28,6 +28,7 @@ DEFAULT_IMPORTS: imports.ImportDict = {
ImportVar(tag="isTrue"),
ImportVar(tag="preventDefault"),
ImportVar(tag="refs"),
ImportVar(tag="getRefValue"),
},
"": {ImportVar(tag="focus-visible/dist/focus-visible")},
"@chakra-ui/react": {ImportVar(tag=constants.USE_COLOR_MODE)},

View File

@ -4,7 +4,7 @@ from .button import Button, ButtonGroup
from .checkbox import Checkbox, CheckboxGroup
from .copytoclipboard import CopyToClipboard
from .editable import Editable, EditableInput, EditablePreview, EditableTextarea
from .formcontrol import Form, FormControl, FormErrorMessage, FormHelperText, FormLabel
from .form import Form, FormControl, FormErrorMessage, FormHelperText, FormLabel
from .iconbutton import IconButton
from .input import Input, InputGroup, InputLeftAddon, InputRightAddon
from .numberinput import (

View File

@ -23,7 +23,7 @@ class Form(ChakraComponent):
# Send all the input refs to the handler.
return {
"on_submit": {
ref[4:]: Var.create(f"{ref}.current.value", is_local=False)
ref[4:]: Var.create(f"getRefValue({ref})", is_local=False)
for ref in self.get_refs()
}
}