All Projects → jacobmischka → svelte-flatpickr

jacobmischka / svelte-flatpickr

Licence: MIT license
Flatpickr component for Svelte.

Programming Languages

Svelte
593 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to svelte-flatpickr

svelte-undoable
Memento design pattern in Svelte
Stars: ✭ 39 (-62.14%)
Mutual labels:  svelte
bob-ross-art-gallery
🖼 A visual, virtual tour of The Joy of Painting, by Bob Ross.
Stars: ✭ 27 (-73.79%)
Mutual labels:  svelte
jsstore-examples
This repo contains examples of jsstore for different frameworks & tools
Stars: ✭ 31 (-69.9%)
Mutual labels:  svelte
svelte-hash-router
tienpv222.github.io/svelte-hash-router
Stars: ✭ 37 (-64.08%)
Mutual labels:  svelte
nodekit
[Moved to Codeberg] A Small Web server.
Stars: ✭ 68 (-33.98%)
Mutual labels:  svelte
sveltekit-electron
Minimal Sveltekit + Electron starter template.
Stars: ✭ 146 (+41.75%)
Mutual labels:  svelte
svelte-material
Modular and customizable Material Design UI components for Svelte.js
Stars: ✭ 30 (-70.87%)
Mutual labels:  svelte
svelte-eslint-parser
Svelte parser for ESLint
Stars: ✭ 30 (-70.87%)
Mutual labels:  svelte
Installer
A simple standalone program which automates the installation, removal and maintenance of BetterDiscord.
Stars: ✭ 1,391 (+1250.49%)
Mutual labels:  svelte
svelte-accessible-dialog
An accessible dialog component for Svelte apps
Stars: ✭ 24 (-76.7%)
Mutual labels:  svelte
generator-jhipster-svelte
Generate Svelte powered JHipster web applications
Stars: ✭ 44 (-57.28%)
Mutual labels:  svelte
cakcuk
A Command Bot Interface builder, CLI-based to easily create your CLI commands for your Workspace
Stars: ✭ 13 (-87.38%)
Mutual labels:  svelte
website
🏡 My personal website! Now built with Svelte...
Stars: ✭ 18 (-82.52%)
Mutual labels:  svelte
svelte-persistent-store
Persist your svelte store in localStorage or sessionStorage
Stars: ✭ 88 (-14.56%)
Mutual labels:  svelte
webstone
Start your next full-stack application with Webstone and configure it as you go.
Stars: ✭ 71 (-31.07%)
Mutual labels:  svelte
emojimix
🤖 emojimix implemented in Svelte 🤖
Stars: ✭ 24 (-76.7%)
Mutual labels:  svelte
svelte-parallax
a (small) spring-based parallax component library for Svelte
Stars: ✭ 87 (-15.53%)
Mutual labels:  svelte
strapi-starter-minimal-sapper-blog
A minimal Sapper Blog built with Strapi
Stars: ✭ 44 (-57.28%)
Mutual labels:  svelte
svelte-micro
Light & reactive one-component router for Svelte
Stars: ✭ 81 (-21.36%)
Mutual labels:  svelte
sirix-svelte-frontend
A GUI console for SirixDB, using Svelte/Sapper.
Stars: ✭ 23 (-77.67%)
Mutual labels:  svelte

svelte-flatpickr

Svelte component for flatpickr datetime picker.

Usage

Ideally, if you're using svelte already, configure your bundler to resolve the package's svelte field (or import from svelte-flatpickr/src/Flatpickr.svelte) and compile the template from within your own project. See sveltejs/svelte#604 for more information.

Don't forget to import flatpickr's stylesheets as well (flatpickr/dist/flatpickr.css, and optionally any theme stylesheets you want).

Versions

  • For Svelte v3 use v3.x.x
  • For Svelte v2.x use v1.x.x
  • For Svelte v1.x use v0.x.x

Flatpickr documentation

Example

See the test directory for a full working example.

<main>
    <form on:submit={handleSubmit}>
        <Flatpickr {options} bind:value bind:formattedValue on:change={handleChange} name="date" />

        <button type="submit">
            Submit
        </button>
    </form>
</main>

<script>
    import Flatpickr from 'svelte-flatpickr';
    import 'flatpickr/dist/flatpickr.css';

    let value, formattedValue;

    const options = {
        enableTime: true,
        onChange(selectedDates, dateStr) {
            console.log('flatpickr hook', selectedDates, dateStr);
        }
    };

    $: console.log({ value, formattedValue });

    function handleChange(event) {
        const [ selectedDates, dateStr ] = event.detail;
        console.log({ selectedDates, dateStr });
    }

    function handleSubmit(event) {
        event.preventDefault();

        console.log(event.target.elements['date'].value);
    }
</script>

The selected date(s) can be obtained using hooks or binding to value.

The format of the date expected can be controlled with the prop dateFormat, which will take a date format acceptable to Flatpickr.

The prop formattedValue can also be bound to, which contains the selected date(s)'s formatted string.

The props input and flatpickr (or fp) can also be bound to, which represent the underlying input element (unless using a custom external element as described below) and the flatpickr instance, respectively. Assigning to these will break the Flatpickr component, please don't.

Hooks

Hooks can be specified normally in the options object, or by listening to the svelte event.

When binding svelte handler, event.details will be [ selectedDates, dateStr, instance ] (see flatpickr events docs).

External Elements

As per the flatpickr documentation, it is also possible to wrap a custom element rather than have the component create the input for you. This allows for decoration of the control such as adding a clear button or similar.

You can add the custom element by wrapping it in the Flatpickr component, as it is the default slot. However, it is necessary to pass the selector for the custom element, as the element attribute to Flatpickr's options.

Specifying the selector for a custom element automatically adds the {wrap: true} option to flatpickr.

<Flatpickr
    options="{ flatpickrOptions }"
    bind:value="{date}"
    element="#my-picker"
>
    <div class="flatpickr" id="my-picker">
        <input type="text" placeholder="Select Date.." data-input />

        <a class="input-button" title="clear" data-clear>
            <i class="icon-close"></i>
        </a>
    </div>
</Flatpickr>

<script>
    import Flatpickr from 'svelte-flatpickr';

    import 'flatpickr/dist/flatpickr.css';
    import 'flatpickr/dist/themes/light.css';

    let date = null;
    const flatpickrOptions = {
        element: '#my-picker',
    };
</script>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].