Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method allowing to retrieve a field by name #485

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export interface ActionInfo {
* Returns the list of fields associated to an action
*/
fields: Field[];

}

/**
Expand All @@ -51,6 +50,10 @@ export interface Action<T extends Record<string, any> = Record<string, any>> ext
*/
submit(formData: T): Promise<State>;

/**
* Return a field by name.
*/
field(name: string): Field | undefined;
}

export class SimpleAction<TFormData extends Record<string, any>> implements Action {
Expand Down Expand Up @@ -157,6 +160,10 @@ export class SimpleAction<TFormData extends Record<string, any>> implements Acti
return state;

}

field(name: string): Field | undefined {
return this.fields.find(field => field.name === name);
}
}

export class ActionNotFound extends Error {}
2 changes: 1 addition & 1 deletion test/unit/state/hal-forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { factory } from '../../../src/state/hal';
import { Action, Client, Field } from '../../../src';
import { HalFormsProperty } from 'hal-types';

type CompareAction = Omit<Action, 'submit'>;
type CompareAction = Omit<Omit<Action, 'submit'>, 'field'>;
reda-alaoui marked this conversation as resolved.
Show resolved Hide resolved

describe('HAL forms', () => {

Expand Down