Skip to content

Commit

Permalink
feat(form): omit from state
Browse files Browse the repository at this point in the history
  • Loading branch information
ido-pluto committed Apr 27, 2024
1 parent f9fa6f8 commit 663274e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/forms/src/components-control/form-utils/view-state.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {AstroGlobal} from 'astro';
import { AstroGlobal } from 'astro';
import superjson from 'superjson';
import {parseFormData} from '../../form-tools/post.js';
import {FormsSettings, getFormOptions} from '../../settings.js';
import {BindForm} from './bind-form.js';
import { parseFormData } from '../../form-tools/post.js';
import { FormsSettings, getFormOptions } from '../../settings.js';
import { BindForm } from './bind-form.js';
import snappy from 'snappy';
import {getSomeProps} from '../props-utils.js';
import { getSomeProps, omitProps } from '../props-utils.js';
import crypto from 'crypto';

const CRYPTO_ALGORITHM = 'aes-256-ctr';
Expand All @@ -25,6 +25,10 @@ export default class ViewStateManager {
return this._astro.props.state ?? true;
}

get omitProps() {
return this._astro.props.omitState;
}

get useState() {
return this.stateProp && this._astro.request.method === 'POST';
}
Expand All @@ -51,7 +55,7 @@ export default class ViewStateManager {
private async _parseState() {
try {
const state = await this._extractStateFromForm();
if(state == null) return;
if (state == null) return;

const [iv, content] = state.split('.');

Expand Down Expand Up @@ -80,7 +84,9 @@ export default class ViewStateManager {

public async createViewState(): Promise<string> {
const data = {
bind: getSomeProps(this._bind.__getState(), this.stateProp),
bind: this.omitProps ?
omitProps(this._bind.__getState(), this.omitProps):
getSomeProps(this._bind.__getState(), this.stateProp),
elements: this._elementsState
};

Expand Down
17 changes: 17 additions & 0 deletions packages/forms/src/components-control/props-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,20 @@ export function getSomeProps(object: any, props: string[] | true) {
}
return result;
}

/**
* Omit properties from an object, any property that starts with an underscore or is in the props array will be omitted
*/
export function omitProps(object: any, props: string[] | true) {
if (props === true) {
return {};
}

const result = {...object};
for (const prop in object) {
if(props.includes(prop) || prop[0] === '_'){
delete result[prop];
}
}
return result;
}
2 changes: 2 additions & 0 deletions packages/forms/src/components/form/BindForm.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Bind from '../../components-control/form-utils/bind-form.js';
export interface Props {
bind?: ReturnType<typeof Bind>;
state?: boolean | string[];
omitState?: string[];
}
const {viewStates} = getContext(Astro, '@astro-utils/forms');
Expand All @@ -29,6 +30,7 @@ await asyncContext(() => Astro.slots.render('default'), Astro, {name: '@astro-ut
let htmlSolt = await asyncContext(() => Astro.slots.render('default'), Astro, {name: '@astro-utils/forms', context, lock: "bindForm"});
bind.__finishFormValidation();
for (const func of context.executeAfter) {
await (func as any)();
}
Expand Down

0 comments on commit 663274e

Please sign in to comment.