{ constructor(); @@ -97,7 +99,15 @@ declare namespace Omi { static css?: string | CSSStyleSheet | (string | CSSStyleSheet)[] static tagName: string static define(name: string): void - + static props?: { + [key: string]: { + type?: PropType; + default?: any; + reflect?: boolean | ((value: any) => any); + changed?: (newValue: any, oldValue: any) => void; + } + } + props: OmiProps
| P prevProps: OmiProps
| P rootElement?: HTMLElement @@ -134,6 +144,14 @@ declare namespace Omi { static css?: string | CSSStyleSheet | (string | CSSStyleSheet)[] static tagName: string static define(name: string): void + static props?: { + [key: string]: { + type?: PropType; + default?: any; + reflect?: boolean | ((value: any) => any); + changed?: (newValue: any, oldValue: any) => void; + } + } props: OmiProps
| P prevProps: OmiProps
| P diff --git a/packages/omi/package.json b/packages/omi/package.json index c69a7b1e41..03a20555d0 100644 --- a/packages/omi/package.json +++ b/packages/omi/package.json @@ -1,6 +1,6 @@ { "name": "omi", - "version": "7.4.4", + "version": "7.4.5", "scripts": { "start": "vite", "dev-vite": "vite", diff --git a/packages/omi/src/component.ts b/packages/omi/src/component.ts index 83d04f627f..025bdb5faf 100644 --- a/packages/omi/src/component.ts +++ b/packages/omi/src/component.ts @@ -50,6 +50,25 @@ export class Component extends HTMLElement { constructor() { super() + + if (!this.constructor.defaultProps) { + this.constructor.defaultProps = {} + } + if (!this.constructor.propTypes) { + this.constructor.propTypes = {} + } + if (!this.constructor.reflectProps) { + this.constructor.reflectProps = {} + } + if (this.constructor.props) { + for (const propName in this.constructor.props) { + const prop = this.constructor.props[propName] + this.constructor.defaultProps[propName] = prop.default + this.constructor.propTypes[propName] = prop.type + this.constructor.reflectProps[propName] = prop.reflect + } + } + // @ts-ignore fix lazy load props missing this.props = Object.assign( {}, @@ -61,6 +80,19 @@ export class Component extends HTMLElement { this.rootElement = null } + attributeChangedCallback(name, oldValue, newValue) { + if (this.constructor.props && this.constructor.props[name]) { + const prop = this.constructor.props[name] + if (prop.changed) { + prop.changed.call(this, newValue, oldValue) + } + } + } + + static get observedAttributes() { + return this.props ? Object.keys(this.props) : [] + } + injectObject() { let p: ExtendedElement = this.parentNode as ExtendedElement // @ts-ignore deprecated diff --git a/packages/omi/src/index.ts b/packages/omi/src/index.ts index 0f3c4a6f4d..7402f29855 100644 --- a/packages/omi/src/index.ts +++ b/packages/omi/src/index.ts @@ -18,4 +18,4 @@ export { Signal } from './signal' export { css } from './css-tag' export { mixin } from './options' export { registerDirective } from './directive' -export const version = '7.4.4' +export const version = '7.4.5' diff --git a/packages/omi/test/props.test.jsx b/packages/omi/test/props.test.jsx index a1d3ac313a..21f3437dd7 100644 --- a/packages/omi/test/props.test.jsx +++ b/packages/omi/test/props.test.jsx @@ -7,6 +7,7 @@ import { getHost, h } from '@/index' +import { genNode } from './gen-node' describe('props', () => { let parentElement @@ -145,5 +146,48 @@ describe('props', () => { expect(valA === valB).toBe(true) }) + it('static props 1', () => { + let a = true + class Ele extends Component { + static props = { + count: { + default: 1 + } + } + + render(props) { + return ( +