Skip to content

Commit

Permalink
Merge pull request #12 from MakerXStudio/validation-indicator
Browse files Browse the repository at this point in the history
Validation indicator
  • Loading branch information
staff0rd authored Mar 30, 2023
2 parents ef550b3 + ddead19 commit d396d4b
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 55 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-ears-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@makerx/forms-core': minor
---

Expose helper.formContext
4 changes: 2 additions & 2 deletions packages/core/src/components/ValidatedFormFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const ValidatedFormFactory =
TSchema extends Record<string, unknown>,
K extends FormFieldHelperBase<TSchema>
>(
formFieldHelperFactory: new () => K
formFieldHelperFactory: new (formContext: UseFormReturn<TSchema>) => K
) =>
({
className,
Expand All @@ -65,7 +65,7 @@ export const ValidatedFormFactory =
onSubmit={onSubmit && formContext.handleSubmit(onSubmit)}
>
{typeof children === 'function'
? children(new formFieldHelperFactory())
? children(new formFieldHelperFactory(formContext))
: children}
</form>
</FormProvider>
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/util/FormFieldHelperBase.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { UseFormReturn } from 'react-hook-form';

export class FormFieldHelperBase<TSchema extends Record<string, any>> {
private readonly fieldPrefix: string;
constructor({ fieldPrefix }: { fieldPrefix?: string } = {}) {
constructor(
public formContext: UseFormReturn<TSchema>,
{ fieldPrefix }: { fieldPrefix?: string } = {}
) {
this.fieldPrefix = fieldPrefix ? `${fieldPrefix}.` : '';
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ionic-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>@makerx/forms-ionic</title>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions packages/ionic-example/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 0 additions & 42 deletions packages/ionic-example/src/App.css

This file was deleted.

37 changes: 34 additions & 3 deletions packages/ionic-example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { IonCol, IonContent, IonGrid, IonPage, IonRow } from '@ionic/react';
import {
IonCol,
IonContent,
IonGrid,
IonIcon,
IonPage,
IonRow,
} from '@ionic/react';
import { ValidatedForm, z, zfd } from '@makerx/forms-ionic';
import { checkmarkCircle, closeCircle } from 'ionicons/icons';

/**
* Define schema
Expand Down Expand Up @@ -47,8 +55,31 @@ function App() {
label: 'This is not required',
field: 'myOptionalParagraph',
})}

{helper.submitButton({ label: 'Submit' })}
<IonGrid>
<IonRow class="ion-align-items-center">
<IonCol size="auto">
{helper.submitButton({ label: 'Submit' })}
</IonCol>
<IonCol>
{helper.formContext.formState.isSubmitted &&
(helper.formContext.formState.isSubmitSuccessful ? (
<IonIcon
style={{ marginTop: 6 }}
icon={checkmarkCircle}
size="large"
color="success"
/>
) : (
<IonIcon
style={{ marginTop: 6 }}
icon={closeCircle}
size="large"
color="danger"
/>
))}
</IonCol>
</IonRow>
</IonGrid>
</div>
)}
</ValidatedForm>
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>@makerx/forms-mui</title>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions packages/mui-example/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion packages/mui-example/public/vite.svg

This file was deleted.

14 changes: 12 additions & 2 deletions packages/mui-example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ValidatedForm, z, zfd } from '@makerx/forms-mui';
import { Container, Grid, Typography } from '@mui/material';
import FailIcon from '@mui/icons-material/Cancel';
import SuccessIcon from '@mui/icons-material/CheckCircle';
import { Container, Grid, Stack, Typography } from '@mui/material';
import { formatISO, parseISO } from 'date-fns';

/**
Expand Down Expand Up @@ -63,7 +65,15 @@ function App() {
})}
</Grid>
<Grid item xs={12}>
{helper.submitButton({ label: 'Submit' })}
<Stack direction="row" spacing={1} alignItems="center">
{helper.submitButton({ label: 'Submit' })}
{helper.formContext.formState.isSubmitted &&
(helper.formContext.formState.isSubmitSuccessful ? (
<SuccessIcon color="success" />
) : (
<FailIcon color="error" />
))}
</Stack>
</Grid>
</Grid>
</Container>
Expand Down

0 comments on commit d396d4b

Please sign in to comment.