Giter Club home page Giter Club logo

Comments (4)

d4rekanguok avatar d4rekanguok commented on September 23, 2024 1

Currently the generate function is coded within the React component that drives the input:

public generateId() {
const { field, onChange } = this.props
const usePrefix = field.get('prefix')
const usePostfix = field.get('postfix')
const useTimestamp = field.get('timestamp')
const prefix = usePrefix ? usePrefix + '-' : ''
const timestamp = useTimestamp ? Date.now() + '-' : ''
const postfix = usePostfix ? '-' + usePostfix : ''
const id = prefix + timestamp + shortid() + postfix
onChange(id)
}

I think it'd be good to extract it out into a util function & export it

from netlify-cms-widgets.

d4rekanguok avatar d4rekanguok commented on September 23, 2024

Hi @henrikwirth, thanks for opening this issue!

It looks like there's a 'prepublish' event we can hook into to generate a uuid for a field. I think this could potentially solve this issue:
https://www.netlifycms.org/docs/beta-features/#registering-to-cms-events

This repo is quite outdated so I'm hesitate to dive back in, if you want to take a crack at setting something up (a new widget, or an update to this one) I'm happy to assist with any issues you might run into!

from netlify-cms-widgets.

henrikwirth avatar henrikwirth commented on September 23, 2024

I think I might have found a way. There is a newRecord: true on the data that is passed to the prePublish hook if the item is newly created/duplicated. If we check for that, we can trigger the generation of the ID right there instead of initially pre-populated.

from netlify-cms-widgets.

henrikwirth avatar henrikwirth commented on September 23, 2024
CMS.registerEventListener({
  name: "preSave",
  handler: ({ entry }) => {
    const data = entry.get("data");

    if (entry.get("newRecord")) {
      return data.set("id", "test-1234");
    }
  },
});

Not sure how we would access the Control Object to generate the ID according to the props. Any idea?

from netlify-cms-widgets.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.