Skip to content

Commit

Permalink
feat(tcr): add component Form
Browse files Browse the repository at this point in the history
  • Loading branch information
Manjiz committed Jun 25, 2018
1 parent b3bfb43 commit 56317c0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/taro-components-rn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cp -rf src AwesomeProjectDir/src/tcr
- 表单内容
- [x] button | PS
- [x] checkbox | FS
- [ ] form
- [x] form | PS(without RESET)
- [x] input | PS
- [x] label | PS(without FOR)
- [x] picker | PS
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-components-rn/src/components/Button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* - app-parameter
* - binderror
* - bindopensetting
* ✔ formType
* ✔ onClick
*
* Loading icon (svg): data:image/svg+xml;charset=utf8, <svg xmlns='http://www.w3.org/2000/svg' width='120' height='120' viewBox='0 0 100 100'><path fill='none' d='M0 0h100v100H0z'/><rect width='7' height='20' x='46.5' y='40' fill='%23E9E9E9' rx='5' ry='5' transform='translate(0 -30)'/><rect width='7' height='20' x='46.5' y='40' fill='%23989697' rx='5' ry='5' transform='rotate(30 105.98 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%239B999A' rx='5' ry='5' transform='rotate(60 75.98 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23A3A1A2' rx='5' ry='5' transform='rotate(90 65 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23ABA9AA' rx='5' ry='5' transform='rotate(120 58.66 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23B2B2B2' rx='5' ry='5' transform='rotate(150 54.02 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23BAB8B9' rx='5' ry='5' transform='rotate(180 50 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23C2C0C1' rx='5' ry='5' transform='rotate(-150 45.98 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23CBCBCB' rx='5' ry='5' transform='rotate(-120 41.34 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23D2D2D2' rx='5' ry='5' transform='rotate(-90 35 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23DADADA' rx='5' ry='5' transform='rotate(-60 24.02 65)'/><rect width='7' height='20' x='46.5' y='40' fill='%23E2E2E2' rx='5' ry='5' transform='rotate(-30 -5.98 65)'/></svg>
Expand Down Expand Up @@ -51,6 +52,7 @@ type Props = {
plain?: boolean,
disabled?: boolean,
loading?: boolean,
formType?: 'submit' | 'reset',
onClick?: Function,
}
type State = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import {
type Props = {
children?: React.Node,
style?: StyleSheet.Styles,
onChange?: Function
onChange?: Function,
_onGroupDataInitial?: Function,
}

class _CheckboxGroup extends React.Component<Props> {
props: Props
values: Array<{ value: any, checked: boolean }> = []
tmpIndex: number = 0

getDataFromValues = () => {
return this.values
.filter((item) => item.checked)
.map((item) => item.value)
}

toggleChange = (e: { value: *, checked: boolean }, index: number) => {
const { onChange } = this.props
this.values[index] = {
Expand All @@ -31,9 +38,7 @@ class _CheckboxGroup extends React.Component<Props> {
}
onChange && onChange({
detail: {
value: this.values
.filter((item) => item.checked)
.map((item) => item.value)
value: this.getDataFromValues()
}
})
}
Expand Down Expand Up @@ -64,10 +69,12 @@ class _CheckboxGroup extends React.Component<Props> {
const {
children,
style,
_onGroupDataInitial,
} = this.props

this.tmpIndex = 0
const mapChildren = this.findAndAttachCb(children)
_onGroupDataInitial && _onGroupDataInitial(this.getDataFromValues())

return (
<View style={style}>
Expand Down
113 changes: 113 additions & 0 deletions packages/taro-components-rn/src/components/Form/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* ✘ report-submit
* ✔ onSubmit(bindsubmit): no FormId info.
* ✘ onReset(bindreset)
*
* @flow
*/

import * as React from 'react'
import {
StyleSheet,
View
} from 'react-native'

type Props = {
children: ?React.Node,
style?: StyleSheet.Styles,
onSubmit?: Function,
onReset?: Function
}
type State = {
}

function isFormTypeElement (typeName) {
return [
'_Input',
'_Textarea',
'_CheckboxGroup',
'_RadioGroup',
'_Switch',
'_Slider',
'_Picker'
].indexOf(typeName) >= 0
}

class _Form extends React.Component<Props, State> {
props: Props
state: State = {
}
formValues: Object = {}

bindValueChangeEvent = (child) => {
// onChange: _CheckboxGroup _RadioGroup _Switch _Slider _Picker
// onBlur: _Input _Textarea
const childTypeName = child.type.name
const childPropsName = child.props.name
const valueChangeCbName = childTypeName === '_Input' || childTypeName === '_Textarea' ? 'onBlur' : 'onChange'
const tmpProps = { ...child.props }
// Initial value
if (['_Input', '_Textarea', '_Slider', '_Picker'].indexOf(childTypeName) >= 0) {
this.formValues[childPropsName] = child.props.value
} else if (childTypeName === '_Switch') {
this.formValues[childPropsName] = !!child.props.checked
} else {
tmpProps._onGroupDataInitial = (value) => {
this.formValues[childPropsName] = value
}
}
tmpProps[valueChangeCbName] = (event) => {
const cb = child.props[valueChangeCbName]
this.formValues[childPropsName] = event.detail.value
cb && cb(...arguments)
}
return React.cloneElement(child, tmpProps, child.props.children)
}

deppDiveIntoChildren = (children) => {
return React.Children.toArray(children).map((child) => {
const childTypeName = child.type.name
if (!child.type) return child
if (childTypeName === '_Button' && ['submit', 'reset'].indexOf(child.props.formType) >= 0) {
const onClick = child.props.onClick
return React.cloneElement(child, {
...child.props,
onClick: () => {
this[child.props.formType]()
onClick && onClick()
}
})
}
return isFormTypeElement(childTypeName) && child.props.name ?
this.bindValueChangeEvent(child) :
React.cloneElement(child, { ...child.props }, this.deppDiveIntoChildren(child.props.children))
})
}

submit = () => {
const { onSubmit } = this.props
onSubmit && onSubmit({
detail: {
value: this.formValues
}
})
}

reset = () => {
}

render () {
const {
children,
style,
} = this.props

return (
<View style={style}>
{this.deppDiveIntoChildren(children)}
</View>
)
}
}

export default _Form
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {
children?: React.Node,
style?: StyleSheet.Styles,
onChange?: Function,
_onGroupDataInitial?: Function,
}
type State = {
checkedValue: ?string
Expand Down Expand Up @@ -46,6 +47,9 @@ class _RadioGroup extends React.Component<Props, State> {
const childTypeName = child.type.name
if (childTypeName === '_Radio') {
const { checkedValue } = this.state
if (!checkedValue && child.props.checked) {
this.props._onGroupDataInitial && this.props._onGroupDataInitial(child.props.value)
}
return React.cloneElement(child, {
checked: checkedValue === child.props.value,
onChange: this.onValueChange.bind(this, child.props.onChange)
Expand Down
2 changes: 2 additions & 0 deletions packages/taro-components-rn/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Progress from './components/Progress'
import Button from './components/Button'
import Checkbox from './components/Checkbox'
import CheckboxGroup from './components/Checkbox/checkbox-group'
import Form from './components/Form'
import Input from './components/Input'
import Label from './components/Label'
import Picker from './components/Picker'
Expand All @@ -29,6 +30,7 @@ export {
Button,
Checkbox,
CheckboxGroup,
Form,
Input,
Label,
Picker,
Expand Down

0 comments on commit 56317c0

Please sign in to comment.