icanjs / semantic-ui-daterangepicker-canjs

Initialize a semantic-ui-daterangepicker on an element using an HTML attribute
MIT License
6 stars 2 forks source link

semantic-ui-daterangepicker-canjs

CanJS wrapper around SemanticUI Date Range Picker component

A can.view.attr wrapper around the SemanticUI Date Range Picker. With can.view.attr you can add custom behavior to elements that contain a specified html attribute. Since SemanticUI is a set of jQuery plugins, can.view.attr is a natural way to invoke them in a CanJS or DoneJS application.

This wrapper allows you to use SemanticUI Date Range Picker with just html, no need to run jQuery plugin yourself.

SemanticUI's CSS should be imported separately. You can use semantic-ui-less package as in this demo.

Demo

See the included demo /demo/demo.html (run npm install, then http-server in the project root and browse /demo/demo.html).

Date Range Picker

Installation

npm install semantic-ui-daterangepicker-canjs --save

Usage

<input type="text" semantic-daterangepicker />

<!-- As a single-calendar date-picker -->
<input type="text" semantic-daterangepicker single="true"/>

<!-- Specify start and end dates -->
<input type="text" semantic-daterangepicker
  start-date="6/23/2016"
  end-date="9/23/2016"/>

<!-- Specify date string format -->
<input type="text" semantic-daterangepicker
  start-date="Jun 23 2016"
  end-date="Sep 23 2016"
  format="MMM D Y"/>

Use as a single date picker with live binding

Use a template like this:

<input type="text" semantic-daterangepicker
  single="true"
  {($value)}="formattedDate"
  format="MMM D Y"/>

And this example viewModel:

import Map from 'can/map/';
import 'can/map/define/';
import moment from 'moment';

let DateMap = Map.extend({
  define: {
    date: {
      value: new Date()
    },

    formattedDate: {
      get(){
        let date = this.attr('date');
        return moment(date).format('MMM D Y');
      },
      set(val){
        let date = moment(val);
        // If an invalid date is set, set it to today's date.
        if (date.toString() === 'Invalid date') {
          return this.attr('formattedDate', moment());
        }
        this.attr('date', date);
        return val;
      }
    }
  }
});

let dateMap = new DateMap();

$('#demo').viewModel(dateMap).attr(dateMap);

API

Attributes:

Contributing

Pull requests are welcome.

Authors