SuperQueryValComponent.vue 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script lang="tsx">
  2. import { computed, defineComponent, PropType, unref } from 'vue';
  3. import { FormSchema } from '/@/components/Form';
  4. import { upperFirst } from 'lodash-es';
  5. import { componentMap } from '/@/components/Form/src/componentMap';
  6. import { createPlaceholderMessage } from '/@/components/Form/src/helper';
  7. import { isFunction } from '/@/utils/is';
  8. export default defineComponent({
  9. name: 'OnlineSuperQueryValComponent',
  10. inheritAttrs: false,
  11. props: {
  12. schema: {
  13. type: Object as PropType<FormSchema>,
  14. default: () => ({}),
  15. },
  16. formModel: {
  17. type: Object as PropType<Recordable>,
  18. default: () => ({}),
  19. },
  20. setFormModel: {
  21. type: Function as PropType<(key: string, value: any) => void>,
  22. default: null,
  23. },
  24. },
  25. setup(props) {
  26. const getComponentsProps = computed(() => {
  27. const { schema, formModel } = props;
  28. let { componentProps = {} } = schema;
  29. if (isFunction(componentProps)) {
  30. componentProps = componentProps({ schema, formModel }) ?? {};
  31. }
  32. return componentProps as Recordable;
  33. });
  34. const getValues = computed(() => {
  35. const { formModel, schema } = props;
  36. let obj = {
  37. field: schema.field,
  38. model: formModel,
  39. values: {
  40. ...formModel,
  41. } as Recordable,
  42. schema: schema,
  43. };
  44. return obj;
  45. });
  46. function renderComponent() {
  47. const { component, changeEvent = 'change', valueField } = props.schema;
  48. const field = 'val';
  49. const isCheck = component && ['Switch', 'Checkbox'].includes(component);
  50. const eventKey = `on${upperFirst(changeEvent)}`;
  51. const on = {
  52. [eventKey]: (...args: Nullable<Recordable>[]) => {
  53. const [e] = args;
  54. if (propsData[eventKey]) {
  55. propsData[eventKey](...args);
  56. }
  57. const target = e ? e.target : null;
  58. const value = target ? (isCheck ? target.checked : target.value) : e;
  59. props.setFormModel(field, value);
  60. },
  61. };
  62. const Comp = componentMap.get(component) as ReturnType<typeof defineComponent>;
  63. const propsData: Recordable = {
  64. allowClear: true,
  65. getPopupContainer: (trigger: Element) => trigger.parentNode,
  66. ...unref(getComponentsProps),
  67. };
  68. const isCreatePlaceholder = !propsData.disabled;
  69. // RangePicker place是一个数组
  70. if (isCreatePlaceholder && component !== 'RangePicker' && component) {
  71. //自动设置placeholder
  72. propsData.placeholder = unref(getComponentsProps)?.placeholder || createPlaceholderMessage(component) + props.schema.label;
  73. }
  74. propsData.codeField = field;
  75. propsData.formValues = unref(getValues);
  76. const bindValue: Recordable = {
  77. [valueField || (isCheck ? 'checked' : 'value')]: props.formModel[field],
  78. };
  79. const compAttr: Recordable = {
  80. ...propsData,
  81. ...on,
  82. ...bindValue,
  83. allowClear: true,
  84. };
  85. return <Comp {...compAttr} />;
  86. }
  87. return () => {
  88. return <div style="width:100%">{renderComponent()}</div>;
  89. };
  90. },
  91. });
  92. </script>