Skip to content

Commit

Permalink
enabled custom save and reset form buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-wishes committed Oct 9, 2024
1 parent 6a36d64 commit d98fab7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 84 deletions.
53 changes: 32 additions & 21 deletions library/src/components/form/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
type UseFormWatch,
} from "react-hook-form"
import { twMerge } from "tailwind-merge"
import { Button } from "../Button"
import { Button, ButtonGroup } from "../Button"

export interface FormProps<T extends FieldValues> {
control: Control<T>
Expand Down Expand Up @@ -44,6 +44,9 @@ export interface DynamicFormProps<T extends FieldValues>
horizontal?: boolean
hideReset?: boolean
hideSave?: boolean

customSubmitButton?: (props: { disabled: boolean }) => React.ReactNode
customResetButton?: (props: { disabled: boolean }) => React.ReactNode
}

export function DynamicForm<T extends FieldValues>({
Expand All @@ -55,6 +58,8 @@ export function DynamicForm<T extends FieldValues>({
className,
hideSave,
hideReset,
customResetButton,
customSubmitButton,
...props
}: DynamicFormProps<T>) {
const {
Expand Down Expand Up @@ -95,26 +100,32 @@ export function DynamicForm<T extends FieldValues>({

<hr className="border border-border" />

<div className="flex flex-row items-end w-full justify-end mt-4">
{!hideReset && (
<Button
type="reset"
appearance="subtle"
disabled={!isDirty}
>
Reset
</Button>
)}
{!hideSave && (
<Button
appearance="primary"
type="submit"
disabled={!isValid}
>
Save
</Button>
)}
</div>
<ButtonGroup className="justify-end">
{!hideReset &&
(customResetButton ? (
customResetButton({ disabled: !isDirty })
) : (
<Button
type="reset"
appearance="subtle"
disabled={!isDirty}
>
Reset
</Button>
))}
{!hideSave &&
(customSubmitButton ? (
customSubmitButton({ disabled: !isValid })
) : (
<Button
appearance="primary"
type="submit"
disabled={!isValid}
>
Save
</Button>
))}
</ButtonGroup>
</form>
)
}
119 changes: 60 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"mime-types": "^2.1.35",
"react-awesome-slider": "^4.1.0",
"react-day-picker": "^8.10.1",
"react-intl": "^6.7.1",
"react-intl": "^6.7.2",
"react-joyride": "^2.9.2",
"react-router-dom": "^6.26.2",
"react-select": "^5.8.1",
Expand All @@ -118,7 +118,7 @@
"@atlaskit/lozenge": "^11.11.0",
"@atlaskit/pagination": "^14.9.3",
"@atlaskit/side-navigation": "^3.5.3",
"@atlaskit/table-tree": "^10.0.3",
"@atlaskit/table-tree": "^10.0.4",
"@atlaskit/tag": "^12.6.3",
"@atlaskit/tag-group": "^10.6.0",
"@atlaskit/textarea": "^5.6.2",
Expand All @@ -129,7 +129,7 @@
"@babel/traverse": "^7.25.7",
"@babel/types": "^7.25.7",
"@biomejs/biome": "^1.9.3",
"@formatjs/cli": "^6.2.13",
"@formatjs/cli": "^6.2.14",
"@monaco-editor/react": "^4.6.0",
"@total-typescript/ts-reset": "^0.6.1",
"@types/node": "^22.7.5",
Expand Down Expand Up @@ -157,7 +157,7 @@
"stylelint-order": "^6.0.4",
"tailwind-merge": "^2.5.3",
"tailwindcss": "^3.4.13",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"typescript-plugin-css-modules": "^5.1.0",
"vite": "^5.4.8",
"vite-plugin-dts": "^4.2.3",
Expand Down

0 comments on commit d98fab7

Please sign in to comment.