Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 9 additions & 2 deletions resources/js/components/forms/builder/ActionInspector.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { Button, Field, Icon, Input, TabContent, TabList, Tabs, TabTrigger } from '@ui';
import { computed, ref, watch } from 'vue';
import type { PropType } from 'vue';
import { usePage } from '@inertiajs/vue3';
import PageRuleBuilder from './pages/PageRuleBuilder.vue';
import { injectBuilderContext } from '@/pages/forms/Builder.vue';
Expand All @@ -15,7 +16,12 @@ enum ActionInspectorTabs {
Logic = 'logic',
}

const activeTab = ref<ActionInspectorTabs>(ActionInspectorTabs.Settings);
const props = defineProps({
/** The tab to open on. The logic tree opens straight on Logic. */
defaultTab: { type: String as PropType<ActionInspectorTabs>, default: ActionInspectorTabs.Settings },
});

const activeTab = ref<ActionInspectorTabs>(props.defaultTab);

const pageIndex = computed(() => pages.value.findIndex((p) => p._id === page.value._id));
const isFirstPage = computed(() => pageIndex.value === 0);
Expand Down Expand Up @@ -64,11 +70,12 @@ const pageDestinationOptions = computed(() => {
});
});

// The last page has no Logic tab, so fall back to Settings, including when it opens on Logic.
watch(page, () => {
if (activeTab.value === ActionInspectorTabs.Logic && isLastPage.value) {
activeTab.value = ActionInspectorTabs.Settings;
}
});
}, { immediate: true });

