-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Allow specifying a dynamic default value via a function #1407
Comments
I've been thinking about this recently - we'd need a way to do it for fields in the config.yml, too. |
@papandreou check out my proposal in #1409, you brought up a good case here that helped. The caveat is that we need to get rid of |
Related to #725. |
@erquhart, cool. That approach looks good to me! |
I don't know if it's a standard thing, but I noticed that the yaml parser I use mentions a funky Might be too weird, though :) |
Yeah, I'm aware of js-in-yaml, and agree that it's weird. Extensions shipping functions feels pretty sane. |
A possible implementation for editor components (not |
Hello, I don't know if what I'm trying to reach is already possible or it could be related to this request. Thanks! |
Managed to hack around this by creating a custom CMS.registerWidget(
'uuid',
class extends React.Component {
static propTypes = {
onChange: PropTypes.func.isRequired,
forID: PropTypes.string,
value: PropTypes.node,
classNameWrapper: PropTypes.string.isRequired
};
static defaultProps = {
value: ''
};
componentDidMount() {
const { value, onChange } = this.props;
if (!value) {
onChange(generateUuid());
}
}
render() {
const { value, classNameWrapper, forID } = this.props;
return (
<span id={forID} className={classNameWrapper}>
{value}
</span>
);
}
}
); |
Makes sense - a dedicated id widget might be a good idea to include with the cms, too. |
I guess -- if it's the only use case we can come up with for the dynamic default value feature 😆 |
Lol exactly, I still haven't thought of another one Sent with GitHawk |
@erquhart Would it be worth thinking about this from a different perspective? Could you add a 'uid' field that supported interpolation in the same way that 'slug' does? They are very similar - both are used once on creation. If this was supported you could do:
You could simplify this by adding a
If you think this is worthwhile I'll happily put together a PR. |
The issue there is consistency and avoiding privileged entities. Frontmatter values are currently always generated by a widget, how do you see your solution working within that context? Sent with GitHawk |
I suppose it depends on how you look at the I can see your concerns about this field having unique behaviour, but a uuid field is by definition unique: it should never change. It has to be stored as a field because there is nowhere else to store it, but I don't think that means it has to be treated like other fields. It is a special case. I think there's a good argument for a uuid being generated for everything by default and used for tracking relations. Anyhow, I think a robust solution is needed, as this is currently quite a big weakness in the library: the fact that relations are based on fields that are not protected from user editing makes the use of relations deeply unstable. |
Definitely agree on the need for it. I think the ideal approach is probably a combination of our suggestions: supporting placeholders in the { name: id, widget: hidden, default: "{{uuid}}" } Sent with GitHawk |
That sounds like a good solution to me. I guess the other thing to consider is whether all items that are created should have a uuid field: is this something that should happen by default? This would definitely be a great help when the need for a relation emerges later on. |
An additional consideration. Because of how a |
I just came across the same issue, and I agree width @Undistraction that It will better to approach it differently, as the uuid is an internal value that has no meaning for the cms user. As the project I'm working on does not include React, I create an uuid widget like @papandreou but with <!-- Include the script that builds the page and powers Netlify CMS -->
<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js"></script>
<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
<script>
CMS.registerWidget(
'uuid',
createClass({
getDefaultProps: function() {
return {
value: ''
}
},
uuid: function(options) {
options = options || {};
var size = options.size || 21;
var url = options.url || "Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz-";
var id = "";
if (typeof self === "undefined" || (!self.crypto && !self.msCrypto)) {
while (0 < size--) {
id += url[(Math.random() * 64) | 0];
}
return id;
}
// else
var crypto = self.crypto || self.msCrypto;
var bytes = crypto.getRandomValues(new Uint8Array(size));
while (0 < size--) {
id += url[bytes[size] & 63];
}
return id;
},
componentDidMount: function() {
var value = this.props.value;
var onChange = this.props.onChange;
var uuid = this.uuid;
if (!value) {
onChange(uuid());
}
},
render: function() {
var value = this.props.value;
var classNameWrapper = this.props.classNameWrapper;
var forID = this.props.forID;
return h('span', { id: forID, className: classNameWrapper }, value);
}
})
);
</script> |
Relevant: #1975 (comment) |
I think the bigger issue here is that widgets need to set their own default values, the CMS has no way of doing that correctly. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
- Do you want to request a feature or report a bug?
feature
- What is the current behavior?
The default value of a field has to be a fixed string/number/boolean (depending on the field type).
- What is the expected behavior?
It would be great to be able specify a function that would return the desired value (maybe even allow it to return a promise for it, for even greater flexibility).
My use case is that I would like to generate
uuid
s for my collection entries and editor components, and store them in a field withwidget: 'hidden'
.Would be great to be able to do something like:
The text was updated successfully, but these errors were encountered: