Skip to content

Commit

Permalink
use more coreui-native frontend validation
Browse files Browse the repository at this point in the history
  • Loading branch information
homuch authored and weiber2002 committed May 2, 2024
1 parent 7035f78 commit 9720a53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
3 changes: 0 additions & 3 deletions client/src/scss/_modalForm.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
form {
input.form-control:invalid {
background-color: #fdd;
}
button.form-btn-full-width {
width: 91.2%;
}
Expand Down
19 changes: 12 additions & 7 deletions client/src/views/out/home/history/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CIcon from '@coreui/icons-react'
import {
CButton,
CForm,
CFormControl,
CImage,
CInputGroup,
CInputGroupText,
Expand All @@ -19,6 +20,7 @@ import ReactTooltip from 'react-tooltip'
const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) => {
const [imgPreview, setImgPreview] = useState(new Array(dataForm.people.length).fill(false))
const [pending, setPending] = useState(false)
const [validated, setValidated] = useState(false)
const formRef = useRef(null)
const preventDefaultAndFocus = (e) => {
e.preventDefault()
Expand Down Expand Up @@ -64,8 +66,9 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
reader.readAsDataURL(file)
}
const handleSubmit = async () => {
setValidated(true) // display the validation message & style
// prevent the form from submitting if the form is invalid
if (!formRef.current.reportValidity()) return
if (!formRef.current.checkValidity()) return

setPending(true)
const data = new FormData()
Expand Down Expand Up @@ -105,6 +108,7 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
}
setPending(false)
setVisible(false)
setValidated(false)
refetch()
}

Expand All @@ -115,12 +119,12 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
<CModalTitle>{add ? 'Add a year' : 'Edit this year'} </CModalTitle>
</CModalHeader>
<CModalBody>
<CForm ref={formRef}>
<CForm ref={formRef} validated={validated} noValidate>
<CInputGroup className="mb-3">
<CInputGroupText>
<CIcon icon="cil-calendar" />
</CInputGroupText>
<input
<CFormControl
required
pattern="B\d{2}"
type="text"
Expand All @@ -140,7 +144,7 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
<CInputGroupText>
<CIcon icon="cil-user" />
</CInputGroupText>
<input
<CFormControl
required
type="text"
name="title"
Expand All @@ -162,7 +166,7 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
<CInputGroupText>
<CIcon icon="cil-user" />
</CInputGroupText>
<input
<CFormControl
type="text"
required
className="form-control"
Expand All @@ -179,6 +183,7 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
type="button"
name="name"
onClick={(e) => handlePeopleDelete(e, index)}
disabled={dataForm.people.length === 1 || pending}
>
x
</CButton>
Expand All @@ -191,13 +196,13 @@ const Editor = ({ visible, setVisible, add, dataForm, setDataForm, refetch }) =>
onMouseLeave={() => handleLeaveImgIcon(index)}
/>
<div
className={'position-absolute form-floating-img-container'}
className="position-absolute form-floating-img-container"
hidden={!(imgPreview[index] && dataForm.people[index].img)}
>
<CImage fluid src={dataForm.people[index].img} alt="img preview" />
</div>
</CInputGroupText>
<input
<CFormControl
type="file"
id="formFile"
onChange={(e) => handleChangeImage(e, index)}
Expand Down

0 comments on commit 9720a53

Please sign in to comment.