ViewVariableList.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React from 'react'
  2. import { List, Icon, Tooltip, Popconfirm, Tag } from 'antd'
  3. import { IViewVariable } from 'containers/View/types'
  4. import { ViewVariableTypes } from '../constants'
  5. import Styles from '../View.less'
  6. export interface IViewVariableListProps {
  7. variables: IViewVariable[]
  8. className?: string
  9. onAdd?: () => void
  10. onDelete?: (key: string) => void
  11. onEdit?: (variable: IViewVariable) => void
  12. }
  13. export class ViewVariableList extends React.PureComponent<IViewVariableListProps> {
  14. private editItem = (variable: IViewVariable) => () => {
  15. this.props.onEdit({ ...variable })
  16. }
  17. private deleteItem = (key: string) => () => {
  18. this.props.onDelete(key)
  19. }
  20. private renderItem = (item: IViewVariable) => {
  21. const icons = [
  22. (
  23. <Tooltip key="edit" title="修改">
  24. <Icon onClick={this.editItem(item)} type="edit" />
  25. </Tooltip>
  26. ),
  27. (
  28. <Popconfirm
  29. key="delete"
  30. title="确定删除?"
  31. placement="left"
  32. onConfirm={this.deleteItem(item.key)}
  33. >
  34. <Tooltip title="删除">
  35. <Icon type="delete" />
  36. </Tooltip>
  37. </Popconfirm>
  38. )
  39. ]
  40. const { name, alias, type } = item
  41. const text = alias ? `${name}[${alias}]` : `${name}`
  42. const color = type === ViewVariableTypes.Query ? 'green' : 'volcano'
  43. const category = type === ViewVariableTypes.Query ? 'QUERY' : 'AUTH'
  44. return (
  45. <List.Item actions={icons}>
  46. <Tag color={color}>{category}</Tag>
  47. <div className={Styles.variableItem}>{text}</div>
  48. </List.Item>
  49. )
  50. }
  51. public render () {
  52. const { variables, className, onAdd } = this.props
  53. return (
  54. <List
  55. className={className}
  56. size="small"
  57. header={<div className={Styles.viewVariableHeader}><h4>变量</h4><Icon type="plus" onClick={onAdd} title="添加" /></div>}
  58. locale={{ emptyText: '暂无变量' }}
  59. dataSource={variables}
  60. renderItem={this.renderItem}
  61. />
  62. )
  63. }
  64. }
  65. export default ViewVariableList