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

Initial values for any type #1581

Closed
ORBAT opened this issue Oct 30, 2019 · 28 comments
Closed

Initial values for any type #1581

ORBAT opened this issue Oct 30, 2019 · 28 comments

Comments

@ORBAT
Copy link

ORBAT commented Oct 30, 2019

Problem

If a document field is of some custom type that always has the same default, I have to respecify those defaults in every document that uses it. As an example:

// paid.js
export default {
  name: 'paidContent',
  title: 'Paid content',
  type: 'boolean',
  description: 'Only show this to paying customers',
  options: {
    layout: "checkbox",
  },
}
// docs.js
export const someDocument {
  name: 'someDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent'
    },
  ],

  initialValue: {
    paidContent: true,
  },
}

export const anotherDocument {
  name: 'anotherDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ],

  initialValue: {
    paidContent: true,
  },
}

Proposed solution

/// paid.js
export const paidContent =  {
  name: 'paidContent',
  title: 'Paid content',
  type: 'boolean',
  description: 'Only show this to paying customers',
  options: {
    layout: "checkbox",
  },

  initialValue: {
    paidContent: true,
  },
};
// docs.js
export const someDocument {
  name: 'someDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ]
}

export const anotherDocument {
  name: 'anotherDocument',
  fields: [
    // buncha fields here
    {
      name: 'paidContent',
      type: 'paidContent',
    }
  ]
}

Both someDocument and anotherDocument would have the initial value { paidContent: true }.

Alternate solutions

E.g. Exporting the initial value, and then importing & merging it with document initial values. More brittle, requires users of the type to do it manually

@kmelve
Copy link
Member

kmelve commented Oct 30, 2019

Thanks for submitting this! It's obviously a feature that might be handy. We have no ETA on it as of yet, but feel free to follow this issue.

@matthewharwood
Copy link

@kmelve pretty please?

@ghost
Copy link

ghost commented Nov 27, 2019

I like the proposed solution from @ORBAT. I would also add that a nice addition would be to ensure that the document can override the default that is set in the type initialValue.

@brentrobbins
Copy link

I also look forward to a solution where we can add Initial values for objects.

@simonhrogers
Copy link

I would also love this to be resolved!

@MaximilianLloyd
Copy link

This is all I want for Christmas

@sisselfladby
Copy link

+1 on initial values for objects as well 🙏

@StudioRATATA
Copy link

i++
And is there a temp workaround for this? And is there any news on the ETA by now?

@johnnypea
Copy link

I would like to be able to use an initial values for objects as well.

@remisture
Copy link

+1 Yes, please :)

@christopherafbjur
Copy link

+1 Looking forward to this!!

@spacedawwwg
Copy link
Contributor

+1 for initial values for objects, please.

@fabifors
Copy link

fabifors commented Jul 9, 2020

+1 for initial / default values for objects 🙏

@djavco
Copy link

djavco commented Jul 21, 2020

+1 for would be very nice to set initial values for objects

@intensr
Copy link

intensr commented Jul 27, 2020

+1 for initial / default values for objects 🧑‍🍳

@rastko909
Copy link

This would really be awesome! Are there any workarounds?

@benjaminsehl
Copy link

+1 on this for sure, major pain point

@nzhenry
Copy link

nzhenry commented Oct 5, 2020

+1 for this. Any idea where this is on the roadmap?

@joshfarrant
Copy link

@kmelve Would you accept a PR for this? On the surface it seems fairly straight-forward, or are you expecting it to be more complex?

@joshfarrant
Copy link

Or @rexxars equally 🙂

^^^

@bryangruneberg
Copy link

Given how great everything else is, I was super surprised to see default values / initial values aren't possible for objects. Would be great to have a solution to this!

@kmelve
Copy link
Member

kmelve commented Jan 22, 2021

Hi folks! We agree that this feature makes sense. Offering a PR is super generous @joshfarrant, but we will do this one internally as it's tied to code paths that we want to clean up a bit. It's on the prioritized backlog though!

@netmet12
Copy link

Any updates here? We really need this! Are you sure you don't want to accept a PR?

@evenwestvang
Copy link
Member

evenwestvang commented Apr 21, 2021

Hi everyone! Thank you for your patience. Just wanted to let you know that we have a PR shaping up for this issue. We'll update this issue with specifics soon.

@bjoerge
Copy link
Member

bjoerge commented Apr 23, 2021

If you are keen on trying out this feature before we ship it officially, we just cut a canary release from PR #2444. You can install it in your Studio by running:

sanity upgrade --tag=canary --save-exact

It should work very much like the solution initially outlined by @ORBAT. You can see a full example of a document that uses different variations of initial values here: https://github.com/sanity-io/sanity/blob/d750f0a9e2c51984d5349edc67987ceb04e37413/examples/test-studio/schemas/initialValuesTest.js

@bjoerge
Copy link
Member

bjoerge commented Apr 28, 2021

Initial value support for any type has shipped in v2.10.0. Thank you all for your patience and valuable input!

@bjoerge bjoerge closed this as completed Apr 28, 2021
@spacedawwwg
Copy link
Contributor

@bjoerge literally came to check for an update on this! nice surprise :)

@fabien
Copy link

fabien commented Apr 30, 2021

@bjoerge this is a great addition!

However, it appears that the initialValue function does not receive any useful arguments.

For a newly created document, this could pass in the document as created using Initial Value Templates.

For nested objects (or arrays of objects) this is much more useful, since you'd be able to infer what to add there based on the main document. For example, when adding to a list of attributes (array of objects), you'd be able to lookup the initialValue of each item based off the specific document type or other document properties.

In short, for consistency with the other functions in Sanity (custom validation, uniqueness etc.) it would make sense to provide a context argument to initialValue.

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

No branches or pull requests