Improve grid integration with new datepicker

This commit is contained in:
Andrew Kingston 2023-11-03 20:27:30 +00:00
parent 78af50bafe
commit b2352157cd
4 changed files with 30 additions and 13 deletions

View File

@ -39,11 +39,13 @@ export default function positionDropdown(element, opts) {
styles = customUpdate(anchorBounds, elementBounds, styles) styles = customUpdate(anchorBounds, elementBounds, styles)
} else { } else {
// Determine vertical styles // Determine vertical styles
const topSpace = anchorBounds.top
const bottomSpace = window.innerHeight - anchorBounds.bottom
if (align === "right-outside") { if (align === "right-outside") {
styles.top = anchorBounds.top styles.top = anchorBounds.top
} else if ( } else if (
window.innerHeight - anchorBounds.bottom < window.innerHeight - anchorBounds.bottom < (maxHeight || 100) &&
(maxHeight || 100) topSpace - bottomSpace > 100
) { ) {
styles.top = anchorBounds.top - elementBounds.height - offset styles.top = anchorBounds.top - elementBounds.height - offset
styles.maxHeight = maxHeight || 240 styles.maxHeight = maxHeight || 240

View File

@ -1,10 +1,15 @@
<script> <script>
import FlatpickrDatePicker from "./FlatpickrDatePicker.svelte" import FlatpickrDatePicker from "./FlatpickrDatePicker.svelte"
import SpectrumDatePicker from "./SpectrumDatePicker.svelte" import SpectrumDatePicker from "./SpectrumDatePicker.svelte"
export let api
</script> </script>
<svelte:component <svelte:component
this={$$props.range ? FlatpickrDatePicker : SpectrumDatePicker} this={$$props.range ? FlatpickrDatePicker : SpectrumDatePicker}
{...$$props} {...$$props}
on:change on:change
on:open
on:close
bind:api
/> />

View File

@ -4,7 +4,7 @@
import "@spectrum-css/textfield/dist/index-vars.css" import "@spectrum-css/textfield/dist/index-vars.css"
import Popover from "../../../Popover/Popover.svelte" import Popover from "../../../Popover/Popover.svelte"
import dayjs from "dayjs" import dayjs from "dayjs"
import { createEventDispatcher } from "svelte" import { createEventDispatcher, onMount } from "svelte"
import TimePicker from "./TimePicker.svelte" import TimePicker from "./TimePicker.svelte"
import Calendar from "./Calendar.svelte" import Calendar from "./Calendar.svelte"
import DateTimeInput from "./DateInput.svelte" import DateTimeInput from "./DateInput.svelte"
@ -19,6 +19,9 @@
export let timeOnly = false export let timeOnly = false
export let ignoreTimezones = false export let ignoreTimezones = false
export let useKeyboardShortcuts = true export let useKeyboardShortcuts = true
export let appendTo = null
export let api = null
export let align = "left"
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
@ -114,6 +117,13 @@
calendar?.setDate(now) calendar?.setDate(now)
handleChange(now) handleChange(now)
} }
onMount(() => {
api = {
open: () => popover?.show(),
close: () => popover?.hide(),
}
})
</script> </script>
<DateTimeInput <DateTimeInput
@ -132,9 +142,14 @@
<Popover <Popover
bind:this={popover} bind:this={popover}
on:open
on:close
on:open={onOpen} on:open={onOpen}
on:close={onClose} on:close={onClose}
{anchor} {anchor}
portalTarget={appendTo}
maxHeight={374}
maxWidth={296}
align="left" align="left"
> >
{#if isOpen} {#if isOpen}

View File

@ -10,7 +10,7 @@
export let readonly = false export let readonly = false
export let api export let api
let flatpickr let datePickerAPI
let isOpen let isOpen
// Adding the 0- will turn a string like 00:00:00 into a valid ISO // Adding the 0- will turn a string like 00:00:00 into a valid ISO
@ -41,7 +41,7 @@
// Ensure we close flatpickr when unselected // Ensure we close flatpickr when unselected
$: { $: {
if (!focused) { if (!focused) {
flatpickr?.close() datePickerAPI?.close()
} }
} }
@ -52,8 +52,8 @@
onMount(() => { onMount(() => {
api = { api = {
onKeyDown, onKeyDown,
focus: () => flatpickr?.open(), focus: () => datePickerAPI?.open(),
blur: () => flatpickr?.close(), blur: () => datePickerAPI?.close(),
isActive: () => isOpen, isActive: () => isOpen,
} }
}) })
@ -75,12 +75,10 @@
<CoreDatePicker <CoreDatePicker
{value} {value}
on:change={e => onChange(e.detail)} on:change={e => onChange(e.detail)}
appendTo={document.documentElement}
enableTime={!dateOnly} enableTime={!dateOnly}
{timeOnly} {timeOnly}
time24hr
ignoreTimezones={schema.ignoreTimezones} ignoreTimezones={schema.ignoreTimezones}
bind:flatpickr bind:api={datePickerAPI}
on:open={() => (isOpen = true)} on:open={() => (isOpen = true)}
on:close={() => (isOpen = false)} on:close={() => (isOpen = false)}
useKeyboardShortcuts={false} useKeyboardShortcuts={false}
@ -110,9 +108,6 @@
position: absolute; position: absolute;
opacity: 0; opacity: 0;
} }
.picker :global(.flatpickr) {
min-width: 0;
}
.picker :global(.spectrum-Textfield-input) { .picker :global(.spectrum-Textfield-input) {
width: 100%; width: 100%;
} }