ListItem.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { PureComponent, createRef, RefObject, MouseEvent } from 'react'
  21. import classnames from 'classnames'
  22. import { Icon, Input, message } from 'antd'
  23. import styles from './ListFormLayout.less'
  24. interface IListItemProps {
  25. id: string
  26. name: string
  27. className?: string
  28. onClick?: (key: string) => void
  29. onChange: (key: string, name: string) => void
  30. onDelete: (key: string) => void
  31. }
  32. interface IListItemStates {
  33. editing: boolean
  34. inputValue: string
  35. }
  36. class ListItem extends PureComponent<IListItemProps, IListItemStates> {
  37. public state: IListItemStates = {
  38. editing: false,
  39. inputValue: this.props.name
  40. }
  41. private container: RefObject<HTMLDivElement> = createRef()
  42. private input: RefObject<Input> = createRef()
  43. private inputValueChange = (e) => {
  44. this.setState({
  45. inputValue: e.target.value
  46. })
  47. }
  48. private editStart = () => {
  49. this.setState(
  50. {
  51. editing: true
  52. },
  53. () => {
  54. this.input.current.input.select()
  55. window.addEventListener('click', this.editFallback, false)
  56. window.addEventListener('keydown', this.enterToFinish, false)
  57. }
  58. )
  59. }
  60. private editFallback = (e) => {
  61. if (!this.container.current.contains(e.target)) {
  62. this.setState({
  63. editing: false,
  64. inputValue: this.props.name
  65. })
  66. window.removeEventListener('click', this.editFallback, false)
  67. window.removeEventListener('keydown', this.enterToFinish, false)
  68. }
  69. }
  70. private editFinish = () => {
  71. const { id } = this.props
  72. const { inputValue } = this.state
  73. if (inputValue) {
  74. this.props.onChange(id, inputValue)
  75. this.setState({
  76. editing: false
  77. })
  78. window.removeEventListener('click', this.editFallback, false)
  79. window.removeEventListener('keydown', this.enterToFinish, false)
  80. } else {
  81. message.error('名称不能为空')
  82. this.input.current.input.focus()
  83. }
  84. }
  85. private enterToFinish = (e: KeyboardEvent) => {
  86. if (e.keyCode === 13) {
  87. this.editFinish()
  88. }
  89. }
  90. private click = () => {
  91. const { id, onClick } = this.props
  92. if (onClick) {
  93. onClick(id)
  94. }
  95. }
  96. private delete = (e: MouseEvent) => {
  97. e.stopPropagation()
  98. const { id, onDelete } = this.props
  99. onDelete(id)
  100. }
  101. public render() {
  102. const { name, className, onClick } = this.props
  103. const { editing, inputValue } = this.state
  104. const contentClass = classnames({
  105. [styles.listItem]: true,
  106. [styles.editing]: editing,
  107. [className]: !!className
  108. })
  109. return (
  110. <div className={contentClass} ref={this.container}>
  111. <h4 onClick={this.click}>{name}</h4>
  112. <Input
  113. value={inputValue}
  114. placeholder="请输入名称"
  115. size="small"
  116. onChange={this.inputValueChange}
  117. ref={this.input}
  118. />
  119. <Icon
  120. className={styles.confirm}
  121. type="check"
  122. onClick={this.editFinish}
  123. />
  124. <Icon type="edit" className={styles.action} onClick={this.editStart} />
  125. <Icon type="delete" className={styles.action} onClick={this.delete} />
  126. </div>
  127. )
  128. }
  129. }
  130. export default ListItem