Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control doesn't set language #47

Open
jmartyniak opened this issue Feb 18, 2023 · 1 comment
Open

Control doesn't set language #47

jmartyniak opened this issue Feb 18, 2023 · 1 comment

Comments

@jmartyniak
Copy link

The code input control doesn't set the selected language in the published json coming out of sanity. The v2 version does.

@damsalem
Copy link

damsalem commented Feb 15, 2024

This issue should be renamed. A more relevant title is:

Language Property Doesn't Set Default Language

Summary

The language property as indicated in the ReadMe does not set the default language in Sanity, it only appears to affect the UI of the Structure editor.

// This was taken directly from the ReadMe
{
  name: 'codeField',
  title: 'Code Field',
  type: 'code',
  options: {
    language: 'javascript', // <---- This property/option is broken
    languageAlternatives: [
      {title: 'JavaScript', value: 'javascript'},
      {title: 'HTML', value: 'html'},
      {title: 'CSS', value: 'css'},
    ],
  },
  hidden: ({parent}) => parent?.showHeaderCode === 'Hide',
}

The evidence of this is that if you add a validation rule ensuring the language isn't blank, you'll find the rule triggers an error.

// Sample code to recreate this issue:
{
  name: 'codeField',
  title: 'Code Field',
  type: 'code',
  options: {
    language: 'javascript',
    languageAlternatives: [
      {title: 'JavaScript', value: 'javascript'},
      {title: 'HTML', value: 'html'},
      {title: 'CSS', value: 'css'},
    ],
  },
  hidden: ({parent}) => parent?.showHeaderCode === 'Hide',
  validation: (Rule) => Rule.required().error('Please select a language'), // <-- this will always trigger the error
}

End User Workaround

  1. From the language dropdown, select a language other than the default
  2. From the language dropdown, reselect the default language

Dev Workaround

If you don't want your end users to have to select a different language and then select the one they actually wanted. You could just add a blank option and keep that at the top. Then your users will always have to select the correct language which will

  1. Drop the broken language property
  2. Add an empty string as the top language alternative. The top language alternative is the default option.
  3. Add a validation rule requiring a language be selected
{
  name: 'codeField',
  title: 'Code Field',
  type: 'code',
  options: {
    languageAlternatives: [
      {title: '', value: ''},
      {title: 'JavaScript', value: 'javascript'},
      {title: 'HTML', value: 'html'},
      {title: 'CSS', value: 'css'},
    ],
  },
  hidden: ({parent}) => parent?.showHeaderCode === 'Hide',
  validation: (Rule) => Rule.required().error('Please select a language'),
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants