Skip to content

Commit

Permalink
feat: add modal and fields for create functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
d-rita committed Dec 19, 2024
1 parent fbf3990 commit b0d7646
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/components/modals/CreateModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
Modal,
ModalTitle,
ModalContent,
ModalActions,
Button,
ButtonStrip,
ReactFinalForm,
} from '@dhis2/ui'
import React from 'react'
import i18n from '../../locales'

const { Form } = ReactFinalForm

interface FieldValues {
key: string
namespace?: string
}

interface CreateModalProps {
closeModal: () => void
handleCreate: (FieldValues) => void
children: React.ReactNode
title: string
}

const CreateModal = ({
handleCreate,
closeModal,
children,
title,
}: CreateModalProps) => {
const onSubmit = (values) => {
handleCreate(values)
}

return (
<Modal>
<Form onSubmit={onSubmit}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<ModalTitle>{title}</ModalTitle>
<ModalContent>{children}</ModalContent>
<ModalActions>
<ButtonStrip end>
<Button secondary onClick={closeModal}>
{i18n.t('Cancel')}
</Button>
<Button primary type="submit">
{i18n.t('Add')}
</Button>
</ButtonStrip>
</ModalActions>
</form>
)}
</Form>
</Modal>
)
}

export default CreateModal
41 changes: 41 additions & 0 deletions src/components/modals/Fields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
ReactFinalForm,
InputFieldFF,
hasValue,
composeValidators,
alphaNumeric,
} from '@dhis2/ui'
import React from 'react'
import i18n from '../../locales'

const { Field } = ReactFinalForm

export const KeysField = ({ initialFocus }: { initialFocus?: boolean }) => {
return (
<Field
name="key"
component={InputFieldFF}
required
label={i18n.t('Key')}
validate={composeValidators(hasValue, alphaNumeric)}
initialFocus={initialFocus}
/>
)
}

export const NamespaceField = ({
initialFocus,
}: {
initialFocus?: boolean
}) => {
return (
<Field
name="namespace"
component={InputFieldFF}
required
label={i18n.t('Namespace')}
validate={composeValidators(hasValue, alphaNumeric)}
initialFocus={initialFocus}
/>
)
}

0 comments on commit b0d7646

Please sign in to comment.