Skip to content

Commit

Permalink
fix(nested-forms): stack loading nested forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed May 8, 2024
1 parent deadffe commit b2caa6e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/simple-form/src/pages/multi-forms.astro
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ function submit(){
<BindForm bind={bind1}>
<BInput type='int' name='num' />
<BButton onClick={submit} extra={bind1}>Increase</BButton>
</BindForm>

<BindForm bind={bind2}>
<BInput type='int' name='num' />
<BButton onClick={submit} extra={bind2}>Increase</BButton>
<BindForm bind={bind2}>
<BInput type='int' name='num' />
<BButton onClick={submit} extra={bind2}>Increase</BButton>
</BindForm>
</BindForm>
</Layout>
4 changes: 2 additions & 2 deletions packages/context/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export async function asyncContext<T>(promise: () => Promise<T>, astro: ContextA
const contextHistory = getContextHistory(astro, name);

contextHistory.push({
...(context ?? astro.props),
...contextHistory.at(-1)
...contextHistory.at(-1),
...(context ?? astro.props)
});

let resolver: () => void | null;
Expand Down
13 changes: 11 additions & 2 deletions packages/forms/src/components/WebForms.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ const context = {
webFormsSettings: { haveFileUpload: false },
tempValues: {},
viewStates: {
counter: 0,
store: {},
counterHash: new Map<any, number>(),
lastCounter: 0,
incCounter(ref: any){
const haveCounter = context.viewStates.counterHash.get(ref);
if(haveCounter) {
return haveCounter;
}
context.viewStates.counterHash.set(ref, context.viewStates.lastCounter);
return context.viewStates.lastCounter++;
}
},
};
Expand Down
12 changes: 6 additions & 6 deletions packages/forms/src/components/form/BindForm.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface Props {
defaultSubmitClick?: string | false;
}
const { viewStates } = getContext(Astro, '@astro-utils/forms');
const { viewStates, bindCounter: parantBindCounter = '' } = getContext(Astro, '@astro-utils/forms');
const { bind = Bind(), defaultSubmitClick } = Astro.props;
const context = {
executeAfter: [],
Expand All @@ -22,7 +22,7 @@ const context = {
onSubmitClickGlobal: defaultSubmitClick,
buttonIds: [] as [string, string | null, boolean][],
settings: {showValidationErrors: false},
bindCounter: viewStates.counter++
bindCounter: viewStates.incCounter(bind)
};
const viewState = new ViewStateManager(bind, context.elementsState, Astro, context.bindCounter);
Expand All @@ -37,15 +37,15 @@ if (!haveState) {
}
// Get information about the form
await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' });
await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' + parantBindCounter });
// resert the form execution
context.tempBindValues = {};
context.executeAfter = [];
// first 'GET' render
context.method = Astro.request.method;
let htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' });
let htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' + parantBindCounter });
bind.__finishFormValidation();
for (const func of context.executeAfter) {
Expand All @@ -55,7 +55,7 @@ for (const func of context.executeAfter) {
if (context.method == 'POST') {
context.method = 'GET';
context.tempBindValues = {};
htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' });
htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' + parantBindCounter });
}
// Edit form render, add default submit button
Expand All @@ -67,7 +67,7 @@ if (context.onSubmitClickGlobal == null && context.buttonIds.length > 0) {
context.onSubmitClickGlobal = state.id;
context.tempBindValues = {};
htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' });
htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, { name: '@astro-utils/forms', context, lock: 'bindForm' + parantBindCounter });
}
---

Expand Down

0 comments on commit b2caa6e

Please sign in to comment.