uvue.uts 982 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { type ComponentPublicInstance } from 'vue';
  2. type SelectOptions = {
  3. context : ComponentPublicInstance,
  4. needAll : boolean | null,
  5. }
  6. export function selectAllComponent(selector : string, options : UTSJSONObject) : ComponentPublicInstance[]|null {
  7. const context = options.get('context')! as ComponentPublicInstance;
  8. let needAll = options.get('needAll') as boolean;
  9. let result:ComponentPublicInstance[] = []
  10. if(needAll == null) { needAll = true };
  11. if(context.$children.length > 0) {
  12. const queue:ComponentPublicInstance[] = [...context.$children];
  13. while(queue.length > 0) {
  14. const child = queue.shift();
  15. const name = child?.$options?.name;
  16. if(name == selector) {
  17. result.push(child as ComponentPublicInstance)
  18. } else {
  19. const children = child?.$children
  20. if(children !== null) {
  21. queue.push(...children)
  22. }
  23. }
  24. if(result.length > 0 && !needAll) {
  25. break;
  26. }
  27. }
  28. }
  29. if(result.length > 0) {
  30. return result
  31. }
  32. return null
  33. }