Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datepicker to filters with datetime & date input fields (Meta #124) #61

Merged
merged 4 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading