ai-form-field-checkbox.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <ie-form-picker :label="rule.label" :remark="rule.description">
  3. <uv-checkbox-group v-model="formModel[rule.fieldName]" @change="checkMutex($event),clearValidate()">
  4. <uv-checkbox v-for="item in columnsWithMutex" :key="getValue(item)" :label="getLabel(item)"
  5. :name="getValue(item)" :customStyle="{width: 'fit-content'}"/>
  6. </uv-checkbox-group>
  7. </ie-form-picker>
  8. </template>
  9. <script>
  10. import _ from 'lodash';
  11. import AiFormFieldPicker from "@/pages/ie/components/ai-form/items/templates/ai-form-field-picker.vue";
  12. export default {
  13. name: "ai-form-field-checkbox",
  14. extends: AiFormFieldPicker, // 选项与picker一致,但呈现是checkbox-group
  15. data() {
  16. return {
  17. lastChecked: []
  18. }
  19. },
  20. computed: {
  21. columnsWithMutex() {
  22. const mutex = this.rule.mutexOption
  23. return mutex ? [mutex, ...this.columns] : this.columns
  24. }
  25. },
  26. methods: {
  27. checkMutex(val) {
  28. const mutex = this.rule.mutexOption
  29. if (!mutex) return // nothing should be checked.
  30. let mutexValues = val
  31. if (this.lastChecked.length < val.length) {
  32. // only need to check new checked item
  33. const [newChecked] = _.difference(val, this.lastChecked)
  34. if (newChecked == mutex) {
  35. mutexValues = [mutex]
  36. } else if (val.includes(mutex)) {
  37. mutexValues = _.difference(val, [mutex])
  38. }
  39. }
  40. this.lastChecked = mutexValues
  41. setTimeout(() => {
  42. // NOTE: can not set `formModel` properties in `change` event directly,
  43. // setTimeout make reactive feature available until last interactive completed.
  44. console.log('check mutex and try to set formModel', this.rule.fieldName, mutexValues)
  45. this.formModel[this.rule.fieldName] = mutexValues
  46. }, 50)
  47. }
  48. }
  49. }
  50. </script>
  51. <style scoped>
  52. ::v-deep .uv-checkbox-group--row {
  53. flex-wrap: wrap;
  54. justify-content: flex-end;
  55. gap: 10px;
  56. width: fit-content;
  57. }
  58. </style>