Props argument in default argument of a prop #14750
Replies: 1 comment
-
|
The That means your use case is valid in principle: a default for one prop can be derived from another prop such as The important caveat is that this argument is the raw props object, not the fully resolved props object. So it is useful for checking values the parent actually passed, but you should not rely on other props already having their own defaults applied. A smaller version of the pattern would be: props: {
displayType: {
type: String,
required: true,
},
chartConfig: {
type: Object,
default(rawProps: Record<string, unknown>) {
const displayType = rawProps.displayType as string | undefined
return displayType && propMap[displayType]
? propMap[displayType].chartConfig
: {}
},
},
}So the reason Vue passes For your generated/default-wrapping approach, I would keep two rules:
Docs: https://vuejs.org/guide/components/props.html#prop-validation |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
props: {
prop: {
type: String,
default: function(props:Record<string, any>){}
}
}
what is the reason we have the props as an argument in default. We tried to replace the defaults of the props using another prop.
So the use case goes like this, we have a prop that determine the type of a chart and we have prop map of each type. So the type prop is used to determine the default value of the other props. So we tried to utilise the props argument in the default function to access the type prop and setting the default of the other props.
function getDefaultProp(props:Record<string, any>,name:string,defaultValue?:any):any {
let displayType = props.displayType;
}
export const props = {
displayType: { type: String, required: true },
.
.
.
}
for(let name in props) {
let value = props[name];
const def = extend({},value);
def.default =()=>undefined;
propsWithoutDefault[name] = def;
if(name ==="displayType"){
continue;
}
if (!value || !value.type) {
Logger.fatal(
Prop '${name}' type is not defined inside object. for flo chart it is mandatory to define the prop as <prop_name> : { type : <String|Number| etc>}.);continue;
}
}
Beta Was this translation helpful? Give feedback.
All reactions