Guest User

Untitled

a guest
Nov 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. class InputComponent extends React.Component {
  2.  
  3. state = {
  4. inputValue: this.props.value
  5. }
  6.  
  7. componentWillReceiveProps(nextProps) {
  8. if (nextProps.value !== this.props.value) {
  9. this.setState({ inputValue: nextProps.value })
  10. }
  11. }
  12.  
  13. handleChange = (e) => {
  14. this.setState({ inputValue: e.target.value}, () => {
  15. if (this.props.onChange){
  16. this.props.onChange(this.state.inputValue)
  17. }
  18. })
  19. }
  20.  
  21. render() {
  22. return <input
  23. value={this.state.inputValue}
  24. onChange={this.handleChange} />
  25. }
  26.  
  27. class RangeComponent extends React.Component {
  28.  
  29. state = {
  30. inputValueFrom: this.props.from,
  31. inputValueTo: this.props.to
  32. }
  33.  
  34. componentWillReceiveProps(nextProps) {
  35. this.setState({
  36. inputValueFrom: nextProps.from,
  37. inputValueTo: nextProps.to
  38. }, () => this.publish())
  39. }
  40.  
  41. handleUpdateFrom = (e) => {
  42. this.setState({ inputValueFrom: e }, () => this.publish())
  43. }
  44.  
  45. handleUpdateTo = (e) => {
  46. this.setState({ inputValueTo: e }, () => this.publish())
  47. }
  48.  
  49. publish(){
  50. if (this.props.onUpdated) {
  51. this.props.onUpdated({
  52. from: this.state.inputValueFrom,
  53. to: this.state.inputValueTo
  54. })
  55. }
  56. }
  57.  
  58. render() {
  59. return <div>
  60. <h6>From:</h6>
  61. <InputComponent value={this.state.inputValueFrom} onChange={this.handleUpdateFrom} />
  62. <h6>To:</h6>
  63. <InputComponent value={this.state.inputValueTo} onChange={this.handleUpdateTo} />
  64. </div>
  65. }
  66. }
  67.  
  68. <RangeComponent from={rangeFrom} to={rangeTo} onUpdated={this.handleRangeUpdate} />
  69.  
  70. handleRangeUpdate = (range) => {
  71. console.log(`Range updated ${range.from} - ${range.to}`)
  72. this.setState({ selectedRange: `Range updated ${range.from} - ${range.to}`})
  73. }
  74.  
  75. <button className='btn btn-block btn-primary' value='' onClick={this.handleResetFromButtonClick}>Reset Range From</button>
  76.  
  77. handleResetFromButtonClick = (e) => {
  78. this.setState({ rangeFrom: e.target.value })
  79. }
  80.  
  81. import React from 'react'
  82. import { render } from 'react-dom'
  83.  
  84. class InputComponent extends React.Component {
  85.  
  86. state = {
  87. inputValue: this.props.value
  88. }
  89.  
  90. componentWillReceiveProps(nextProps) {
  91. if (nextProps.value !== this.props.value) {
  92. this.setState({ inputValue: nextProps.value })
  93. }
  94. }
  95.  
  96. handleChange = (e) => {
  97. this.setState({ inputValue: e.target.value}, () => {
  98. if (this.props.onChange){
  99. this.props.onChange(this.state.inputValue)
  100. }
  101. })
  102. }
  103.  
  104. render() {
  105. return <input
  106. value={this.state.inputValue}
  107. onChange={this.handleChange} />
  108. }
  109. }
  110.  
  111. class RangeComponent extends React.Component {
  112.  
  113. state = {
  114. inputValueFrom: this.props.from,
  115. inputValueTo: this.props.to
  116. }
  117.  
  118. componentWillReceiveProps(nextProps) {
  119.  
  120. console.log('componentWillReceiveProps', nextProps)
  121.  
  122. this.setState({
  123. inputValueFrom: nextProps.from,
  124. inputValueTo: nextProps.to
  125. }, () => this.publish())
  126. }
  127.  
  128. handleUpdateFrom = (e) => {
  129. this.setState({ inputValueFrom: e }, () => this.publish())
  130. }
  131.  
  132. handleUpdateTo = (e) => {
  133. this.setState({ inputValueTo: e }, () => this.publish())
  134. }
  135.  
  136. publish(){
  137. if (this.props.onUpdated) {
  138. this.props.onUpdated({
  139. from: this.state.inputValueFrom,
  140. to: this.state.inputValueTo
  141. })
  142. }
  143. }
  144.  
  145. render() {
  146. return <div>
  147. <h6>From:</h6>
  148. <InputComponent value={this.state.inputValueFrom} onChange={this.handleUpdateFrom} />
  149. <h6>To:</h6>
  150. <InputComponent value={this.state.inputValueTo} onChange={this.handleUpdateTo} />
  151. </div>
  152. }
  153. }
  154.  
  155. class App extends React.Component {
  156.  
  157. state = {
  158. value: 'My Value',
  159. rangeFrom: 'Range From',
  160. rangeTo: 'Range To',
  161. selectedRange: null
  162. }
  163.  
  164. handleSingleChange = (e) => {
  165. this.setState({ value: e })
  166. }
  167.  
  168. handleRangeUpdate = (range) => {
  169. console.log(`Range updated ${range.from} - ${range.to}`)
  170. //this.setState({ selectedRange: `Range updated ${range.from} - ${range.to}`})
  171. }
  172.  
  173. handleResetFromButtonClick = (e) => {
  174. this.setState({ rangeFrom: e.target.value })
  175. }
  176.  
  177. handleResetToButtonClick = (e) => {
  178. this.setState({ rangeTo: e.target.value })
  179. }
  180.  
  181. render() {
  182.  
  183. const { value, selectedRange, rangeFrom, rangeTo } = this.state
  184.  
  185. console.log('rangeFrom', rangeFrom)
  186. console.log('rangeTo', rangeTo)
  187.  
  188. return (
  189. <div>
  190. <h3>Component: {value}</h3>
  191. <InputComponent value={value} onChange={this.handleSingleChange} />
  192. <hr />
  193. <h3>Range Component: {selectedRange}</h3>
  194. <RangeComponent from={rangeFrom} to={rangeTo} onUpdated={this.handleRangeUpdate} />
  195.  
  196. <hr />
  197. <button className='btn btn-block btn-primary' value='' onClick={this.handleResetFromButtonClick}>Reset Range From</button>
  198. <br />
  199. <button className='btn btn-block btn-primary' value='' onClick={this.handleResetToButtonClick}>Reset Range To</button>
  200. </div>
  201. )
  202. }
  203. }
  204.  
  205. render(<App />, document.getElementById("root"))
Add Comment
Please, Sign In to add comment