framework icon indicating copy to clipboard operation
framework copied to clipboard

Date field isn't implemented.

Open toscani opened this issue 5 years ago • 7 comments

All the fields are updated in Field\Factory.php except the date field.

 /**
     * Return a DateField instance.
     *
     * @param string $name       The name attribute of the date input.
     * @param array  $features   Custom field features - title, info.
     * @param array  $attributes Input html attributes.
     *
     * @return \Themosis\Field\Fields\DateField
     */
    public function date($name, array $features = [], array $attributes = [])
    {
        $properties = [
            'features' => $features,
            'atts' => array_merge(['class' => 'newtag'], $attributes, ['data-field' => 'date']),
            'name' => $name,
        ];

        return $this->make('Themosis\\Field\\Fields\\DateField', $properties);
    }

The make method does not exist anymore and throws an exception. I've tried adding a class to a text field and implement my own datepicker, but since that's a jquery datepicker it doesn't work very well.

I really need a date field for calendar items, any suggestions?

thanks!

toscani avatar Aug 28 '19 12:08 toscani

Yeah this one is tricky and hasn't been yet in core. The best way is to create a custom field, basically you can create a new class called DateFieldType for example and extends the TextType field as your javascript will attach a picker to the input only.

Then from the class, you can setup a call to your custom js asset from the constructor for example, so everytime the field is requested, the asset is enqueued.

Here is an example of such class (not tested):

<?php

namespace App\Fields;

use Themosis\Forms\Types\TextType;

class DateFieldType extends TextType
{
    public function __construct(string $name)
    {
         parent::__construct($name);
         Asset::add('datepicker-script', 'js/datepicker.js', ['jquery'], '1.0.0', true)->to(['front', 'admin']);
    }
}

Then you can use that class when defining a form like so:

// Inside a form "build" method
$dateField = (new DateFieldType('startdate'))
    ->setLocale(app()->getLocale())
    ->setViewFactory(app('view'))
    ->setOptions([
        // field common options...
    ]);

return $factory->make()
    ->add($dateField)
    ->get();

The code sample here only works on default HTML forms. If you want to add a datepicker field for use inside a metabox, it's different as you need to generate a ReactJS component and associate it with your field class -> the framework API is not yet completed for this...

jlambe avatar Aug 30 '19 09:08 jlambe

-> the framework API is not yet completed for this...

Hi @jlambe so do you mean, currently, isn't possibile to extend or add new custom Fields for metaboxes?

MoiseScalzo avatar Aug 30 '19 14:08 MoiseScalzo

@MoiseScalzo Yes it is possible but it's not polished :) Lots of improvements can be achieve there.

jlambe avatar Aug 30 '19 14:08 jlambe

@jlambe thanks...could you maybe provide a basic example on how achieve that or the link to the documentation (I wasn't able to find It)?

MoiseScalzo avatar Aug 30 '19 14:08 MoiseScalzo

@jlambe I'm playing with Metaboxes trying to load custom fields. As you said isn't seems to be so easy, especially because I'm trying to achieve that with some reverse engineering :) I made a custom field class e registered my React component but I got this error:

backend.js:1 ReferenceError: The [custom.fields.dateTime] component can not be found
at e.getComponent (themosis.core.js?ver=2.0.4:1)

How can I register my React component in another JS file or, extend themosis? Thank you in advance for your help

MoiseScalzo avatar Sep 02 '19 12:09 MoiseScalzo

@MoiseScalzo Normally the framework should expose an API in order to register components at runtime but a bug at compilation breaks it. Looking at framework core JS webpack configuration, we're trying to expose a themosis.register('componentName', componentRef) method but currently trying to compile 2 files breaks this...

jlambe avatar Sep 03 '19 10:09 jlambe

@jlambe yes exactly what I did at first time...trying to register the component with themosis.register but didn't works

MoiseScalzo avatar Sep 03 '19 11:09 MoiseScalzo