Skip to content

Commit

Permalink
Merge pull request #61 from araise-dev/feature/124-filter-datepicker
Browse files Browse the repository at this point in the history
Add datepicker to filters with `datetime` & `date` input fields (Meta #124)
  • Loading branch information
tuxes3 authored Jul 24, 2024
2 parents a413789 + 5a3eb42 commit 3cc6982
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/Filter/Type/DateFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,36 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;

class DateFilterType extends DatetimeFilterType
{
protected $locale;

public function __construct(
?string $column = null,
array $joins = [],
protected ?RequestStack $requestStack = null
) {
parent::__construct($column, $joins);
$this->locale = $requestStack->getMainRequest()?->getLocale() ?? 'en';
}

public function getValueField(?string $value = null, ?string $operator = null): string
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

$stimulusAttrs = (new StimulusHelper(null))->createStimulusAttributes();
$stimulusAttrs
->addController('araise/core-bundle/datetime', [
'lang' => $this->locale,
])
;
return sprintf(
'<input type="date" name="{name}" value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
'<input type="date" name="{name}" value="%s" %s>',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
$stimulusAttrs
);
}

Expand Down
23 changes: 20 additions & 3 deletions src/Filter/Type/DatetimeFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace araise\TableBundle\Filter\Type;

use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;

class DatetimeFilterType extends FilterType
{
Expand All @@ -20,6 +22,14 @@ class DatetimeFilterType extends FilterType

public const CRITERIA_IS_EMPTY = 'is_empty';

public function __construct(
?string $column = null,
array $joins = [],
protected ?RequestStack $requestStack = null
) {
parent::__construct($column, $joins);
}

public function getOperators(): array
{
return [
Expand All @@ -38,10 +48,17 @@ public function getValueField(?string $value = null, ?string $operator = null):
{
$date = \DateTime::createFromFormat(static::getQueryDataFormat(), (string) $value) ?: new \DateTime();
$value = $date->format(static::getDateFormat());

$locale = $this->requestStack->getMainRequest()?->getLocale();
$stimulusAttrs = (new StimulusHelper(null))->createStimulusAttributes();
$stimulusAttrs
->addController('araise/core-bundle/datetime', [
'lang' => $locale ?? 'en',
])
;
return sprintf(
'<input type="datetime-local" name="{name}" value="%s">',
$operator !== static::CRITERIA_IS_EMPTY ? $value : ''
'<input type="datetime-local" name="{name}" value="%s" %s>',
$operator !== static::CRITERIA_IS_EMPTY ? $value : '',
$stimulusAttrs
);
}

Expand Down

0 comments on commit 3cc6982

Please sign in to comment.