Skip to content

Commit

Permalink
feat(StringInputWithCharacterCount): use character count input for mo…
Browse files Browse the repository at this point in the history
…re string fields
  • Loading branch information
mathiazom committed Sep 11, 2024
1 parent ab49dbc commit dd964f4
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 27 deletions.
5 changes: 5 additions & 0 deletions studio/schemas/builders/pageBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import testimonals from "../objects/sections/testimonials";
import imageSection from "../objects/sections/image";
import grid from "../objects/sections/grid";
import contactForm from "../objects/sections/form";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const pageBuilderID = "pageBuilder";

Expand All @@ -26,6 +27,10 @@ const pageBuilder = defineType({
"Enter a distinctive name for the dynamic page to help content editors easily identify and manage it. This name is used internally and is not visible on your website.",
type: "string",
validation: (Rule) => Rule.required().max(30),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 30 }),
},
}),
pageSlug,
seo,
Expand Down
15 changes: 14 additions & 1 deletion studio/schemas/documents/blog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineField, defineType } from "sanity";
import seo from "../objects/seo";
import { pageSlug } from "../schemaTypes/slug";
import { title } from "../fields/text";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const blogId = "blog";

Expand All @@ -17,6 +18,10 @@ const blog = defineType({
"Enter a distinctive name for the page to help content editors easily identify and manage it. This name is used internally and is not visible on your website.",
type: "string",
validation: (Rule) => Rule.required().max(30),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 30 }),
},
}),
pageSlug,
seo,
Expand All @@ -28,7 +33,11 @@ const blog = defineType({
"Enter the label used to refer to all posts regardless of their category. This label will be displayed in the filter section on the main blog page. Examples include 'news', 'stories', or 'posts'.",
type: "string",
initialValue: "All posts",
validation: (Rule) => Rule.required(),
validation: (Rule) => Rule.required().max(20),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 20 }),
},
}),
defineField({
name: "categories",
Expand All @@ -49,6 +58,10 @@ const blog = defineType({
"The name of the category. This will be displayed on the website and used for organizing blog posts.",
type: "string",
validation: (Rule) => Rule.required().min(1).max(100),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 100 }),
},
}),
],
}),
Expand Down
6 changes: 6 additions & 0 deletions studio/schemas/documents/companyInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineType, defineField } from "sanity";
import seo from "../objects/seo";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const companyInfoID = "companyInfo";

Expand All @@ -24,6 +25,11 @@ const companyInfo = defineType({
type: "string",
title: "Site Name",
description: "The name of your website.",
validation: (rule) => rule.max(60),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 60 }),
},
}),
defineField({
name: "defaultLanguage",
Expand Down
6 changes: 6 additions & 0 deletions studio/schemas/documents/companyLocation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineField, defineType } from "sanity";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const companyLocationID = "companyLocation";
export const companyLocationNameID = "companyLocationName";
Expand All @@ -13,6 +14,11 @@ const companyLocation = defineType({
name: companyLocationNameID,
type: "string",
title: "Location",
validation: (rule) => rule.max(50),
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: 50 }),
},
}),
],
});
Expand Down
7 changes: 6 additions & 1 deletion studio/schemas/fields/categories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const categoriesId = "categories";

Expand All @@ -12,6 +13,10 @@ const categories = defineField({
type: "string",
title: "Category",
validation: (Rule) => Rule.required().min(1).max(100),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 100 }),
},
},
],
});
Expand Down
34 changes: 17 additions & 17 deletions studio/schemas/fields/media.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export enum ImageAlignment {
Left = "left",
Expand All @@ -15,20 +16,25 @@ const alignmentOptions = [
{ title: "Right", value: ImageAlignment.Right },
];

const imageAltField = defineField({
name: "alt",
type: "string",
title: "Alternative Text",
description:
"Provide a description of the image for accessibility. Leave empty if the image is purely decorative.",
validation: (rule) => rule.max(100),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 100 }),
},
});

const image = defineField({
name: "image",
title: "Image",
type: "image",
options: { hotspot: true },
fields: [
{
name: "alt",
type: "string",
title: "Alternative Text",
description:
"Provide a description of the image for accessibility. Leave empty if the image is purely decorative.",
},
],
fields: [imageAltField],
});

