import { Prop, toNative } from 'vue-facing-decorator'; import PowerduckState from '../../app/powerduck-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { isNullOrEmpty } from '../../common/utils/is-null-or-empty'; import Button from '../button/button'; import { ButtonLayout } from '../button/button-layout'; import FlexContainer from '../form/flex-container'; import FlexFormItem from '../form/form-item-flex'; import Modal, { ModalSize } from '../modal/modal'; import ModalBody from '../modal/modal-body'; import ModalFooter from '../modal/modal-footer'; import './css/mobile-optimized-hero-filter.css'; interface MobileOptimizedHeroFilterArgs { caption: string; isLoading?: boolean; cssClass?: string; placeholder?: string; useIntersectionObserver?: boolean; clearFilterText?: string; submitText?: string; clickedClear?: () => void; /** * The modal footer's submit. Returning `false` keeps the modal open, e.g. while a required field * is empty and its message must stay in view; any other result hides it. */ clickedSubmit?: () => void | boolean; modalBackdropCssClass?: string; mobileSearchIcon?: string; mobileModalCssClass?: string; /** Extra class on the sheet's `.modal-content`; unlike `mobileModalCssClass` it may change while the sheet is open. */ mobileModalContentCssClass?: string; mobileShowClearFilters?: boolean; mobileModalSize?: ModalSize; mobileFirstLineText: string; mobileSecondLineText: string; } @Component class MobileOptimizedHeroFilterComponent extends TsxComponent implements MobileOptimizedHeroFilterArgs { @Prop() caption: string; @Prop() isLoading: boolean; @Prop() cssClass?: string; @Prop() placeholder?: string; @Prop() useIntersectionObserver?: boolean; @Prop() clearFilterText?: string; @Prop() clickedClear?: () => void; @Prop() clickedSubmit?: () => void | boolean; @Prop() modalBackdropCssClass: string; @Prop() mobileSearchIcon: string; @Prop() mobileModalCssClass?: string; @Prop() mobileModalContentCssClass?: string; @Prop() mobileShowClearFilters?: boolean; @Prop() mobileFirstLineText: string; @Prop() mobileSecondLineText: string; @Prop() mobileModalSize?: ModalSize; mounted() { if (this.useIntersectionObserver) { this.$nextTick(() => { const observer = new IntersectionObserver(([e]) => { e.target.toggleAttribute('data-stuck', e.intersectionRatio < 1); }, { threshold: [1] }); observer.observe(this.$el); (this as any)._observer = observer; }); } } beforeUnmount(): void { if ((this as any)._observer != null) { ((this as any)._observer as IntersectionObserver).disconnect(); (this as any)._observer = null; } } showFilterModal() { this.getModal().show(); } hideFilterModal() { this.getModal().hide(); } private onSearchEntryKeydown(e: KeyboardEvent): void { if (e.key !== 'Enter' && e.key !== ' ') { return; } e.preventDefault(); this.showFilterModal(); } /** The footer submit: hides the modal unless `clickedSubmit` returns `false` (see the prop). */ private handleSubmitClicked(): void { if (this.clickedSubmit() !== false) { this.hideFilterModal(); } } private getModal(): typeof Modal.prototype { return this.$refs.mobileModal as typeof Modal.prototype; } render(h) { let firstLine = this.mobileFirstLineText; let isPlaceholder = false; if (isNullOrEmpty(firstLine)) { firstLine = this.placeholder ?? `${PowerduckState.getResourceValue('search')}...`; isPlaceholder = true; } return (
{/* The search entry opens the filter dialog: role, tabindex and Enter / Space make the div a keyboard-operable button without changing how it looks or reacts to a tap. */}
{ this.showFilterModal(); }} onKeydown={(e: KeyboardEvent) => this.onSearchEntryKeydown(e)} >
{firstLine}
{this.mobileSecondLineText?.length > 0 &&
{this.mobileSecondLineText}
}
{this.$slots.default?.()}
{this.$slots.default?.()}
{this.mobileShowClearFilters && (
); } } const MobileOptimizedHeroFilter = toNative(MobileOptimizedHeroFilterComponent); export default MobileOptimizedHeroFilter;