select.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { SceneType, QuoteSceneStatus } from 'constant';
  2. import { Table } from 'components'
  3. import { Modal, Input } from 'antd'
  4. import { ScenePage, sceneTitleColumn, sceneTimeColumn, modelSceneTimeColumn, modelSceneRawTypeColumn } from 'views/scene'
  5. import { fetchScenes, filterScenesSelector, useSelector, getSceneIdent } from 'store'
  6. import { useThunkPaging, useRefershFoce } from 'hook'
  7. import style from '../style.module.scss'
  8. import type { Scene, SceneIdents } from "api";
  9. export type SelectScenesProps = {
  10. sceneIdents: SceneIdents
  11. onClose: () => void,
  12. onSelect: (ids: SelectScenesProps['sceneIdents']) => void
  13. }
  14. export const SelectScenes = ({ sceneIdents, ...props }: SelectScenesProps) => {
  15. let idents: SceneIdents = sceneIdents.map(ident => ({
  16. ...ident,
  17. numList: [...ident.numList]
  18. }))
  19. const getTypeIdents = (type: SceneType) => idents.find(ident => ident.type === type) as SceneIdents[number]
  20. const getSelectIds = (type: SceneType, scenes: Scene[]) => {
  21. const typeIdents = getTypeIdents(type)
  22. const selectedIds = []
  23. for (const scene of scenes) {
  24. const sceneIdent = getSceneIdent(scene)
  25. if (typeIdents.numList.includes(sceneIdent)) {
  26. selectedIds.push(scene.id)
  27. }
  28. }
  29. return selectedIds
  30. }
  31. const replaceIdents = (type: SceneType, scenes: Scene[], selectScenes: Scene[]) => {
  32. const typeIdents = getTypeIdents(type)
  33. for (const scene of scenes) {
  34. const sceneIdent = getSceneIdent(scene)
  35. const inSelect = selectScenes.includes(scene)
  36. const identIndex = typeIdents?.numList.indexOf(sceneIdent)
  37. if (~identIndex && !inSelect) {
  38. typeIdents.numList.splice(identIndex, 1)
  39. } else if (!~identIndex && inSelect) {
  40. typeIdents.numList.push(sceneIdent)
  41. }
  42. }
  43. console.log(typeIdents)
  44. }
  45. const Content = ({ type }: { type: SceneType }) => {
  46. const scenes = useSelector((state) => filterScenesSelector(state, { type }))
  47. const states = useThunkPaging({ type, sceneName: '', status: QuoteSceneStatus.SUCCESS }, fetchScenes)
  48. const [[paging, setPaging], [params, setParams]] = states
  49. const selectedIds = getSelectIds(type, scenes)
  50. const refersh = useRefershFoce()
  51. console.log(selectedIds)
  52. const rowSelection: any = {
  53. selectedRowKeys: selectedIds,
  54. onChange(ids: string, selectScenes: Scene[]) {
  55. console.log(ids, selectScenes)
  56. replaceIdents(type, scenes, selectScenes)
  57. refersh()
  58. }
  59. };
  60. const columns = type === SceneType.SWMX
  61. ? [modelSceneRawTypeColumn, modelSceneTimeColumn]
  62. : [sceneTimeColumn]
  63. return (
  64. <div >
  65. <div className={style['model-header']}>
  66. <Input.Search
  67. allowClear
  68. className='content-header-search'
  69. placeholder="输入名称搜索"
  70. onSearch={sceneName => setParams({ sceneName })}
  71. style={{ width: 264, marginTop: '15px' }}
  72. />
  73. </div>
  74. <div className={style["scene-table-layout"]}>
  75. <Table
  76. columns={[sceneTitleColumn, ...columns] as any}
  77. rowSelection={rowSelection}
  78. data={scenes}
  79. paging={paging}
  80. unDataMsg={params.sceneName ? '未搜索到结果' : '暂无数据'}
  81. onChangePaging={setPaging}
  82. />
  83. </div>
  84. </div>
  85. )
  86. }
  87. return (
  88. <Modal
  89. width="800px"
  90. title="添加场景"
  91. visible={true}
  92. onOk={() => props.onSelect(idents)}
  93. onCancel={props.onClose}
  94. okText="确定"
  95. cancelText="取消"
  96. >
  97. <ScenePage TabContent={Content} />
  98. </Modal>
  99. )
  100. }
  101. export default SelectScenes