<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Keval Badarakhiya</title>
    <description>The latest articles on DEV Community by Keval Badarakhiya (@keval_badarakhiya).</description>
    <link>https://dev.to/keval_badarakhiya</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2458062%2Ff3aa705a-c3b2-4f86-861c-ee2728ca5365.jpg</url>
      <title>DEV Community: Keval Badarakhiya</title>
      <link>https://dev.to/keval_badarakhiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keval_badarakhiya"/>
    <language>en</language>
    <item>
      <title>A Guide to Creating Custom Field Inputs in Strapi (version 5)</title>
      <dc:creator>Keval Badarakhiya</dc:creator>
      <pubDate>Thu, 21 Nov 2024 06:32:57 +0000</pubDate>
      <link>https://dev.to/keval_badarakhiya/a-guide-to-creating-custom-field-inputs-in-strapi-version-5-79o</link>
      <guid>https://dev.to/keval_badarakhiya/a-guide-to-creating-custom-field-inputs-in-strapi-version-5-79o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Strapi&lt;/strong&gt; is an open-source headless CMS that provides powerful customization capabilities. One of the standout features is its ability to add custom field inputs, allowing developers to tailor the content management experience to their specific needs.&lt;/p&gt;

&lt;p&gt;In this guide, we'll walk you through creating custom field inputs in Strapi(Version 5).&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Custom Field Inputs?
&lt;/h2&gt;

&lt;p&gt;Custom field inputs in Strapi allow developers to extend the default field types (text, number, boolean, etc.) with unique functionalities.&lt;/p&gt;

&lt;p&gt;For example, you might need a color picker, a tag input, or even a complex JSON editor in your CMS.&lt;/p&gt;

&lt;p&gt;Custom fields enhance the flexibility of Strapi by letting developers define input types specific to their use case while keeping the admin panel user-friendly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To follow along, make sure you have:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strapi v5 installed:&lt;/strong&gt; Ensure you’re working with Strapi version 5.&lt;br&gt;
&lt;strong&gt;Node.js:&lt;/strong&gt; Installed on your machine (recommended: v16 or higher).&lt;br&gt;
&lt;strong&gt;Basic understanding of Strapi:&lt;/strong&gt; Familiarity with the Strapi admin panel and plugin structure.&lt;br&gt;
&lt;strong&gt;Text editor:&lt;/strong&gt; Such as VS Code.&lt;/p&gt;



&lt;p&gt;Now, Let's start to create custom field plugin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a New Strapi Plugin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ Strapi plugins provide a modular way to add features to your application, including custom fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-strapi-plugin custom-field-input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate the basic structure of your plugin in the &lt;code&gt;src/plugins&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Define the Custom Field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ In your plugin folder &lt;code&gt;(src/plugins/custom-field-input/server/register.js)&lt;/code&gt; , create a file to define the new custom field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/plugins/custom-field-input/server/src/register.js

module.exports = {
  async register({ strapi }) {
    strapi.customFields.register({
      name: 'custom-input',
      plugin: 'custom-field-input',
      type: 'string', // The data type stored in the database
    });
  },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we &lt;code&gt;register the custom field&lt;/code&gt; and specify that it stores a string type in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Build the Admin UI Component&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ The admin panel needs a React component to render your custom input field. Strapi uses React for its admin interface, so you'll define your field in React.&lt;br&gt;
Create a &lt;code&gt;components&lt;/code&gt; folder in your plugin:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p src/plugins/custom-field-input/admin/src/components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the &lt;code&gt;components&lt;/code&gt; folder, create a file for your custom field input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/plugins/custom-field-input/admin/src/components/CustomInput.js
import React from 'react';

const CustomInput = ({ value, onChange, name, intlLabel, description }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;label htmlFor={name}&amp;gt;{intlLabel.defaultMessage}&amp;lt;/label&amp;gt;
      &amp;lt;input
        id={name}
        name={name}
        type="text"
        value={value || ''}
        onChange={(e) =&amp;gt; onChange({ target: { name, value: e.target.value } })}
      /&amp;gt;
      {description &amp;amp;&amp;amp; &amp;lt;p&amp;gt;{description.defaultMessage}&amp;lt;/p&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};

export default CustomInput;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This component provides a simple text input with a &lt;code&gt;label&lt;/code&gt; and &lt;code&gt;description&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Connect the Admin UI to the Plugin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ Update the &lt;code&gt;admin&lt;/code&gt; entry file to register your custom field in the admin panel suppose I have a plugin (PLUGIN_ID) name styled-textinput.&lt;br&gt;
path : &lt;code&gt;/src/plugins/YourCustomPlugin/admin/src/index.js or register.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PLUGIN_ID } from './pluginId';
export default {
  register(app) {

    app.customFields.register({
      name: "styledTextarea",
      pluginId: `${PLUGIN_ID}`,
      type: "string",
      intlLabel: {
        id: `${PLUGIN_ID}.styledTextarea.label`,
        defaultMessage: "Text Area",
      },
      intlDescription: {
        id: `${PLUGIN_ID}.styledTextarea.description`,
        defaultMessage: "Create Your Text Aread",
      },
      components: {
        Input: async () =&amp;gt; import("./components/`(custom field UI component location)`"),
      },
    })
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;➡️ Also, don't forget to register/modify your &lt;code&gt;plugin.ts&lt;/code&gt; in the &lt;code&gt;config&lt;/code&gt; folder :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  'your_plugin_name': {
    enabled: true,
    resolve: './src/plugins/your_plugin_name'
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 5: Add Translations (Optional)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ This part is optional. However, if you want to add this translation part you may refer to this &lt;a href="https://docs.strapi.io/dev-docs/custom-fields" rel="noopener noreferrer"&gt;custom-fields with translation guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Test Your Custom Field&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;➡️ According to the strapi 5 documentation, You need to run this command &lt;code&gt;(npm run build)&lt;/code&gt; on this path &lt;code&gt;(/src/plugins/your_custom_plugin_name)&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;p&gt;➡️ After making the build, You will be able to run the project and see your custom field input by this command &lt;code&gt;(npm run develop)&lt;/code&gt; on root directory &lt;code&gt;(/your_strapi_project)&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start your Strapi project:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run develop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Go to the &lt;code&gt;Content-Type Builder&lt;/code&gt; in the admin panel.&lt;/li&gt;
&lt;li&gt;Add a new field to a &lt;code&gt;content type&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Select &lt;code&gt;your custom field&lt;/code&gt; (Custom Input Field) from the list of available fields.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You should see your custom input field appear in the content editor. I am sure that this content will help to create your custom Input field plugin in your strapi project (version 5).&lt;/p&gt;

&lt;p&gt;"If you found this guide helpful, don’t forget to clap, like, and share it with your network. I’d love to hear your thoughts or answer any questions—feel free to leave a comment below. Thank you for taking the time to read this guide and happy coding! 😊"&lt;/p&gt;

</description>
      <category>customfields</category>
      <category>strapiplugins</category>
      <category>strapi</category>
      <category>headlesscms</category>
    </item>
  </channel>
</rss>
