How to use the @commercetools-frontend/ui-kit.LocalizedTextInput.omitEmptyTranslations function in @commercetools-frontend/ui-kit

To help you get started, we’ve selected a few @commercetools-frontend/ui-kit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github commercetools / ui-kit / examples / form-inputs.example.story.js View on Github external
// validate slug
  // A slug must match [a-zA-Z0-9_-]{2,256}
  // The error object of the slug is
  //  {
  //    missing: Boolean,
  //    translations: { de: { hasForbiddenChars: Boolean }, ... },
  //  }
  // The "missing" part is used to highlight all fields, while the
  // "translations" part gets mapped to errors per translation.
  if (LocalizedTextInput.isEmpty(formValues.slug)) {
    errors.slug.missing = true;
  } else {
    const isValidSlug = value => /^[a-zA-Z0-9_-]{2,256}$/.test(value);
    const translationErrors = Object.keys(
      LocalizedTextInput.omitEmptyTranslations(formValues.slug)
    ).reduce((acc, language) => {
      const value = isValidSlug(formValues.slug[language])
        ? {}
        : { hasForbiddenChars: true };
      return {
        ...acc,
        [language]: value,
      };
    }, {});

    errors.slug.translations = translationErrors;
  }

  // validate description
  if (MultilineTextInput.isEmpty(formValues.description))
    errors.description.missing = true;