export const imageExtended = defineField({
Expand All @@ -37,13 +43,7 @@ export const imageExtended = defineField({
type: "image",
options: { hotspot: true },
fields: [
{
name: "alt",
type: "string",
title: "Alternative Text",
description:
"Provide a description of the image for accessibility. Leave empty if the image is purely decorative.",
},
imageAltField,
{
name: "imageAlignment",
title: "Image Alignment",
Expand Down
8 changes: 8 additions & 0 deletions studio/schemas/fields/text.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StringRule, defineField } from "sanity";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

enum titleID {
basic = "basicTitle",
Expand Down Expand Up @@ -35,6 +36,10 @@ const createField = ({
title,
type: "string",
validation: validationRules,
components: {
input: (props) =>
StringInputWithCharacterCount({ ...props, maxCount: maxLength }),
},
});
};

Expand All @@ -54,6 +59,9 @@ export const optionalSubtitle = defineField({
title: "Subtitle",
type: "string",
validation: (Rule) => Rule.max(60),
components: {
input: (props) => StringInputWithCharacterCount({ ...props, maxCount: 60 }),
},
});

export const richTextID = "richText";
Expand Down
12 changes: 10 additions & 2 deletions studio/schemas/objects/footerSection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defineType } from "sanity";
import { defineType, StringInputProps } from "sanity";
import { linkID } from "./link";
import { soMeLinksID } from "../documents/socialMediaProfiles";
import { richText, richTextID } from "../fields/text";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const footerSectionID = {
main: "footerSection",
Expand Down Expand Up @@ -32,7 +33,14 @@ export const footerSection = defineType({
type: "string",
description:
"Enter the title for this footer section. This will help identify the section within the footer.",
validation: (Rule) => Rule.required().error("Section title is required"),
validation: (Rule) => [
Rule.required().error("Section title is required"),
Rule.max(60),
],
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 60 }),
},
},
{
name: footerSectionID.type,
Expand Down
8 changes: 7 additions & 1 deletion studio/schemas/objects/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import AnchorSelect from "../../components/AnchorSelect";
import LinkTypeSelector from "../../components/LinkTypeSelector";
import NewTabSelector from "../../components/NewTabSelector";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";

export const linkID = "link";

Expand Down Expand Up @@ -34,6 +35,11 @@ export const link = defineField({
title: "Provide a link title",
type: "string",
description: "Enter the link text that will be displayed on the website.",
validation: (rule) => rule.max(60),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 60 }),
},
},
{
name: "linkType",
Expand Down
8 changes: 7 additions & 1 deletion studio/schemas/objects/sections/article.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import { richText, title } from "../../../schemas/fields/text";
import { imageExtended } from "../../../schemas/fields/media";
import { link } from "../link";
import { StringInputWithCharacterCount } from "../../../components/StringInputWithCharacterCount";

export const articleID = "article";

Expand All @@ -14,6 +15,11 @@ export const article = defineField({
name: "tag",
title: "Tag",
type: "string",
validation: (rule) => rule.max(60),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 60 }),
},
},
title,
richText,
Expand Down
7 changes: 6 additions & 1 deletion studio/schemas/objects/sections/hero.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// hero.ts
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import callToActionField from "../../fields/callToActionFields";
import CustomCallToActions from "../../../components/CustomCallToActions";
import { title } from "studio/schemas/fields/text";
import { StringInputWithCharacterCount } from "../../../components/StringInputWithCharacterCount";

export const heroID = "hero";

Expand All @@ -17,6 +18,10 @@ export const hero = defineField({
title: "Description",
type: "string",
validation: (Rule) => Rule.max(200),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 200 }),
},
},
{
name: "callToActions",
Expand Down
11 changes: 9 additions & 2 deletions studio/schemas/objects/sections/logoSalad.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import image from "studio/schemas/fields/media";
import { richText } from "studio/schemas/fields/text";
import { StringInputWithCharacterCount } from "../../../components/StringInputWithCharacterCount";

export const logoSaladID = "logoSalad";

Expand All @@ -21,8 +22,14 @@ export const logoSalad = defineField({
type: "string",
description:
"Required text displayed in a smaller body text style. Use it to provide additional context or details about the logos.",
validation: (Rule) =>
validation: (Rule) => [
Rule.required().error("Logo description is required."),
Rule.max(100),
],
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 100 }),
},
},
{
name: "logos",
Expand Down
5 changes: 4 additions & 1 deletion studio/schemas/objects/seo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineField } from "sanity";
import { defineField, StringInputProps } from "sanity";
import { StringInputWithCharacterCount } from "studio/components/StringInputWithCharacterCount";

const seoFieldID = {
Expand Down Expand Up @@ -59,6 +59,9 @@ const seo = defineField({
title: "SEO & Social Media Keywords",
description:
"Enter targeted keywords to enhance your content’s visibility in search engines and social media platforms. Use relevant and specific keywords that describe your content, helping to attract the right audience and improve your SEO performance",
components: {
input: StringInputWithCharacterCount,
},
}),
defineField({
name: seoFieldID.image,
Expand Down
7 changes: 7 additions & 0 deletions studio/schemas/objects/testimony.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { richText, title } from "../fields/text";
import image from "../fields/media";
import { StringInputWithCharacterCount } from "../../components/StringInputWithCharacterCount";
import { StringInputProps, StringRule } from "sanity";

export const testimony = {
name: "testimony",
Expand All @@ -11,6 +13,11 @@ export const testimony = {
name: "subTitle",
type: "string",
title: "Subtitle",
validation: (rule: StringRule) => rule.max(100),
components: {
input: (props: StringInputProps) =>
StringInputWithCharacterCount({ ...props, maxCount: 100 }),
},
},
image,
richText,
Expand Down

0 comments on commit dd964f4

Please sign in to comment.