-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pikaday demo with ProvidePlugin configuration
- Loading branch information
Showing
8 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import "pikaday/css/pikaday.css" | ||
import Pikaday from "pikaday" | ||
|
||
export default class Datepicker { | ||
constructor(element, options) { | ||
if (options == null) { options = {} } | ||
|
||
// Store DOM element for reference | ||
this.element = element | ||
|
||
// Do not re-run on elements that already have datepickers | ||
if (this.element.datepicker === undefined) { | ||
options = Object.assign({}, | ||
this.defaultOptions(), | ||
options | ||
) | ||
|
||
const picker = new Pikaday(options) | ||
|
||
// Store picker on element for reference | ||
this.element.datepicker = picker | ||
|
||
return picker | ||
} else { | ||
console.log("Datepicker already attached") | ||
return | ||
} | ||
} | ||
|
||
// Overridden by `options` in constructor | ||
defaultOptions() { | ||
return { | ||
field: this.element, | ||
format: "M/D/YYYY", | ||
bound: true, | ||
keyboardInput: false, | ||
showDaysInNextAndPreviousMonths: true, | ||
position: 'bottom left' | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Normally, I would use the following import: | ||
// | ||
// import Datepicker from 'datepicker' | ||
// | ||
// but this app also demonstrates how to use the | ||
// ProvidePlugin to make Datepicker available | ||
// without imports | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
document.querySelectorAll('input[data-datepicker]').forEach((el) => { | ||
new Datepicker(el) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import "./datepicker" | ||
import "./form" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
<h1>Welcome#index</h1> | ||
<p>Find me in app/views/welcome/index.html.erb</p> | ||
<h1>Pikaday demo</h1> | ||
<div> | ||
<form action=""><input type="text" data-datepicker></form> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
const { environment } = require('@rails/webpacker') | ||
const webpack = require('webpack') | ||
const {resolve} = require('path'); | ||
|
||
environment.plugins.append('Provide', new webpack.ProvidePlugin({ | ||
Datepicker: [resolve('app/javascript/application/datepicker'), 'default'] | ||
})) | ||
|
||
module.exports = environment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters