Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cgsmith committed Dec 6, 2022
0 parents commit 4c4471f
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
build
#composer.lock
docs
vendor

# cache directories
Thumbs.db
*.DS_Store
*.empty

#phpstorm project files
.idea

#netbeans project files
nbproject

#eclipse, zend studio, aptana or other eclipse like project files
.buildpath
.project
.settings


# mac deployment helpers
switch
index
28 changes: 28 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Modified BSD License
====================

_Copyright © 2022, CGSmith, LLC_
_All rights reserved._

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the CGSmith, LLC nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL CGSMITH, LLC BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
Flatpickr JS Widget for Yii2
====================================

[![Latest Version](https://img.shields.io/github/tag/cgsmith/yii2-flatpickr-widget.svg?style=flat-square&label=release)](https://github.com/2amigos/yii2-date-picker-widget/tags)
[![Software License](https://img.shields.io/badge/license-BSD-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads](https://img.shields.io/packagist/dt/cgsmith/yii2-flatpickr-widget.svg?style=flat-square)](https://packagist.org/packages/cgsmith/yii2-flatpickr-widget)


Renders a [flatpickr Datepicker plugin](https://flatpickr.js.org/).

Installation
------------
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```bash
$ composer require cgsmith/yii2-flatpickr-widget:~1.0
```
or add

```json
"cgsmith/yii2-flatpickr-widget": "~1.0"
```

to the require section of your application's `composer.json` file.

Usage
-----
The widget renders the flatpickr onto your form.

***Example of use with a form***
Use the widget by setting up its `model` and `attribute`.

```php
<?php
use cgsmith\flatpickr\FlatpickrWidget;

// as a widget
?>

<?= FlatpickrWidget::widget([
'model' => $model,
'attribute' => 'date',
]);?>


<?php
// additional config options for flatpickr

echo $form->field($model, 'date')->widget(
FlatpickrWidget::widget([
'model' => $model,
'attribute' => 'date',
'flatpickrConfig' => [ // This is passed as a JSON object to flatpickr
'enableTime' => false,
'dateFormat' => 'F j, Y H:i',
'altInput' => true,
'altFormat' => 'F j, Y',
]
]);
?>
```

Resources Information
-------------------
Please, check the [flatpicker site](https://flatpickr.js.org/options/) documentation for further information about its configuration options.

Contributing
------------

Contributions are welcome!

Credits
-------

- [Chris Smith](https://github.com/cgsmith)
- [All Contributors](../../contributors)

License
-------

The BSD License (BSD). Please see [License File](LICENSE.md) for more information.
42 changes: 42 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "cgsmith/yii2-flatpickr-widget",
"description": "flatpickr (DatePicker) widget for Yii2.",
"type": "yii2-extension",
"keywords": [
"cgsmith",
"yii2",
"yii 2",
"extension",
"widget",
"datepicker",
"flatpickr"
],
"homepage": "https://github.com/cgsmith/yii2-flatpickr-widget",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Chris Smith",
"email": "[email protected]",
"homepage": "https://cgsmith.net",
"role": "Developer"
}
],
"support": {
"issues": "https://github.com/cgsmith/yii2-flatpickr-widget/issues",
"source": "https://github.com/2amigos/yii2-flatpickr-widget"
},
"require": {
},
"require-dev": {
},
"autoload": {
"psr-4": {
"cgsmith\\flatpickr\\": "src"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
14 changes: 14 additions & 0 deletions src/FlatpickrAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace cgsmith\flatpickr;

class FlatpickrAsset extends \yii\web\AssetBundle
{
public $js = [
'//cdn.jsdelivr.net/npm/flatpickr',
];

public $css = [
'//cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css',
];
}
61 changes: 61 additions & 0 deletions src/FlatpickrWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace cgsmith\flatpickr;

use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;

/**
* FlatpickrWidget renders a Input Widget.
*
* ```php
* <?= $form->field($model, 'next_payout_date')->widget(
* FlatpickrWidget::class, [
* 'model' => $model,
* 'attribute' => 'next_payout_date',
* // you can set flatpickrConfig array to match flatpickr JS options
* ]
* );
* ?>
* ```
*
* For more details and usage information on InputWidget, see the [guide article on forms](guide:input-forms).
*
* @author Chris Smith <[email protected]>
*/
class FlatpickrWidget extends InputWidget
{
/**
* @link https://flatpickr.js.org/options/
* @var array
*/
public array $flatpickrConfig = [
'enableTime' => false,
'dateFormat' => 'U',
'altInput' => true,
'altFormat' => 'F j, Y',
'minDate' => 'today',
];

public function run()
{
$this->options['class'] = trim($this->options['class'] . ' ' . 'date-' . $this->attribute);

$input = $this->hasModel()
? Html::activeTextInput($this->model, $this->attribute, $this->options)
: Html::textInput($this->name, $this->value, $this->options);

echo $input;

$flatpickrConfig = Json::encode($this->flatpickrConfig);

FlatpickrAsset::register($this->getView());
$view = $this->getView();
$view->registerJs('
// cgsmith-flatpickr-widget
$(\'.date-' . $this->attribute.'\').flatpickr('
. $flatpickrConfig .
')');
}
}

0 comments on commit 4c4471f

Please sign in to comment.