field-picker.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // module/user/components/field-picker/field-picker.ts
  2. Component({
  3. behaviors: ['wx://form-field'],
  4. lifetimes: {},
  5. properties: {
  6. title: { type: String, value: '' },
  7. options: { type: Array, value: [] },
  8. optionsColumns: { type: Number, value: 3 },
  9. itemHeight: { type: Number, value: 64 },
  10. multiple: { type: Boolean, value: true },
  11. closeOnOverlayClick: { type: Boolean, value: true },
  12. },
  13. data: {
  14. visible: false,
  15. value: [],
  16. selected: [] as Option[],
  17. },
  18. observers: {
  19. 'value, options'(values: Option[] | Option | string, options: Option[]) {
  20. const selected = new Set<Option>();
  21. if (options.length) {
  22. const _values = Array.isArray(values) ? values : values ? [values] : [];
  23. for (const item of _values) {
  24. const value = typeof item === 'object' ? item?.value : item;
  25. const [v,l] = value.split(':');
  26. const option = options.find(item => item.value === v)
  27. if (option) selected.add({...option, label: l ?? option.label});
  28. }
  29. }
  30. this.setData({ selected: [...selected] });
  31. },
  32. },
  33. methods: {
  34. onShow() {
  35. this.setData({ visible: true });
  36. },
  37. onPickerConfirm(event: any) {
  38. const { options } = event.detail;
  39. console.log(event.detail, '123-->');
  40. this.setData({ value: options })
  41. },
  42. onPickerClose() {
  43. this.setData({ visible: false })
  44. }
  45. }
  46. })