1
0
Fork 0
mirror of synced 2024-06-29 03:20:34 +12:00

Fix validation and focus styling on datepickers

This commit is contained in:
Andrew Kingston 2021-01-29 10:18:41 +00:00
parent 2db5e62e35
commit af4f902e10
3 changed files with 38 additions and 10 deletions

View file

@ -1263,6 +1263,12 @@
"type": "text", "type": "text",
"label": "Placeholder", "label": "Placeholder",
"key": "placeholder" "key": "placeholder"
},
{
"type": "boolean",
"label": "Show Time",
"key": "enableTime",
"defaultValue": true
} }
] ]
} }

View file

@ -7,28 +7,48 @@
export let field export let field
export let label export let label
export let placeholder export let placeholder
export let enableTime
let fieldState let fieldState
let fieldApi let fieldApi
let value let open = false
$: fieldApi?.setValue(value) let flatpickr
$: flatpickrOptions = { $: flatpickrOptions = {
element: `#${$fieldState?.id}-wrapper`, element: `#${$fieldState?.id}-wrapper`,
enableTime: true, enableTime: enableTime || false,
altInput: true, altInput: true,
altFormat: "F j Y, H:i", altFormat: enableTime ? "F j Y, H:i" : "F j, Y",
} }
const handleChange = event => { const handleChange = event => {
const [fullDate] = event.detail const [dates] = event.detail
value = fullDate fieldApi.setValue(dates[0])
}
const onOpen = () => {
open = true
}
const onClose = () => {
open = false
// Manually blur all input fields since flatpickr creates a second
// duplicate input field.
// We need to blur both because the focus styling does not get properly
// applied.
const els = document.querySelectorAll(`#${$fieldState.id}-wrapper input`)
els.forEach(el => el.blur())
} }
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi> <SpectrumField {label} {field} bind:fieldState bind:fieldApi>
{#if fieldState} {#if fieldState}
<Flatpickr <Flatpickr
{value} bind:flatpickr
value={$fieldState.value}
on:open={onOpen}
on:close={onClose}
options={flatpickrOptions} options={flatpickrOptions}
on:change={handleChange} on:change={handleChange}
element={`#${$fieldState.id}-wrapper`}> element={`#${$fieldState.id}-wrapper`}>
@ -37,6 +57,7 @@
aria-disabled="false" aria-disabled="false"
aria-invalid="false" aria-invalid="false"
class="flatpickr spectrum-InputGroup spectrum-Datepicker" class="flatpickr spectrum-InputGroup spectrum-Datepicker"
class:is-focused={open}
aria-readonly="false" aria-readonly="false"
aria-required="false" aria-required="false"
aria-haspopup="true"> aria-haspopup="true">
@ -53,7 +74,8 @@
<button <button
type="button" type="button"
class="spectrum-Picker spectrum-InputGroup-button" class="spectrum-Picker spectrum-InputGroup-button"
tabindex="-1"> tabindex="-1"
on:click={flatpickr?.open}>
<svg <svg
class="spectrum-Icon spectrum-Icon--sizeM" class="spectrum-Icon spectrum-Icon--sizeM"
focusable="false" focusable="false"

View file

@ -92,10 +92,10 @@ const inclusionConstraint = (options = []) => value => {
const dateConstraint = (dateString, isEarliest) => { const dateConstraint = (dateString, isEarliest) => {
const dateLimit = Date.parse(dateString) const dateLimit = Date.parse(dateString)
return value => { return value => {
if (value == null || value === "" || !value.length) { if (value == null || value === "") {
return null return null
} }
const dateValue = Date.parse(value[0]) const dateValue = Date.parse(value)
const valid = isEarliest ? dateValue >= dateLimit : dateValue <= dateLimit const valid = isEarliest ? dateValue >= dateLimit : dateValue <= dateLimit
const adjective = isEarliest ? "Earliest" : "Latest" const adjective = isEarliest ? "Earliest" : "Latest"
const limitString = flatpickr.formatDate(new Date(dateLimit), "F j Y, H:i") const limitString = flatpickr.formatDate(new Date(dateLimit), "F j Y, H:i")