Skip to content

Commit

Permalink
Merge pull request #485 from Cosium/field-by-name
Browse files Browse the repository at this point in the history
Add a method allowing to retrieve a field by name
  • Loading branch information
evert authored Sep 22, 2024
2 parents 32c7391 + d0478c8 commit b8abae7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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<Action, 'submit' | 'field'>;

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

Expand Down

0 comments on commit b8abae7

Please sign in to comment.