Skip to content

applyDefaults shoud be available with an option directly in props #1949

Description

@jucop-odoo

Defining a type for validation and then having to applyDefaults(props(..., type), type) is redundant.
It would be easier to be able to set it directly like props.static("properties", propType, {applyNestedDefault: true})

Current implementation

import { Component, mount, signal, props, xml, types as t, toRaw, applyDefaults } from "@odoo/owl";

const propType = t.object({
    label: t.string().optional(),
    title: t.string().optional("Default title"),
});

class Counter extends Component {
    static template = xml`
        <button t-on-click="this.increment">
          <t t-out="this.properties.label"/>: <t t-out="this.value()"/>
          <br/>
          Title : <t t-out="this.properties.title"/>
        </button>`;

    // note that the prop is fixed: it cannot change!
    properties = applyDefaults(props.static("properties", propType), propType);
    value = signal(0);
    
    increment() {
        this.value.set(this.value() + 1);
    }

    setup() {
      console.log(this.properties);
    }
}

class Root extends Component {
    static components = { Counter };
    static template = xml`
        <Counter properties="{label: 'Apples'}"/>`;
}

mount(Root, document.body, { templates: TEMPLATES, dev: true });

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions