select.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. export function getSelector(options = {}) {
  2. let selector = '';
  3. if (options.name) {
  4. selector = `[name="${options.name}"]`;
  5. }
  6. const field = `Select${options.control ? 'Control' : 'Field'}`;
  7. return `${field}${selector}`;
  8. }
  9. export function openMenu(wrapper, options = {}) {
  10. const selector = getSelector(options);
  11. wrapper
  12. .find(`${selector} input[role="combobox"]`)
  13. .at(options.at || 0)
  14. .simulate('focus');
  15. wrapper
  16. .find(`${selector} .Select-control`)
  17. .at(options.at || 0)
  18. .simulate('mouseDown', {button: 0});
  19. return wrapper;
  20. }
  21. export function clearValue(wrapper) {
  22. wrapper.find('.Select-clear-zone').simulate('mouseDown', {button: 0});
  23. }
  24. export function findOption(wrapper, {value, label} = {}, options) {
  25. const selector = getSelector(options);
  26. const valueSelector = !!value ? 'value' : 'label';
  27. return wrapper
  28. .find(`${selector} Option`)
  29. .findWhere(
  30. el => el.prop('option') && el.prop('option')[valueSelector] === (value || label)
  31. );
  32. }
  33. export function selectByLabel(wrapper, label, options = {}) {
  34. openMenu(wrapper, options);
  35. findOption(wrapper, {label}, options)
  36. .at(options.at || 0)
  37. .simulate('mouseDown');
  38. }
  39. export function selectByValue(wrapper, value, options = {}) {
  40. openMenu(wrapper, options);
  41. findOption(wrapper, {value}, options)
  42. .at(options.at || 0)
  43. .simulate('mouseDown');
  44. }