From 9431f9d6f7c901a77f1024881950b1b987665a43 Mon Sep 17 00:00:00 2001 From: Dombi Attila <83396+dombesz@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:00:39 +0200 Subject: [PATCH 1/5] Add form preview documentation and create a generic form preview stimulus controller --- .../project-life-cycles-form.controller.ts | 13 +- .../controllers/form-preview.controller.ts | 56 ++++++++ frontend/src/stimulus/setup.ts | 2 + lookbook/docs/patterns/02-forms.md.erb | 124 ++++++++++++++++++ lookbook/previews/patterns/forms_preview.rb | 11 ++ .../forms_preview/form_preview.html.erb | 35 +++++ .../sections/edit_component.html.erb | 6 +- 7 files changed, 233 insertions(+), 14 deletions(-) create mode 100644 frontend/src/stimulus/controllers/form-preview.controller.ts create mode 100644 lookbook/previews/patterns/forms_preview/form_preview.html.erb diff --git a/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts b/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts index 1cc2421b730f..ec075b748f56 100644 --- a/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts +++ b/frontend/src/stimulus/controllers/dynamic/overview/project-life-cycles-form.controller.ts @@ -31,22 +31,13 @@ import { Controller } from '@hotwired/stimulus'; export default class ProjectLifeCyclesFormController extends Controller { - static targets = ['form']; - - declare readonly formTarget:HTMLFormElement; - handleChange(event:Event) { const target = event.target as HTMLElement; - const previewUrl = this.formTarget.dataset.previewUrl; - - if (!previewUrl || this.datePickerVisible(target)) { + if (this.datePickerVisible(target)) { return; // flatpickr is still open, do not submit yet. } - const form = this.formTarget; - form.action = previewUrl; - - form.requestSubmit(); + this.dispatch('triggerFormPreview'); } datePickerVisible(element:HTMLElement) { diff --git a/frontend/src/stimulus/controllers/form-preview.controller.ts b/frontend/src/stimulus/controllers/form-preview.controller.ts new file mode 100644 index 000000000000..ce6560fc894d --- /dev/null +++ b/frontend/src/stimulus/controllers/form-preview.controller.ts @@ -0,0 +1,56 @@ +/* + * -- copyright + * OpenProject is an open source project management software. + * Copyright (C) the OpenProject GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License version 3. + * + * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: + * Copyright (C) 2006-2013 Jean-Philippe Lang + * Copyright (C) 2010-2013 the ChiliProject Team + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * See COPYRIGHT and LICENSE files for more details. + * ++ + */ + +import { ApplicationController } from 'stimulus-use'; + +export default class FormPreviewController extends ApplicationController { + static values = { url: String }; + + declare readonly formTarget:HTMLFormElement; + declare urlValue:string; + + connect() { + // Ensure this.element is a form element + if (!(this.element instanceof HTMLFormElement)) { + throw new Error('The controller must be bound to a