PointsItem.jsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import React, { Component } from 'react'
  2. import './pointsItem.css'
  3. import { Icon, Modal } from 'semantic-ui-react'
  4. import { DatePicker, Space } from 'antd'
  5. import locale from 'antd/lib/date-picker/locale/zh_CN'
  6. import { isObjEmpty } from '../../utils/common'
  7. import ApprovalBean from '../../model/ApprovalBean'
  8. import moment from 'moment'
  9. import 'moment/locale/zh-cn'
  10. export default class PointsItem extends Component {
  11. componentDidMount () {
  12. let { mApproval } = this.state
  13. this.setState({
  14. mApproval: this.props.approval,
  15. approvalAble: this.props.approvalAble,
  16. })
  17. let options = []
  18. let datas = mApproval.datas
  19. if (!isObjEmpty(datas)) {
  20. for (let i = 0; i < datas.length; i++) {
  21. options.push(datas[i].display)
  22. }
  23. }
  24. this.setState({
  25. selectList: options,
  26. inputValue: mApproval.values,
  27. })
  28. }
  29. componentWillReceiveProps () {
  30. this.componentDidMount()
  31. }
  32. constructor () {
  33. super()
  34. this.state = {
  35. modalOpen: false,
  36. selectList: [],
  37. inputValue: '',
  38. mApproval: new ApprovalBean(),
  39. approvalAble: true,
  40. }
  41. }
  42. render () {
  43. let valueItem = ''
  44. const dateFormat = 'YYYY-MM-DD'
  45. const { inputValue, selectList, mApproval, approvalAble } = this.state
  46. if (approvalAble == false) {
  47. valueItem = <div className='value-input'>{mApproval.values}</div>
  48. } else if (mApproval.isSelect()) {
  49. let placeHolder = ''
  50. if (mApproval.mustInput) {
  51. placeHolder = '请选择(必选)'
  52. } else {
  53. placeHolder = '请选择(非必选)'
  54. }
  55. if (mApproval.inputType() == 2) {
  56. //日期选择框
  57. let defaultDate = inputValue
  58. if (!isObjEmpty(inputValue)) {
  59. defaultDate = ''
  60. } else {
  61. defaultDate = moment(defaultDate, 'YYYY-MM-DD')
  62. if (!defaultDate._isValid) {
  63. defaultDate = ''
  64. }
  65. }
  66. valueItem = <div style={{ display: 'flex' }}>
  67. <DatePicker locale={locale} className='date-input'
  68. defaultValue={defaultDate}
  69. format={dateFormat} size='small'
  70. placeholder={placeHolder}
  71. onChange={this.onDatePicker}
  72. suffixIcon=' '/>
  73. <Icon name='angle right' style={{ margin: '0px', padding: '0px' }}/>
  74. </div>
  75. } else {
  76. //弹框选择
  77. const { modalOpen } = this.state
  78. let modalItems = []
  79. for (let i = 0; i < selectList.length; i++) {
  80. modalItems.push(<div className='selectItem' onClick={
  81. this.modalSelect.bind(this, i)
  82. }>{selectList[i]}</div>)
  83. }
  84. valueItem = <Modal trigger={<div style={{ display: 'flex' }}
  85. onClick={this.onSelectClick}>
  86. <input placeholder={placeHolder} readOnly unselectable='on'
  87. className='value-input'
  88. value={inputValue}/>
  89. <Icon name='angle right' style={{ margin: '0px', padding: '0px' }}/>
  90. </div>}
  91. open={modalOpen}
  92. onClose={this.modalClose}
  93. size='small'>
  94. <Modal.Content image>
  95. <Modal.Description>
  96. {modalItems}
  97. </Modal.Description>
  98. </Modal.Content>
  99. </Modal>
  100. }
  101. } else {
  102. let placeHolder = ''
  103. if (mApproval.mustInput) {
  104. placeHolder = '请输入(必填)'
  105. } else {
  106. placeHolder = '请输入(非必填)'
  107. }
  108. if (mApproval.dfType == 'N') {
  109. //数字输入框
  110. valueItem = <div style={{ display: 'flex' }}>
  111. <input type='number' onKeyPress={this.numKeyPress}
  112. placeholder={placeHolder} className='value-input'
  113. value={inputValue} onChange={this.inputChange}/>
  114. <Icon name='angle right' style={{
  115. margin: '0px',
  116. padding: '0px',
  117. visibility: 'hidden',
  118. }}/>
  119. </div>
  120. } else {
  121. //文本输入框
  122. valueItem = <div style={{ display: 'flex' }}>
  123. <input placeholder={placeHolder} className='value-input'
  124. value={inputValue} onChange={this.inputChange}/>
  125. <Icon name='angle right' style={{
  126. margin: '0px',
  127. padding: '0px',
  128. visibility: 'hidden',
  129. }}/>
  130. </div>
  131. }
  132. }
  133. /*switch (this.props.type) {
  134. case '1':
  135. //输入框
  136. valueItem = <div style={{display: 'flex'}}>
  137. <input placeholder='请输入' className='value-input'
  138. value={inputValue} onChange={this.inputChange}/>
  139. <Icon name='angle right' style={{margin: '0px', padding: '0px', visibility: 'hidden'}}/>
  140. </div>
  141. break
  142. case '2':
  143. //日期选择框
  144. valueItem = <div style={{display: 'flex'}}>
  145. <DatePicker locale={locale} className='date-input'
  146. defaultValue={inputValue}
  147. format={dateFormat} size='small'
  148. onChange={this.onDatePicker}
  149. suffixIcon=' '/>
  150. <Icon name='angle right' style={{margin: '0px', padding: '0px'}}/>
  151. </div>
  152. break
  153. case '3':
  154. //弹框选择
  155. const {modalOpen, selectList} = this.state
  156. let modalItems = []
  157. for (let i = 0; i < selectList.length; i++) {
  158. modalItems.push(<div className='selectItem' onClick={
  159. this.modalSelect.bind(this, i)
  160. }>{selectList[i]}</div>)
  161. }
  162. valueItem = <Modal trigger={<div style={{display: 'flex'}} onClick={this.onSelectClick}>
  163. <input placeholder='请选择' readOnly unselectable='on'
  164. className='value-input'
  165. value={inputValue}/>
  166. <Icon name='angle right' style={{margin: '0px', padding: '0px'}}/>
  167. </div>}
  168. open={modalOpen}
  169. onClose={this.modalClose}
  170. size='small'>
  171. <Modal.Content image>
  172. <Modal.Description>
  173. {modalItems}
  174. </Modal.Description>
  175. </Modal.Content>
  176. </Modal>
  177. break
  178. default:
  179. valueItem = <div></div>
  180. break
  181. }*/
  182. return <div style={{ padding: '4px' }}>
  183. <div className='points-parent'>
  184. <div className='points-caption'>{mApproval.caption}</div>
  185. <div className='points-value'>
  186. {valueItem}
  187. </div>
  188. </div>
  189. </div>
  190. }
  191. numKeyPress = (event) => {
  192. const invalidChars = ['-', '+', 'e', '.', 'E']
  193. if (invalidChars.indexOf(event.key) !== -1) {
  194. event.preventDefault()
  195. }
  196. }
  197. modalClose = () => {
  198. this.setState({
  199. modalOpen: false,
  200. })
  201. }
  202. modalSelect = (index) => {
  203. const { selectList } = this.state
  204. this.setState({
  205. inputValue: selectList[index],
  206. modalOpen: false,
  207. }, () => {
  208. this.props.valueListener(this.props.approval.type, this.props.index,
  209. this.state.inputValue, false)
  210. })
  211. }
  212. inputChange = (e) => {
  213. this.setState({
  214. inputValue: e.target.value,
  215. }, () => {
  216. this.props.valueListener(this.props.approval.type, this.props.index,
  217. this.state.inputValue, false)
  218. })
  219. }
  220. onDatePicker = (e, dateString) => {
  221. this.setState({
  222. inputValue: dateString,
  223. }, () => {
  224. this.props.valueListener(this.props.approval.type, this.props.index,
  225. this.state.inputValue, false)
  226. })
  227. }
  228. onSelectClick = () => {
  229. this.setState({
  230. modalOpen: true,
  231. })
  232. }
  233. }