Skip to content

Commit

Permalink
refactor: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-He95 committed Apr 8, 2023
1 parent 08ecb05 commit 0a46ad2
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"devDependencies": {
"@ant-design/icons": "^5.0.1",
"@antfu/eslint-config": "^0.34.0",
"@simon_he/base-webcomponent": "^0.0.14",
"@simon_he/base-webcomponent": "^0.0.17",
"@types/node": "^18.0.0",
"@vitejs/plugin-vue": "^3.1.2",
"@webcomponents/webcomponentsjs": "^2.7.0",
Expand All @@ -89,7 +89,8 @@
"unocss": "^0.29.2",
"vite": "^4.2.1",
"vitepress": "1.0.0-alpha.61",
"vitest": "^0.13.1"
"vitest": "^0.13.1",
"xx": "^0.0.3"
},
"simple-git-hooks": {
"pre-commit": "pnpm exec lint-staged --concurrent false",
Expand Down
3 changes: 2 additions & 1 deletion playground/src/pages/checkbox.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ registerCheckbox()
const checked = ref(false)
const handler = () => {
console.log('22')
checked.value = !checked.value
}
</script>
Expand All @@ -13,7 +14,7 @@ const handler = () => {
<adw-checkbox :checked="checked" @click-handler="handler">
checkbox
</adw-checkbox>
<adw-checkbox :checked="checked" disabled>
<adw-checkbox :checked="checked" disabled @click-handler="handler">
disabled
</adw-checkbox>
</div>
Expand Down
14 changes: 10 additions & 4 deletions pnpm-lock.yaml

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

8 changes: 8 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export class BaseWebComponent extends HTMLElement {
this.render()
}

isTrueProp(prop: string, initial = false) {
if (!Reflect.has(this.props, prop))
return initial

const value = this.props[prop]
return value === '' || value !== 'false'
}

initialProps() {
const mutationObserver = new MutationObserver(() => this.setProps())
mutationObserver.observe(this, {
Expand Down
3 changes: 2 additions & 1 deletion src/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ export class AdwButton extends BaseWebComponent {
const btnClass = this.lintClass(
`${this.name} ${this.name}-${type} ${className}`,
)
this.registerEvent('click-handler', btnClass)
if (!this.isTrueProp('disabled'))
this.registerEvent('click-handler', btnClass)

return `<button class="${btnClass}" ${attributes}><slot></slot></button>`
}
Expand Down
7 changes: 3 additions & 4 deletions src/card.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseWebComponent } from '@simon_he/base-webcomponent'
import { getAttributes } from '../utils'
import { BaseWebComponent } from './base'
// import { BaseWebComponent } from '@simon_he/base-webcomponent'
// import { BaseWebComponent } from './base'

export class AdwCard extends BaseWebComponent {
name = 'adw-card'
Expand Down Expand Up @@ -129,8 +129,7 @@ export class AdwCard extends BaseWebComponent {
`${this.name} ${className} ${cover ? `${this.name}-hover` : ''}`,
)
let head

if (cover) {
if (this.isTrueProp('cover')) {
head = `<div class="${this.name}-cover">${cover}</div>`
}
else {
Expand Down
6 changes: 4 additions & 2 deletions src/checkbox.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseWebComponent } from '@simon_he/base-webcomponent'
// import { BaseWebComponent } from './base'
import { getAttributes } from '../utils'

export class AdwCheckbox extends BaseWebComponent {
Expand Down Expand Up @@ -68,10 +69,11 @@ export class AdwCheckbox extends BaseWebComponent {
}

template(): string {
const { className = '', disabled } = this.props
const { className = '' } = this.props
const attributes = getAttributes(this.props)
const checkboxClass = this.lintClass(`${this.name} ${className}`)
if (disabled === undefined || disabled === 'false')
console.log(this.props, this.isTrueProp('disabled'))
if (!this.isTrueProp('disabled'))
this.registerEvent('click-handler', `${this.name}-wrapper`)
return `
<div class="${this.name}-wrapper">
Expand Down
4 changes: 2 additions & 2 deletions src/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class AdwSwitch extends BaseWebComponent {
}

template(): string {
const { className = '', checked = 'false', disabled = false } = this.props
const { className = '', checked = 'false' } = this.props

const attributes = getAttributes(this.props)

Expand All @@ -98,7 +98,7 @@ export class AdwSwitch extends BaseWebComponent {
checked === 'true' ? `${this.name}-checked` : `${this.name}-unchecked`
}`,
)
if (disabled === false)
if (!this.isTrueProp('disabled'))
this.registerEvent('on-change', this.name)

return `<button type="button" role="switch" aria-checked="${checked}" class="${switchClass}" ${attributes}>
Expand Down

0 comments on commit 0a46ad2

Please sign in to comment.