From bbe7b9da07862964e9d954635d3bbc532a96ffe5 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 18 Nov 2026 06:45:37 +0000 Subject: [PATCH] feat: add change events to Checkbox and Rating input elements (#6734) --- .../contrib/builder/elements/element_types.py | 6 + .../test_workflow_actions_views.py | 6 +- .../builder/elements/test_element_types.py | 6 + .../workflow_actions/test_change_events.py | 23 +- .../test_workflow_actions_service.py | 8 + ...e_events_to_checkbox_and_rating_input.json | 9 + .../elements/components/CheckboxElement.vue | 9 +- .../components/RatingInputElement.vue | 13 +- web-frontend/modules/builder/elementTypes.js | 8 + .../ChangeableValueElements.spec.js | 371 ++++++++++++++++++ .../components/CheckboxElement.spec.js | 2 +- .../components/ClickableContainers.spec.js | 22 ++ 12 files changed, 457 insertions(+), 26 deletions(-) create mode 100644 changelog/entries/unreleased/feature/2748_add_change_events_to_checkbox_and_rating_input.json create mode 100644 web-frontend/test/unit/builder/components/elements/components/ChangeableValueElements.spec.js diff --git a/backend/src/baserow/contrib/builder/elements/element_types.py b/backend/src/baserow/contrib/builder/elements/element_types.py index 8fe9ed654e..3544809d45 100644 --- a/backend/src/baserow/contrib/builder/elements/element_types.py +++ b/backend/src/baserow/contrib/builder/elements/element_types.py @@ -1514,6 +1514,9 @@ class RatingInputElementType(InputElementType): ] simple_formula_fields = ["value", "label"] + def get_event_names(self, instance: RatingInputElement) -> List[str]: + return [EventTypes.CHANGE.value] + class SerializedDict(ElementDict): label: BaserowFormulaObject required: bool @@ -1786,6 +1789,9 @@ class CheckboxElementType(InputElementType): serializer_field_names = ["label", "default_value", "required"] simple_formula_fields = ["label", "default_value"] + def get_event_names(self, instance: CheckboxElement) -> List[str]: + return [EventTypes.CHANGE.value] + class SerializedDict(ElementDict): label: BaserowFormulaObject required: bool diff --git a/backend/tests/baserow/contrib/builder/api/workflow_actions/test_workflow_actions_views.py b/backend/tests/baserow/contrib/builder/api/workflow_actions/test_workflow_actions_views.py index 5179e79fc3..e3740f33ab 100644 --- a/backend/tests/baserow/contrib/builder/api/workflow_actions/test_workflow_actions_views.py +++ b/backend/tests/baserow/contrib/builder/api/workflow_actions/test_workflow_actions_views.py @@ -168,6 +168,8 @@ def test_start_workflow_commits_history_before_scheduling( ("simple_container", EventTypes.CLICK), ("choice", EventTypes.CHANGE), ("record_selector", EventTypes.CHANGE), + ("checkbox", EventTypes.CHANGE), + ("rating_input", EventTypes.CHANGE), ], ) def test_create_notification_workflow_action( @@ -348,8 +350,8 @@ def test_create_workflow_action_invalid_event( ("record_selector", EventTypes.CLICK), ("button", EventTypes.CHANGE), ("input_text", EventTypes.CHANGE), - ("checkbox", EventTypes.CHANGE), - ("rating_input", EventTypes.CHANGE), + ("checkbox", EventTypes.CLICK), + ("rating_input", EventTypes.CLICK), ], ) def test_create_workflow_action_rejects_unsupported_change_event( diff --git a/backend/tests/baserow/contrib/builder/elements/test_element_types.py b/backend/tests/baserow/contrib/builder/elements/test_element_types.py index e99a1d2d3c..b41661bd76 100644 --- a/backend/tests/baserow/contrib/builder/elements/test_element_types.py +++ b/backend/tests/baserow/contrib/builder/elements/test_element_types.py @@ -1765,6 +1765,12 @@ def test_element_type_get_event_names(data_fixture): assert get_event_names(button) == ["click"] assert get_event_names(choice) == ["change"] assert get_event_names(record_selector) == ["change"] + checkbox = data_fixture.create_builder_checkbox_element(page=page) + rating_input = data_fixture.create_builder_element( + RatingInputElementType, page=page + ) + assert get_event_names(checkbox) == ["change"] + assert get_event_names(rating_input) == ["change"] assert get_event_names(form) == ["submit"] assert get_event_names(table) == [f"{button_field.uid}_click"] assert get_event_names(menu) == [f"{button_item_uid}_click"] diff --git a/backend/tests/baserow/contrib/builder/workflow_actions/test_change_events.py b/backend/tests/baserow/contrib/builder/workflow_actions/test_change_events.py index 8d5a00c585..6f0fa926ab 100644 --- a/backend/tests/baserow/contrib/builder/workflow_actions/test_change_events.py +++ b/backend/tests/baserow/contrib/builder/workflow_actions/test_change_events.py @@ -27,7 +27,7 @@ from baserow.core.graph.types import GraphPointPosition from baserow.core.utils import MirrorDict -@pytest.fixture(params=["choice", "record_selector"]) +@pytest.fixture(params=["choice", "record_selector", "checkbox", "rating_input"]) def change_action(data_fixture, request): user = data_fixture.create_user() draft = data_fixture.create_builder_application(user=user) @@ -62,11 +62,10 @@ def change_action(data_fixture, request): repeat = data_fixture.create_builder_repeat_element( page=page, data_source=record_source ) - element_kwargs = ( - {"data_source": option_source} - if request.param == "record_selector" - else {"options": [{"name": "Selected", "value": "selected"}]} - ) + element_kwargs = { + "choice": {"options": [{"name": "Selected", "value": "selected"}]}, + "record_selector": {"data_source": option_source}, + }.get(request.param, {}) element = data_fixture.create_builder_element( type(element_type_registry.get(request.param)), page=page, @@ -75,9 +74,12 @@ def change_action(data_fixture, request): required=True, **element_kwargs, ) - selected = ( - str(option_rows[2].id) if request.param == "record_selector" else "selected" - ) + selected, rendered = { + "choice": ("selected", "selected"), + "record_selector": (str(option_rows[2].id), str(option_rows[2].id)), + "checkbox": (True, "true"), + "rating_input": (3, "3"), + }[request.param] service = data_fixture.create_local_baserow_upsert_row_service( integration=integration, table=output ) @@ -103,6 +105,7 @@ def change_action(data_fixture, request): "output_field": output_fields[0], "action": action, "selected": selected, + "rendered": rendered, } @@ -135,7 +138,7 @@ def test_public_change_action_reads_new_value_and_enclosing_record( fixture["output"] .get_model() .objects.values_list(fixture["output_field"].db_column, flat=True) - ) == [f"Beta:{fixture['selected']}"] + ) == [f"Beta:{fixture['rendered']}"] @pytest.mark.django_db diff --git a/backend/tests/baserow/contrib/builder/workflow_actions/test_workflow_actions_service.py b/backend/tests/baserow/contrib/builder/workflow_actions/test_workflow_actions_service.py index 27872a05ea..c0ee4ef419 100644 --- a/backend/tests/baserow/contrib/builder/workflow_actions/test_workflow_actions_service.py +++ b/backend/tests/baserow/contrib/builder/workflow_actions/test_workflow_actions_service.py @@ -43,6 +43,8 @@ from baserow.core.exceptions import PermissionDenied, UserNotInWorkspace ("simple_container", EventTypes.CLICK), ("choice", EventTypes.CHANGE), ("record_selector", EventTypes.CHANGE), + ("checkbox", EventTypes.CHANGE), + ("rating_input", EventTypes.CHANGE), ], ) def test_create_workflow_action(data_fixture, element_type, event): @@ -117,6 +119,8 @@ def test_delete_workflow_action_no_permissions(data_fixture): ("button", EventTypes.CLICK), ("choice", EventTypes.CHANGE), ("record_selector", EventTypes.CHANGE), + ("checkbox", EventTypes.CHANGE), + ("rating_input", EventTypes.CHANGE), ], ) def test_update_workflow_action(data_fixture, element_type, event): @@ -395,6 +399,8 @@ def test_dispatch_workflow_action_no_permissions(data_fixture): ("simple_container", EventTypes.CLICK), ("choice", EventTypes.CHANGE), ("record_selector", EventTypes.CHANGE), + ("checkbox", EventTypes.CHANGE), + ("rating_input", EventTypes.CHANGE), ], ) def test_create_workflow_action_with_invalid_event( @@ -481,6 +487,8 @@ def test_create_workflow_action_without_element_only_accepts_static_events( ("simple_container", EventTypes.CLICK), ("choice", EventTypes.CHANGE), ("record_selector", EventTypes.CHANGE), + ("checkbox", EventTypes.CHANGE), + ("rating_input", EventTypes.CHANGE), ], ) def test_update_workflow_action_with_invalid_event( diff --git a/changelog/entries/unreleased/feature/2748_add_change_events_to_checkbox_and_rating_input.json b/changelog/entries/unreleased/feature/2748_add_change_events_to_checkbox_and_rating_input.json new file mode 100644 index 0000000000..c8ab67615f --- /dev/null +++ b/changelog/entries/unreleased/feature/2748_add_change_events_to_checkbox_and_rating_input.json @@ -0,0 +1,9 @@ +{ + "type": "feature", + "message": "Checkbox and Rating input elements can trigger actions when their value changes in the Application Builder.", + "issue_origin": "github", + "issue_number": 2748, + "domain": "builder", + "bullet_points": [], + "created_at": "2026-11-16" +} diff --git a/web-frontend/modules/builder/components/elements/components/CheckboxElement.vue b/web-frontend/modules/builder/components/elements/components/CheckboxElement.vue index 76b52de30d..ee05430cbf 100644 --- a/web-frontend/modules/builder/components/elements/components/CheckboxElement.vue +++ b/web-frontend/modules/builder/components/elements/components/CheckboxElement.vue @@ -4,10 +4,11 @@ :style="getStyleOverride('input')" > diff --git a/web-frontend/modules/builder/elementTypes.js b/web-frontend/modules/builder/elementTypes.js index c3a9cd2348..f2c451d476 100644 --- a/web-frontend/modules/builder/elementTypes.js +++ b/web-frontend/modules/builder/elementTypes.js @@ -2131,6 +2131,10 @@ export class CheckboxElementType extends FormElementType { return 'checkbox' } + getEvents(element) { + return [new ChangeEvent({ app: this.app })] + } + get name() { return this.app.$i18n.t('elementType.checkbox') } @@ -2721,6 +2725,10 @@ export class RatingInputElementType extends FormElementType { return 'rating_input' } + getEvents(element) { + return [new ChangeEvent({ app: this.app })] + } + get name() { return this.app.$i18n.t('elementType.ratingInput') } diff --git a/web-frontend/test/unit/builder/components/elements/components/ChangeableValueElements.spec.js b/web-frontend/test/unit/builder/components/elements/components/ChangeableValueElements.spec.js new file mode 100644 index 0000000000..0d9190671c --- /dev/null +++ b/web-frontend/test/unit/builder/components/elements/components/ChangeableValueElements.spec.js @@ -0,0 +1,371 @@ +import { reactive } from 'vue' +import flushPromises from 'flush-promises' +import { TestApp } from '@baserow/test/helpers/testApp' +import CheckboxElement from '@baserow/modules/builder/components/elements/components/CheckboxElement.vue' +import RatingInputElement from '@baserow/modules/builder/components/elements/components/RatingInputElement.vue' +import PageElement from '@baserow/modules/builder/components/page/PageElement.vue' +import { DATA_SCOPES } from '@baserow/modules/builder/constants' + +const CASES = { + checkbox: { + component: CheckboxElement, + initial: 'false', + initialFormula: { default_value: { mode: 'raw', formula: 'false' } }, + // The first and second user changes and the value each one stores. + changed: ['true', 'false'], + // A click toggles the native checkbox, as a user would. + change: async (wrapper) => { + await wrapper.get('.ab-checkbox input').trigger('click') + await flushPromises() + }, + current: (wrapper) => wrapper.get('.ab-checkbox input').element.checked, + initialValue: false, + changedValue: true, + isBlocked: (wrapper) => + wrapper.get('.ab-checkbox input').attributes('disabled') !== undefined, + }, + rating_input: { + component: RatingInputElement, + initial: '1', + initialFormula: { value: { mode: 'raw', formula: '1' } }, + changed: ['3', '4'], + change: async (wrapper, step) => { + await wrapper + .findAll('.rating__star') + [step === 0 ? 2 : 3].trigger('click') + await flushPromises() + }, + current: (wrapper) => wrapper.findAll('.rating__star--selected').length, + initialValue: 1, + changedValue: 3, + isBlocked: (wrapper) => + !wrapper.get('.rating').classes().includes('editing'), + }, +} + +describe('Value change workflows', () => { + let testApp + + beforeEach(() => { + testApp = new TestApp() + }) + + afterEach(async () => { + if (vi.isFakeTimers()) { + await vi.runOnlyPendingTimersAsync() + vi.useRealTimers() + } + vi.restoreAllMocks() + await testApp.afterEach() + }) + + const mountValueElement = async ( + type, + { + elementValues = {}, + mode = 'public', + contextMode = mode, + withForm = false, + withRepeat = false, + } = {} + ) => { + const element = reactive({ + id: 12, + type, + page_id: 1, + css_classes: 'qa-value', + styles: {}, + style_background_file: null, + visibility: 'all', + label: { mode: 'raw', formula: 'Value' }, + required: false, + max_value: 5, + color: 'primary', + rating_style: 'star', + ...CASES[type].initialFormula, + ...elementValues, + }) + const form = { + ...element, + id: 11, + type: 'form_container', + css_classes: 'qa-form', + submit_button_label: { mode: 'raw', formula: 'Submit' }, + reset_initial_values_post_submission: true, + } + const repeat = { + ...element, + id: 10, + type: 'repeat', + data_source_id: 2, + schema_property: null, + } + const graph = { 12: {} } + if (withForm) { + graph[11] = { children: { '': [12] } } + } + if (withRepeat) { + graph[10] = { children: { '': [withForm ? 11 : 12] } } + } + graph[0] = withRepeat ? 10 : withForm ? 11 : 12 + const page = reactive({ + id: 1, + path: '/', + path_params: [], + query_params: [], + shared: false, + dataSources: [ + { + id: 2, + type: 'csv_file_reader', + name: 'Records', + schema: { + type: 'array', + items: { + type: 'object', + properties: { + id: { type: 'number' }, + name: { type: 'string' }, + }, + }, + }, + }, + ], + elements: [], + workflowActions: [], + graph, + }) + const sharedPage = { id: 2, shared: true, elements: [], dataSources: [] } + const builder = { id: 1, theme: {}, pages: [page, sharedPage] } + for (const item of [ + ...(withRepeat ? [repeat] : []), + ...(withForm ? [form] : []), + element, + ]) { + await testApp.store.dispatch('element/forceCreate', { + page, + element: item, + }) + } + if (withRepeat) { + testApp.store.commit('elementContent/SET_CONTENT', { + element: repeat, + value: [ + { id: 101, name: 'Outer Alpha' }, + { id: 102, name: 'Outer Beta' }, + ], + }) + } + await testApp.store.dispatch('builderWorkflowAction/forceCreate', { + page, + workflowAction: { + id: 20, + element_id: element.id, + page_id: page.id, + type: 'notification', + event: 'change', + order: 1, + title: { formula: "get('form_data.12')" }, + description: withRepeat + ? { formula: "get('current_record.name')" } + : { mode: 'raw', formula: 'Value changed' }, + }, + }) + if (withForm) { + await testApp.store.dispatch('builderWorkflowAction/forceCreate', { + page, + workflowAction: { + id: 21, + element_id: form.id, + page_id: page.id, + type: 'notification', + event: 'submit', + order: 1, + title: { mode: 'raw', formula: 'Form submitted' }, + description: { mode: 'raw', formula: '' }, + }, + }) + } + const root = withForm ? form : element + const wrapper = await testApp.mount( + withForm ? PageElement : CASES[type].component, + { + props: { element: root }, + global: { + provide: { + workspace: {}, + builder, + currentPage: page, + elementPage: page, + mode, + applicationContext: { + builder, + page, + element: root, + mode: contextMode, + dataScope: DATA_SCOPES.EXCLUDE_TARGET_SUBTREE, + recordIndexPath: withRepeat ? [1] : [], + ...(withRepeat ? { recordId: 102 } : {}), + }, + }, + }, + } + ) + await flushPromises() + return { wrapper, element, page, builder } + } + + const notifications = () => testApp.store.getters['builderToast/all'] + const titles = () => notifications().map(({ title }) => title) + + describe.each(Object.keys(CASES))('%s', (type) => { + const { change, current, changed, initialValue, changedValue, isBlocked } = + CASES[type] + + test('runs one change workflow per user change with the new form value', async () => { + const { wrapper } = await mountValueElement(type) + expect(current(wrapper)).toBe(initialValue) + expect(notifications()).toHaveLength(0) + + await change(wrapper, 0) + + expect(current(wrapper)).toBe(changedValue) + expect(notifications()).toMatchObject([ + { type: 'info-primary', title: changed[0], message: 'Value changed' }, + ]) + + await change(wrapper, 1) + + // Toasts are listed newest first. + expect(titles()).toEqual([changed[1], changed[0]]) + }) + + test('exposes the changed value inside a form without firing on reset', async () => { + const { wrapper } = await mountValueElement(type, { withForm: true }) + await change(wrapper, 0) + expect(titles()).toEqual([changed[0]]) + + await wrapper + .get('.form-container-element__submit-button button') + .trigger('click') + await flushPromises() + + expect(current(wrapper)).toBe(initialValue) + expect(titles()).toEqual(['Form submitted', changed[0]]) + }) + + test('reads the enclosing repeated record', async () => { + const { wrapper } = await mountValueElement(type, { withRepeat: true }) + + await change(wrapper, 0) + + expect(notifications()).toMatchObject([ + { title: changed[0], message: 'Outer Beta' }, + ]) + }) + + test.each(['editing', 'public'])( + 'does not run workflows in an editor copy rendered in %s mode', + async (mode) => { + const { wrapper } = await mountValueElement(type, { + mode, + contextMode: 'editing', + withRepeat: true, + }) + + await change(wrapper, 0) + + expect(notifications()).toHaveLength(0) + } + ) + + test('does not fire on equal store updates or changed default formulas', async () => { + const { wrapper, element, page, builder } = await mountValueElement(type) + await change(wrapper, 0) + expect(notifications()).toHaveLength(1) + + await testApp.store.dispatch('formData/setFormData', { + page, + uniqueElementId: '12', + payload: { + value: changedValue, + elementId: 12, + type: type === 'checkbox' ? 'boolean' : 'number', + isValid: true, + }, + }) + await flushPromises() + expect(notifications()).toHaveLength(1) + + vi.useFakeTimers() + const formulaKey = Object.keys(CASES[type].initialFormula)[0] + await testApp.store.dispatch('element/forceUpdate', { + builder, + page, + element, + values: { + [formulaKey]: { + mode: 'raw', + formula: type === 'checkbox' ? 'true' : '5', + }, + }, + }) + await vi.advanceTimersByTimeAsync(600) + await flushPromises() + + expect(notifications()).toHaveLength(1) + }) + + test('blocks changes until the current workflow completes', async () => { + const actionType = testApp + .getRegistry() + .get('workflowAction', 'notification') + const execute = actionType.execute.bind(actionType) + let finish + const pending = new Promise((resolve) => { + finish = resolve + }) + vi.spyOn(actionType, 'execute').mockImplementationOnce((args) => { + const result = execute(args) + return pending.then(() => result) + }) + const { wrapper } = await mountValueElement(type) + await change(wrapper, 0) + expect(titles()).toEqual([changed[0]]) + expect(isBlocked(wrapper)).toBe(true) + + await change(wrapper, 1) + + expect(current(wrapper)).toBe(changedValue) + expect(notifications()).toHaveLength(1) + + finish() + await flushPromises() + expect(isBlocked(wrapper)).toBe(false) + await change(wrapper, 1) + + // Toasts are listed newest first. + expect(titles()).toEqual([changed[1], changed[0]]) + }) + }) + + test('fires once when the checkbox label is clicked', async () => { + const { wrapper } = await mountValueElement('checkbox') + + await wrapper.get('.ab-checkbox__label').trigger('click') + await flushPromises() + + expect(wrapper.get('.ab-checkbox input').element.checked).toBe(true) + expect(titles()).toEqual(['true']) + }) + + test('fires once when the selected rating is clicked to clear it', async () => { + const { wrapper } = await mountValueElement('rating_input') + + await wrapper.findAll('.rating__star')[0].trigger('click') + await flushPromises() + + expect(wrapper.findAll('.rating__star--selected')).toHaveLength(0) + expect(titles()).toEqual(['0']) + }) +}) diff --git a/web-frontend/test/unit/builder/components/elements/components/CheckboxElement.spec.js b/web-frontend/test/unit/builder/components/elements/components/CheckboxElement.spec.js index e06dacda3d..12cb4a37cb 100644 --- a/web-frontend/test/unit/builder/components/elements/components/CheckboxElement.spec.js +++ b/web-frontend/test/unit/builder/components/elements/components/CheckboxElement.spec.js @@ -15,7 +15,7 @@ describe('CheckboxElement', () => { }) const mountCheckbox = async (label, { required = false } = {}) => { - const page = reactive({ id: 1, elements: [] }) + const page = reactive({ id: 1, elements: [], workflowActions: [] }) const builder = { id: 1, theme: { primary_color: '#ccc' }, pages: [page] } const mode = 'public' const element = { diff --git a/web-frontend/test/unit/builder/components/elements/components/ClickableContainers.spec.js b/web-frontend/test/unit/builder/components/elements/components/ClickableContainers.spec.js index 45f2ece969..e99c362ec7 100644 --- a/web-frontend/test/unit/builder/components/elements/components/ClickableContainers.spec.js +++ b/web-frontend/test/unit/builder/components/elements/components/ClickableContainers.spec.js @@ -333,6 +333,28 @@ describe('Clickable containers', () => { expect(notifications()).toHaveLength(0) }) + test('toggles a child checkbox without triggering a parent action', async () => { + const { wrapper } = await mountTree({ + outerType, + innerType, + childType: 'checkbox', + actionIds: [10, 11], + elementValues: { default_value: { mode: 'raw', formula: 'false' } }, + }) + const checkbox = getElement(wrapper, 12) + const input = checkbox.get('.ab-checkbox input') + + await input.trigger('click') + await flushPromises() + expect(input.element.checked).toBe(true) + + await checkbox.get('.ab-checkbox__label').trigger('click') + await flushPromises() + expect(input.element.checked).toBe(false) + + expect(notifications()).toHaveLength(0) + }) + test('treats a read-only rating as noninteractive child content', async () => { const { wrapper } = await mountTree({ outerType,