knowledge.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import MultiSelect from "@/components/bs-ui/select/multi";
  2. import { readFileLibDatabase } from "@/controllers/API";
  3. import { useEffect, useRef, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. export default function KnowledgeSelect({ multiple = false, value, disabled = false, onChange, children }:
  6. { multiple?: boolean, value: any, disabled?: boolean, onChange: (a: any) => any, children?: (fun: any) => React.ReactNode }) {
  7. const { t } = useTranslation()
  8. const [options, setOptions] = useState<any>([]);
  9. const originOptionsRef = useRef([])
  10. const pageRef = useRef(1)
  11. const reload = (page, name) => {
  12. readFileLibDatabase(page, 60, name).then(res => {
  13. pageRef.current = page
  14. originOptionsRef.current = res.data
  15. const opts = res.data.map(el => ({ label: el.name, value: el.id }))
  16. setOptions(_ops => page > 1 ? [..._ops, ...opts] : opts)
  17. })
  18. }
  19. useEffect(() => {
  20. reload(1, '')
  21. }, [])
  22. // const handleChange = (res) => {
  23. // // id => obj
  24. // onChange(res.map(el => originOptionsRef.current.find(el2 => el2.id === el)))
  25. // }
  26. // 加载更多
  27. const loadMore = (name) => {
  28. reload(pageRef.current + 1, name)
  29. }
  30. return <MultiSelect
  31. multiple={multiple}
  32. value={value}
  33. disabled={disabled}
  34. options={options}
  35. placeholder={t('build.selectKnowledgeBase')}
  36. searchPlaceholder={t('build.searchBaseName')}
  37. onChange={onChange}
  38. onLoad={() => reload(1, '')}
  39. onSearch={(val) => reload(1, val)}
  40. onScrollLoad={(val) => loadMore(val)}
  41. >
  42. {children?.(reload)}
  43. </MultiSelect>
  44. };