Skip to content

Commit

Permalink
feat(omi): more concise static props definitions for cross framework use
Browse files Browse the repository at this point in the history
  • Loading branch information
dntzhang committed Jan 11, 2024
1 parent 0809eaf commit 9953bd5
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 29 deletions.
14 changes: 7 additions & 7 deletions packages/omi-vue/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/omi-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"omi": "^7.3.8",
"omi": "^7.4.5",
"omi-weui": "^0.0.10",
"vue": "^3.3.8"
},
Expand Down
18 changes: 9 additions & 9 deletions packages/omi-vue/src/components/my-counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { define, Component, h } from 'omi'

define('my-counter', class extends Component {

static propTypes = {
count: Number
}

static observedAttributes = ['count']

attributeChangedCallback(name, oldValue, newValue) {
this.state[name] = newValue
this.update()
static props = {
count: {
type: Number,
default: 0,
changed(newValue, oldValue) {
this.state.count = newValue
this.update()
}
}
}

state = {
Expand Down
18 changes: 9 additions & 9 deletions packages/omi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,15 @@ import { define, Component, h } from 'omi'

define('my-counter', class extends Component {

static propTypes = {
count: Number
}

static observedAttributes = ['count']

attributeChangedCallback(name, oldValue, newValue) {
this.state[name] = newValue
this.update()
static props = {
count: {
type: Number,
default: 0,
changed(newValue, oldValue) {
this.state.count = newValue
this.update()
}
}
}

state = {
Expand Down
69 changes: 69 additions & 0 deletions packages/omi/examples/static-props.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { render, tag, Component, h, bind } from '@/index'

@tag('counter-demo')
class CounterDemo extends Component {
static props = {
count: {
count: Number,
// count: [Number, String, Array],
default: 20,
reflect: true,
// or reflect(value) {}
changed(newValue, oldValue) {
console.log('changed', newValue, oldValue)
}
}
}

state = {
count: 0
}

install() {
this.state.count = this.props.count
}

receiveProps(): void {
if (this.props.count === this.state.count) {
return false
}

this.state.count = this.props.count
}

@bind
add() {
this.state.count++
this.update()
this.fire('change', this.state.count)
}

render() {
return (
<>
<span>{this.state.count} <button onClick={this.add}>+</button></span>
</>
)
}
}


@tag('my-app')
class MyApp extends Component {
count = 10

@bind
onChange(evt) {
this.count = evt.detail
this.update()
}

render() {
return (

<counter-demo onChange={this.onChange} count={this.count}></counter-demo>

)
}
}
render(<my-app />, document.body)
20 changes: 19 additions & 1 deletion packages/omi/omi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ declare namespace Omi {
css?: (() => string) | string;
}

type PropType = String | Number | Boolean | Array<any> | Object | Array<PropType>;

abstract class WeElement<P = {}> {
constructor();

Expand All @@ -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> | P
prevProps: OmiProps<P> | P
rootElement?: HTMLElement
Expand Down Expand Up @@ -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> | P
prevProps: OmiProps<P> | P
Expand Down
2 changes: 1 addition & 1 deletion packages/omi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omi",
"version": "7.4.4",
"version": "7.4.5",
"scripts": {
"start": "vite",
"dev-vite": "vite",
Expand Down
32 changes: 32 additions & 0 deletions packages/omi/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{},
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/omi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
44 changes: 44 additions & 0 deletions packages/omi/test/props.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getHost,
h
} from '@/index'
import { genNode } from './gen-node'

describe('props', () => {
let parentElement
Expand Down Expand Up @@ -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 (
<div>{props.count}</div>
)
}
}
let node = genNode()
define(node.name, Ele)
render(<Ele />, parentElement)
expect(parentElement.firstChild.shadowRoot.innerHTML).toBe('<div>1</div>')

})

it('static props 2', () => {
let a = true
class Ele extends Component {
static props = {
count: {
default: 1,
}
}

render(props) {
return (
<div>{props.count}</div>
)
}
}
let node = genNode()
define(node.name, Ele)
render(<Ele count={2} />, parentElement)
expect(parentElement.firstChild.shadowRoot.innerHTML).toBe('<div>2</div>')

})

})

0 comments on commit 9953bd5

Please sign in to comment.