7

I am developing new extension in which I need to add color picker and date picker as well in an admin form. I am developing form using a component. Please check sample screenshot of the form field. I want to implement in UI Form. Could any one help me for that?

enter image description here

Thank You in Advance

5
  • is there anyone who can help? Commented Aug 16, 2017 at 7:16
  • I found your question :) I'll write the answer when I have a free time. It will be like text field (hidden) with a colorpicker and HEX-value of the color. Commented Aug 16, 2017 at 10:33
  • Hi, could you give answer please Commented Aug 26, 2017 at 12:18
  • This is possible & need to add color picker script. I wil send you 100% solution
    – Sourav
    Commented Nov 10, 2017 at 3:43
  • I also want to add color picker to category attribute. Could anyone help please. Commented Nov 25, 2017 at 5:54

5 Answers 5

14

I recently needed to add a color picker to a category form. Here is what wound up working:

Vendor/Module/view/adminhtml/ui_component/category_form.xml

<?xml version="1.0"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="content">
        <field name="my_color_picker">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Vendor_Module/js/form/element/color-select</item>
                    <item name="template" xsi:type="string">ui/form/field</item>
                    <item name="elementTmpl" xsi:type="string">Vendor_Module/form/element/color-select</item>
                    <item name="label" xsi:type="string">My Color Picker</item>
                    <item name="labelVisible" xsi:type="boolean">true</item>
                    <item name="visible" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                    <item name="source" xsi:type="string">category_form</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

It's essentially a generic text input, but we've defined a unique elementTmpl and component.

Vendor/Module/view/adminhtml/web/js/form/element/color-select.js

define([
    'Magento_Ui/js/form/element/abstract',
    'mageUtils',
    'jquery',
    'jquery/colorpicker/js/colorpicker'
], function (Element, utils, $) {
    'use strict';

    return Element.extend({
        defaults: {
            visible: true,
            label: '',
            error: '',
            uid: utils.uniqueid(),
            disabled: false,
            links: {
                value: '${ $.provider }:${ $.dataScope }'
            }
        },

        initialize: function () {
            this._super();
        },

        initColorPickerCallback: function (element) {
            var self = this;

            $(element).ColorPicker({
                onSubmit: function(hsb, hex, rgb, el) {
                    self.value(hex);
                    $(el).ColorPickerHide();
                },
                onBeforeShow: function () {
                    $(this).ColorPickerSetColor(this.value);
                }
            }).bind('keyup', function(){
                $(this).ColorPickerSetColor(this.value);
            });
        }
    });
});

I'm initiating the color picker with an afterRender callback. In my use case the element was not rendered on the page when my component was initialized, so adding the color picker in the initial method did not work for me.

Vendor/Module/view/adminhtml/web/template/form/element/color-select.html

<input class="admin__control-text" type="text"
       data-bind="
        event: {change: userChanges},
        value: value,
        hasFocus: focused,
        valueUpdate: valueUpdate,
        afterRender: initColorPickerCallback,
        attr: {
            name: inputName,
            placeholder: placeholder,
            'aria-describedby': noticeId,
            id: uid,
            disabled: disabled
    }"/>

You'll also want to add the color picker css as a layout update, so the filename will vary:

Vendor/Module/view/adminhtml/layout/{frontName}_{controller}_{action}.xml

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <css src="jquery/colorpicker/css/colorpicker.css"/>
    </head>
</page>

End Result (Input Focus State) End Result (Input Focus State)

5
  • Hi is this work? I tried, its working also but it does not auto fill color code to textbox. Can you please let us know how to solve this? thank you. Commented Nov 20, 2017 at 7:00
  • @Vish see post below
    – John
    Commented Mar 19, 2019 at 15:58
  • i followed same tutorial but got a problem can u please check [Here](magento.stackexchange.com/questions/275929/… )
    – Waqar Ali
    Commented May 24, 2019 at 4:53
  • @pmclain Can you share this module? I also need to add color picker field in category edit page of admin. Commented Jul 7, 2021 at 15:04
  • The formElement should be colorPicker to work colorFormat properly.
    – Eduardo
    Commented Sep 13, 2022 at 11:08
9

I added onChange event

            $(element).ColorPicker({
            onSubmit: function(hsb, hex, rgb, el) {
                self.value(hex);
                $(el).ColorPickerHide();
            },
            onBeforeShow: function () {
                $(this).ColorPickerSetColor(this.value);
            },
            onChange: function(hsb, hex, rgb, el) {
                self.value('#'+hex);
            }

enter image description here

5

The best way to add color picker in field is .

<field name="md_label_background_color">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/form/element/color-picker</item>
                <item name="template" xsi:type="string">ui/form/field</item>
                <item name="elementTmpl" xsi:type="string">ui/form/element/color-picker</item>
                <item name="label" xsi:type="string">Background Color (hex)</item>
                <item name="labelVisible" xsi:type="boolean">true</item>
                <item name="visible" xsi:type="boolean">true</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">category_form</item>
            </item>
        </argument>
    </field>

No need to addd any file!!

1
  • how can we add it to frontend ui form.I have tried and it don't show color picker shade on it.rest was working. Commented Nov 25, 2020 at 10:17
3

There is already a colorPicker Component defined in the core.

vendor/magento/module-ui/view/base/ui_component/etc/definition/colorPicker.xsd

Try adding this to your form:

<colorPicker name="colors_picker" class="Magento\Ui\Component\Form\Element\ColorPicker" component="Magento_Ui/js/form/element/color-picker" template="ui/form/element/color-picker" provider="${ $.parentName }">
    <settings>
        <label translate="true">Color</label>
        <colorFormat>HEX</colorFormat>
        <colorPickerMode>full</colorPickerMode>
        <dataScope>colors_picker</dataScope>
    </settings>
</colorPicker>
1
  • color picker data is not saving in database. Commented Feb 5, 2020 at 10:34
0

Simple way to add colorpicker in admin ui form

Vendor/Module/view/adminhtml/ui_component/custom_form.xml

<field name="background_color" sortOrder="2" formElement="colorPicker" component="Magento_Ui/js/form/element/color-picker">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="source" xsi:type="string">category_slider</item>
        </item>
    </argument>
    <settings>
        <validation>
            <rule name="required-entry" xsi:type="boolean">true</rule>
        </validation>
        <dataType>text</dataType>
        <label translate="true">Slider background color</label>
        <dataScope>background_color</dataScope>
        <elementTmpl>ui/form/element/color-picker</elementTmpl>
    </settings>
    <formElements>
        <colorPicker class="Magento\Ui\Component\Form\Element\ColorPicker">
            <settings>
                <colorFormat>hex</colorFormat>
                <colorPickerMode>full</colorPickerMode>
            </settings>
        </colorPicker>
    </formElements>
</field>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.