From 954b558d70b4cdc0568e4a0cba34bc1a17e98465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Tue, 14 Nov 2023 19:09:27 +0100 Subject: [PATCH 1/2] Add a method allowing to retrieve a field by name --- src/action.ts | 9 ++++++++- test/unit/state/hal-forms.ts | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/action.ts b/src/action.ts index c17e75e..8282672 100644 --- a/src/action.ts +++ b/src/action.ts @@ -38,7 +38,6 @@ export interface ActionInfo { * Returns the list of fields associated to an action */ fields: Field[]; - } /** @@ -51,6 +50,10 @@ export interface Action = Record> ext */ submit(formData: T): Promise; + /** + * Return a field by name. + */ + field(name: string): Field | undefined; } export class SimpleAction> implements Action { @@ -157,6 +160,10 @@ export class SimpleAction> implements Acti return state; } + + field(name: string): Field | undefined { + return this.fields.find(field => field.name === name); + } } export class ActionNotFound extends Error {} diff --git a/test/unit/state/hal-forms.ts b/test/unit/state/hal-forms.ts index 8495950..015aa24 100644 --- a/test/unit/state/hal-forms.ts +++ b/test/unit/state/hal-forms.ts @@ -3,7 +3,7 @@ import { factory } from '../../../src/state/hal'; import { Action, Client, Field } from '../../../src'; import { HalFormsProperty } from 'hal-types'; -type CompareAction = Omit; +type CompareAction = Omit, 'field'>; describe('HAL forms', () => { From d0478c8719812a43fa411b1065000e988d12bc7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Thu, 19 Sep 2024 08:38:24 +0200 Subject: [PATCH 2/2] Update test/unit/state/hal-forms.ts Co-authored-by: Evert Pot --- test/unit/state/hal-forms.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/state/hal-forms.ts b/test/unit/state/hal-forms.ts index 015aa24..e4110c3 100644 --- a/test/unit/state/hal-forms.ts +++ b/test/unit/state/hal-forms.ts @@ -3,7 +3,7 @@ import { factory } from '../../../src/state/hal'; import { Action, Client, Field } from '../../../src'; import { HalFormsProperty } from 'hal-types'; -type CompareAction = Omit, 'field'>; +type CompareAction = Omit; describe('HAL forms', () => {