Tailwind Logo

Component Creation and Template Linkage - Date and Number Edition

Next.js

Published: 2022-04-06

As for dates and numbers, they can be managed using text fields, but it is possible to set up dates, times, and numbers as fields in Sitecore. In this article, we will show you how to use the fields so that you can structure your content using these items.

Creating Date and Numeric Components

This time we will create a component to handle dates and numbers and a component to display content on the page.

Component Creation

First, prepare two Data/DateNumberSample definition files by executing the following code.

PowerShell
jss scaffold Data/DateNumberSample
template20.png

This will create the following two files

  • sitecore/definitions/components/Data/DateNumberSample.sitecore.ts
  • src/components/Data/DateNumberSample.tsx

The first file can define the types of fields that make up the template. Add code to the following file

TypeScript
export default function DateNumberSample(manifest: Manifest): void {
  manifest.addComponent({
    name: 'DateNumberSample',
    icon: SitecoreIcon.DocumentTag,
    fields: [
      { name: 'number1', type: CommonFieldTypes.Number },
      { name: 'date1', type: CommonFieldTypes.Date },
      { name: 'datetime1', type: CommonFieldTypes.DateTime },
    ],
  });
}

Then edit the file that defines the rendering, remembering that DateField has been added to the import.

TypeScript
import { Text, DateField, Field, withDatasourceCheck } from '@sitecore-jss/sitecore-jss-nextjs';
import { StyleguideComponentProps } from 'lib/component-props';

type DateNumberSampleProps = StyleguideComponentProps & {
  fields: {
    number1: Field<number>;
    date1: Field<string>;
    datetime1: Field<string>;
  };
};

const DateNumberSample = (props: DateNumberSampleProps): JSX.Element => (
  <div>
    <h2>数字</h2>
    <div>
      <Text field={props.fields.number1} />
    </div>
    <h2>日付</h2>
    <div>
      <DateField field={props.fields.date1} />
    </div>
    <h2>日付と時間</h2>
    <div>
      <DateField field={props.fields.datetime1} />
    </div>
  </div>
);

export default withDatasourceCheck()<DateNumberSampleProps>(DateNumberSample);

Create a template in Sitecore

Same development as before, but create a template in Sitecore. This time we will have three fields.

template21.png

Creation of folders for data storage

Create a folder named DateNumberSample under the already created Components/Data folder. Then, create a sample item under that folder. When creating the item, it is necessary to specify a template, which is the template created in the previous step.

template22.png

We have included each of these data and created sample data as well.

template23.png

Add renderings, set placeholders

Then add rendering. Add a new Json Rendering under the path /sitecore/layout/Renderings/Project/sitecoredemo-jp/Data. Specify DateNumberSample as the rendering name. Set the following two items in the settings of the rendering you created.

  • Data Source Location: Pre-created folders
  • Data Source Template: Pre-created templates

The settings are as follows

template24.png

Add the rendering you have created to the placeholder.

template25.png

Verification

Paste the components you have already created onto the page. The following image shows the procedure.

template26.gif

To edit the data of a placed component, click on the Edit content item in the upper right corner to edit the respective item.

template27.png

When the Experience Editor is selected instead of Horizon, the dialog is available for date selection.

template28.png

Summary

In this article, we have shown how to use `Field<number>` in terms of dealing with numbers. We also saw how the `DateField` can be used for dates and date and time, allowing you to use the calendar control when editing items.

Tags