forgeplus-react/src/modules/test/md/TestMaterialDesign.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-03-16 14:12:11 +08:00
import React, { Component } from 'react';
import { IIHOC as DebuggerHOC, stringify } from './ii_debug'
// Props Proxy and state abstraction demonstration
function PPHOC(WrappedComponent) {
return class PP extends React.Component {
componentDidMount() {
// console.log('componentDidMount1 componentDidMount1 ')
}
constructor(props) {
super(props)
this.state = { fields: {} }
}
getField(fieldName) {
if (!this.state.fields[fieldName]) {
// TODO 从服务端取state对应的数据
// 共享state
this.state.fields[fieldName] = {
value: '',
onChange: event => {
this.state.fields[fieldName].value = event.target.value
this.forceUpdate()
}
}
}
return {
value: this.state.fields[fieldName].value,
onChange: this.state.fields[fieldName].onChange
}
}
render() {
const props = Object.assign({}, this.props, {
fields: this.getField.bind(this),
})
return (
<div>
<h2>
PP HOC
</h2>
<p>Im a Props Proxy HOC that abstracts controlled inputs</p>
2020-04-28 17:23:34 +08:00
<WrappedComponent {...props} />
2020-03-16 14:12:11 +08:00
</div>
)
}
}
}
class Example extends React.Component {
componentDidMount() {
console.log('componentDidMount componentDidMount ')
}
render() {
return (
<div>
<h2>
Wrapped Component
</h2>
<p>
Props
</p>
<pre>{stringify(this.props)}</pre>
<form>
<label>Automatically controlled input!</label>
2020-04-28 17:23:34 +08:00
<input type="email" {...this.props.fields('email')} />
2020-03-16 14:12:11 +08:00
</form>
</div>
)
}
}
const EnhancedExample = DebuggerHOC(PPHOC(Example))
export default EnhancedExample