Skip to content

Commit

Permalink
fix: better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Dec 4, 2023
1 parent ead9f54 commit b415f40
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
28 changes: 28 additions & 0 deletions examples/simple-form/src/pages/button.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
import {BButton, BindForm} from '@astro-utils/forms/forms.js';
import 'bootstrap/dist/css/bootstrap.css';
import {Button} from 'reactstrap';
import Layout from '../layouts/Layout.astro';
let showSubmitText: string;
function formSubmit() {
Astro.locals.session.counter ??= 0;
Astro.locals.session.counter++;
showSubmitText = `You clicked ${Astro.locals.session.counter} times`;
}
---
<Layout title="Welcome to Astro Metro.">
<BindForm>
<BindForm>
<BButton as={Button} props={{color: 'info'}} onClick={formSubmit}>Submit</BButton>
{showSubmitText}
</BindForm>

<BindForm>
{showSubmitText}
</BindForm>
</BindForm>

</Layout>
4 changes: 3 additions & 1 deletion examples/simple-form/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import Layout from '../layouts/Layout.astro';
import {BButton, Bind, BindForm, BInput} from '@astro-utils/forms/forms.js';
import {BButton, Bind, BindForm, BInput, FormErrors} from '@astro-utils/forms/forms.js';
import {Button} from 'reactstrap';
import 'bootstrap/dist/css/bootstrap.css';
Expand All @@ -16,7 +16,9 @@ function formSubmit(){
---
<Layout title="Welcome to Astro Metro.">
<BindForm bind={form}>
<FormErrors/>
{showSubmitText}

{Astro.locals.session.counter &&
<p>You have submitted {Astro.locals.session.counter} times.</p>
}
Expand Down
17 changes: 10 additions & 7 deletions packages/forms/components/form/BButton.astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ export interface Props<T extends keyof JSX.IntrinsicElements | React.JSXElementC
const {as: asComponent = 'button', props: componentProps, onClick, whenFormOK, connectId = createUniqueContinuanceName(onClick), ...props} = Astro.props;
const {bind, executeAfter} = getContext(Astro, '@astro-utils/forms');
const checkFormValidation = whenFormOK && !bind?.errors.length || !whenFormOK;
if (isPost(Astro) && await validateAction(Astro, 'button-callback', connectId) && checkFormValidation) {
if (executeAfter) {
executeAfter.push(onClick);
} else {
await onClick();
async function executeFormAction(callback = onClick) {
const checkFormValidation = whenFormOK && !bind?.errors.length || !whenFormOK;
if (isPost(Astro) && await validateAction(Astro, 'button-callback', connectId) && checkFormValidation) {
await callback();
}
}
if (executeAfter) {
executeAfter.push(executeFormAction);
} else {
throw new Error('Use BButton inside a BindForm component');
}
const Component = asComponent as any;
---
<Component type="submit" name="button-callback" value={connectId} {...props} {...componentProps}>
Expand Down

0 comments on commit b415f40

Please sign in to comment.