-
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.
feat(sectionedForm): initial architecture and base components (#431)
* feat(sectionedForm): initial SectionedForm architecture * fix: add showcase for formstructure * feat(dataSet): dataSet sectionedForm * feat: sectioned form router and fixes * fix: some cleanup * refactor: cleanup * refactor: more cleanup * refactor: remove unused file * refactor: cleanup and fix imports * fix: call submit from footer * fix: some cleanup * refactor: rename context * fix: add error noticebox * feat: add section in one page - update selection by scroll * refactor: remove unused code * Revert "fix: add error noticebox" This reverts commit 50e8bb3. * fix: errornotice after revert * fix(errorbox): allow to close box, fix styling * fix: cleanup error notice * fix: fix selectedsection scroll syncing * fix: cleanup * fix: fix import after bad merge * feat: stop hiding form tab and give some temp vertical space to not yet developed sections * feat: move data set form to different route temporarelly --------- Co-authored-by: Flaminia Cavallo <flaminia@dhis2.org>
- Loading branch information
Showing
31 changed files
with
1,156 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.noticeBox { | ||
max-width: 640px; | ||
} | ||
|
||
.errorList { | ||
padding: 0; | ||
} |
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,56 @@ | ||
import { FormState, FormSubscription } from 'final-form' | ||
import { useFormState } from 'react-final-form' | ||
|
||
const formStateSubscriptions = { | ||
errors: true, | ||
submitError: true, | ||
submitFailed: true, | ||
hasValidationErrors: true, | ||
hasSubmitErrors: true, | ||
dirtySinceLastSubmit: true, | ||
modifiedSinceLastSubmit: true, | ||
} satisfies FormSubscription | ||
|
||
type FinalFormErrorProps = Pick< | ||
FormState<unknown>, | ||
keyof typeof formStateSubscriptions | ||
> | ||
|
||
export type FormErrorState = Omit<FinalFormErrorProps, 'errors'> & { | ||
// helper to decide wheter a noticebox should be shown | ||
shouldShowErrors: boolean | ||
// we rename "errors" to "validationErrors" to make it more clear that it only contain validation errors | ||
validationErrors: Record<string, string> | undefined | ||
} | ||
|
||
export const useFormStateErrors = (): FormErrorState => { | ||
const { | ||
dirtySinceLastSubmit, | ||
errors, | ||
hasSubmitErrors, | ||
hasValidationErrors, | ||
submitError, | ||
submitFailed, | ||
modifiedSinceLastSubmit, | ||
}: FinalFormErrorProps = useFormState({ | ||
subscription: formStateSubscriptions, | ||
}) | ||
|
||
const hasAnyError = !!(hasSubmitErrors || hasValidationErrors) | ||
|
||
// should only show errors after trying to submit | ||
const shouldShowErrors = | ||
(hasAnyError && submitFailed && !dirtySinceLastSubmit) || | ||
(submitFailed && hasSubmitErrors) | ||
|
||
return { | ||
dirtySinceLastSubmit, | ||
hasSubmitErrors, | ||
hasValidationErrors, | ||
shouldShowErrors, | ||
submitError, | ||
submitFailed, | ||
validationErrors: errors, | ||
modifiedSinceLastSubmit, | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
src/components/sectionedForm/DefaultSectionedFormFooter.tsx
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,59 @@ | ||
import i18n from '@dhis2/d2-i18n' | ||
import { Button } from '@dhis2/ui' | ||
import React from 'react' | ||
import { useForm } from 'react-final-form' | ||
import { useSectionedFormContext, useSelectedSection } from '../../lib' | ||
import { LinkButton } from '../LinkButton' | ||
import { SectionedFormFooter } from './SectionedFormFooter' | ||
|
||
export const DefaultSectionedFormFooter = () => { | ||
const descriptor = useSectionedFormContext() | ||
const [selected, setSelectedSection] = useSelectedSection() | ||
|
||
const { submit } = useForm() | ||
const currSelectedIndex = descriptor.sections.findIndex( | ||
(s) => s.name === selected | ||
) | ||
const prevSection = descriptor.sections[currSelectedIndex - 1] | ||
const nextSection = descriptor.sections[currSelectedIndex + 1] | ||
|
||
const handleNavigateBack = () => { | ||
if (prevSection) { | ||
setSelectedSection(prevSection.name) | ||
} | ||
} | ||
const handleNavigateNext = () => { | ||
if (nextSection) { | ||
setSelectedSection(nextSection.name) | ||
} | ||
} | ||
|
||
return ( | ||
<SectionedFormFooter> | ||
<SectionedFormFooter.SectionActions> | ||
{prevSection && ( | ||
<Button small onClick={handleNavigateBack}> | ||
{i18n.t('Go back')} | ||
</Button> | ||
)} | ||
{nextSection && ( | ||
<Button | ||
style={{ marginInlineStart: 'auto' }} | ||
small | ||
onClick={handleNavigateNext} | ||
> | ||
{i18n.t('Next section')} | ||
</Button> | ||
)} | ||
</SectionedFormFooter.SectionActions> | ||
<SectionedFormFooter.FormActions> | ||
<Button primary type="submit" onClick={submit}> | ||
{i18n.t('Save and exit')} | ||
</Button> | ||
<LinkButton to={'..'}> | ||
{i18n.t('Exit without saving')} | ||
</LinkButton> | ||
</SectionedFormFooter.FormActions> | ||
</SectionedFormFooter> | ||
) | ||
} |
23 changes: 23 additions & 0 deletions
23
src/components/sectionedForm/DefaultSectionedFormSidebar.tsx
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,23 @@ | ||
import React from 'react' | ||
import { useSectionedFormContext, useSelectedSection } from '../../lib' | ||
import { | ||
SectionedFormSidebar, | ||
SectionedFormSidebarItem, | ||
} from './SectionedFormSidebar' | ||
|
||
export const DefaultSectionedFormSidebar = () => { | ||
const { sections } = useSectionedFormContext() | ||
|
||
const [selected] = useSelectedSection() | ||
|
||
const items = sections.map((section) => ( | ||
<SectionedFormSidebarItem | ||
key={section.name} | ||
active={selected === section.name} | ||
sectionName={section.name} | ||
> | ||
{section.label} | ||
</SectionedFormSidebarItem> | ||
)) | ||
return <SectionedFormSidebar>{items}</SectionedFormSidebar> | ||
} |
Oops, something went wrong.