Skip to content

Commit

Permalink
feat: 新增 FormIdCollector 组件
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 11, 2019
1 parent 2840922 commit 0b6b786
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mounted",
"version": "0.1.0",
"description": "基于 Taro 的微信小程序组件库。",
"main": "src/index.js",
"main": "src/components/index.ts",
"scripts": {
"dev": "taro build --type weapp --watch",
"release": "standard-version -a && git push --follow-tags origin master"
Expand Down
9 changes: 5 additions & 4 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import Home from './pages/Home/Home'
class App extends Taro.Component {
config: Taro.Config = {
pages: [
// 'pages/Home/Home',
// 'pages/Sticky/Sticky',
'pages/Transition/X',
'pages/Home/Home',
'pages/Sticky/Sticky',
// 'pages/Transition/X',
// 'pages/Popup/Popup',
// 'pages/Picker/PickerView',
// 'pages/Picker/Picker',
'pages/Picker/SinglePicker',
// 'pages/Picker/SinglePicker',
// 'pages/Test/Index',
],
window: {
navigationBarTitleText: 'DEMO',
Expand Down
7 changes: 7 additions & 0 deletions src/components/FormIdCollector/Collector.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.form {
display: none;
width: 0;
height: 0;
overflow: hidden;
pointer-events: none;
}
38 changes: 38 additions & 0 deletions src/components/FormIdCollector/Collector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Form, Button } from '@tarojs/components'
import { noop } from 'vtils'
import { component, RequiredProp } from '../component'
import _ from './Collector.module.scss'
import Self from './Collector'

export default class Collector extends component({
props: {
count: 0 as any as RequiredProp<number>,
onCollect: noop as any as RequiredProp<
(e: { detail: { formId: string } }) => void
>,
},
}) {
public render() {
const { count } = this.props
return (
<Form
className={_.form}
reportSubmit={true}
onSubmit={this.props.onCollect}>
<Button formType='submit'>
{
count > 1
? (
<Self
count={count - 1}
onCollect={this.props.onCollect}>
{this.props.children}
</Self>
)
: this.props.children
}
</Button>
</Form>
)
}
}
45 changes: 45 additions & 0 deletions src/components/FormIdCollector/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Label, View, Button } from '@tarojs/components'
import { noop } from 'vtils'
import { component } from '../component'
import Collector from './Collector'

/**
* Form Id 收集组件。
*/
export default class MFormIdCollector extends component({
props: {
/** 每次点击的收集个数 */
count: 1 as number,
/** 是否禁用 */
disabled: false as boolean,
/** 收集完成事件 */
onCollect: noop as (formIds: string[]) => void,
},
}) {
/** Form Id 列表 */
formIds: string[] = []

handleSubmit: Collector['props']['onCollect'] = e => {
this.formIds.push(e.detail.formId)
if (this.formIds.length === this.props.count) {
this.props.onCollect(this.formIds.slice())
this.formIds = []
}
}

public render() {
const { count, disabled } = this.props
return disabled ? this.props.children : (
<View>
<Label for='button'>
{this.props.children}
</Label>
<Collector
count={count}
onCollect={this.handleSubmit}>
<Button id='button' />
</Collector>
</View>
)
}
}
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @index(^[A-Z]): export { default as M${variable/Block$//:pascal:} } from ${relpath}
export { default as MFormIdCollector } from './FormIdCollector'
export { default as MPicker } from './Picker'
export { default as MPickerView } from './PickerView'
export { default as MPopup } from './Popup'
Expand Down
10 changes: 10 additions & 0 deletions src/pages/Test/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { component } from '../../components/component'
import Test from './Test'

export default class extends component() {
render() {
return (
<Test n={1} />
)
}
}
18 changes: 18 additions & 0 deletions src/pages/Test/Test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import Self from './Test'

export default class Test extends Component<{ n: number }> {
render() {
const { n } = this.props
return (
<View>
{
n === 1
? <Self n={0} />
: 'over'
}
</View>
)
}
}

0 comments on commit 0b6b786

Please sign in to comment.