watchEdits(page, () => page.value.button_label, dirty);
watchEdits(page, () => page.value.show_previous_button, dirty);
Expand Down
52 changes: 42 additions & 10 deletions resources/js/components/forms/logic/LogicTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum TreeDensity {
export enum SelectionType {
Field = 'field',
Page = 'page',
Action = 'action',
}

export type Selection = {
Expand All @@ -21,6 +22,7 @@ import LogicTreeFieldset from './LogicTreeFieldset.vue';
import LayoutPanel from '@/pages/layout/LayoutPanel.vue';
import FieldInspector from '@/components/forms/builder/FieldInspector.vue';
import PageInspector from '@/components/forms/builder/PageInspector.vue';
import ActionInspector from '@/components/forms/builder/ActionInspector.vue';
import { Button, Icon } from '@ui';
import { useSortable } from '@/composables/forms/use-drag-and-drop';
import { computed, nextTick, onMounted, onUnmounted, ref, useTemplateRef, watch } from 'vue';
Expand Down Expand Up @@ -110,10 +112,14 @@ const hasPageRules = (page) => (page.rules ?? []).some((rule) => {
return (rule.conditions ?? []).some((condition) => condition.field && condition.value !== null && condition.value !== '');
});

const hasPageIndicators = (page, pageIndex) => isConnectorDestination(pageIndex) || hasPageRules(page);
const selectField = (field) => field.type === 'import' || emit('select', { type: SelectionType.Field, id: field._id });
const selectPage = (page) => emit('select', { type: SelectionType.Page, id: page._id });
const isFieldSelected = (field) => props.selected?.type === SelectionType.Field && props.selected.id === field._id;
const selectAction = (page) => emit('select', { type: SelectionType.Action, id: page._id });
const isActionSelected = (page) => props.selected?.type === SelectionType.Action && props.selected.id === page._id;
const actionLabel = (page) => page.button_label?.length ? __(page.button_label) : __('Next Page');
// Only pages with a page after them get buttons; the last page's Submit button has no logic.
const hasActions = (pageIndex) => pageIndex < props.pages.length - 1;
const isPageSelected = (page) => props.selected?.type === SelectionType.Page && props.selected.id === page._id;

const selectedFieldEndConnectionPageIndex = computed(() => {
Expand Down Expand Up @@ -189,7 +195,14 @@ const onEscape = (event: KeyboardEvent) => {
}

if (props.selected) {
const { type, id } = props.selected;
emit('select', null);

// Closing the page buttons' inspector puts focus back on the button that opened it.
if (type === SelectionType.Action) {
nextTick(() => tree.value?.querySelector<HTMLElement>(`[data-page-actions="${id}"]`)?.focus());
}

return;
}

Expand Down Expand Up @@ -230,25 +243,18 @@ onUnmounted(() => {
>
<div
class="flex w-full min-w-0 flex-nowrap items-center justify-center gap-1.5"
:class="{ '-ms-1.5': hasPageIndicators(page, pageIndex) }"
:class="{ '-ms-1.5': isConnectorDestination(pageIndex) }"
>
<Icon
v-if="isConnectorDestination(pageIndex)"
name="chevron-right"
class="logic-arrow size-3! shrink-0 -ms-2.5 relative -top-0.25 text-blue-400 dark:text-blue-500"
aria-hidden="true"
/>
<span v-if="hasPageRules(page)" v-tooltip="__('Logic attached')" class="inline-flex shrink-0">
<Icon
name="logic-tree"
class="size-3.5! text-gray-400 dark:text-gray-500"
aria-hidden="true"
/>
</span>
<div
class="mx-auto flex w-full shrink-0 justify-center items-center gap-2 rounded-xl border border-dashed border-gray-300 px-3.5 py-2 text-xs font-medium text-gray-850 bg-white dark:border-gray-700 dark:bg-gray-900 dark:text-gray-200"
:class="{
'w-[85%]!': hasPageIndicators(page, pageIndex),
'w-[85%]!': isConnectorDestination(pageIndex),
'ring-1 ring-blue-500 border-transparent': isPageSelected(page),
}"
>
Expand Down Expand Up @@ -301,6 +307,31 @@ onUnmounted(() => {
</ul>
</div>
</div>

<!-- The page buttons. They open the page's logic, as they do in the builder. -->
<div v-if="hasActions(pageIndex)" :id="`actions-${page._id}`" class="flex justify-center px-2">
<div class="relative">
<!-- Sits outside the button so the button stays centred under the column.
The tooltip goes on a span, not the SVG: Chrome puts an SVG with a focus listener in the Tab order. -->
<span
v-if="hasPageRules(page)"
v-tooltip="__('Logic attached')"
class="absolute end-full top-1/2 me-2 inline-flex -translate-y-1/2"
>
<Icon name="logic-tree" class="size-3.5! text-gray-400 dark:text-gray-500" aria-hidden="true" />
</span>
<Button
size="sm"
variant="primary"
:data-page-actions="page._id"
class="border-0! dark:border-0! shadow-none!"
:class="{ 'outline-2! outline-offset-2! outline-blue-500!': isActionSelected(page) }"
style="--theme-color-primary: var(--theme-color-gray-950)"
:text="actionLabel(page)"
@click="selectAction(page)"
/>
</div>
</div>
</div>
</div>
</div>
Expand All @@ -317,6 +348,7 @@ onUnmounted(() => {

<LayoutPanel v-if="!isRightPanelCollapsed" side="right" :mobile-open="isInspectorOpen">
<PageInspector v-if="selected?.type === SelectionType.Page" />
<ActionInspector v-else-if="selected?.type === SelectionType.Action" default-tab="logic" />
<FieldInspector v-else-if="selected?.type === SelectionType.Field" />
</LayoutPanel>

Expand Down
2 changes: 1 addition & 1 deletion resources/js/pages/forms/Logic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const flattenedFields = computed(() => pages.value.flatMap((page, pageIndex) =>
),
));

const selectedPage = computed(() => selected.value?.type === SelectionType.Page ? pages.value.find((page) => page._id === selected.value.id) ?? null : null);
const selectedPage = computed(() => [SelectionType.Page, SelectionType.Action].includes(selected.value?.type) ? pages.value.find((page) => page._id === selected.value.id) ?? null : null);
const selectedField = computed(() => selected.value?.type === SelectionType.Field ? findField(selected.value.id) : null);

const { showFieldNumbers } = useFieldNumberingPreference();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ test('fields from linked fieldsets are suggested for page rules with their prefi

expect(wrapper.vm.suggestableConditionFields.map((f) => f.handle)).toEqual(['name', 'work_company']);
});

test('opens on the default tab, falling back to Settings on the last page', () => {
const first = { _id: 'first', rules: [], sections: [] };
const last = { _id: 'last', rules: [], sections: [] };
const mountFor = (page) => {
Object.assign(context, { dirty: vi.fn(), fieldtypes: [], inspecting: ref(page), pages: ref([first, last]) });

return mount(ActionInspector, { shallow: true, props: { defaultTab: 'logic' } });
};

expect(mountFor(first).vm.activeTab).toBe('logic');
expect(mountFor(last).vm.activeTab).toBe('settings');
});
Loading