SaveForm.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { useCtx } from "@/comm/ctx";
  2. import { css } from "@linaria/core";
  3. import { Button, Form, Input, Select } from "ant-design-vue";
  4. import { useModal } from "queenjs";
  5. import { defineComponent, reactive } from "vue";
  6. import { string } from "vue-types";
  7. const layout = {
  8. wrapperCol: { span: 24 },
  9. };
  10. export default defineComponent({
  11. props: {
  12. thumbnail: string(),
  13. name: string(),
  14. },
  15. setup(props) {
  16. const modal = useModal();
  17. const { storeCtrl } = useCtx();
  18. const formState = reactive({
  19. thumbnail: props.thumbnail,
  20. name: props.name,
  21. category: "default",
  22. });
  23. const rules = reactive({
  24. name: [
  25. { required: true, message: "名称不能为空", trigger: "change" },
  26. {
  27. min: 2,
  28. max: 20,
  29. message: "长度为2~20位字符",
  30. trigger: "change",
  31. },
  32. ],
  33. });
  34. const { validate, validateInfos } = Form.useForm(formState, rules);
  35. const changeCategory = (v: any) => {
  36. formState.category = v;
  37. };
  38. const saveSubmit = async () => {
  39. await validate();
  40. modal.submit(formState);
  41. };
  42. return () => {
  43. return (
  44. <div class={EditFormStyle}>
  45. <div class={"thumbnail mb-24px"}>
  46. <img src={formState.thumbnail} />
  47. </div>
  48. <div class="mb-8px">选择保存位置</div>
  49. <div class={"select_box"}>
  50. <Select
  51. class={"w-full"}
  52. value={formState.category}
  53. options={storeCtrl.state.category}
  54. onChange={(v: any) => changeCategory(v)}
  55. />
  56. </div>
  57. <div class={"mt-24px"}>
  58. <Form {...layout} name="basic">
  59. <Form.Item {...validateInfos.name}>
  60. <Input
  61. prefix="名称"
  62. v-model={[formState.name, "value"]}
  63. maxlength={20}
  64. />
  65. </Form.Item>
  66. </Form>
  67. </div>
  68. <div class={btsStyle}>
  69. <Button onClick={() => modal.cancel()}>取消</Button>
  70. <Button
  71. type="primary"
  72. danger={false}
  73. style={{ marginLeft: "10px" }}
  74. onClick={() => saveSubmit()}
  75. >
  76. 保存
  77. </Button>
  78. </div>
  79. </div>
  80. );
  81. };
  82. },
  83. });
  84. const EditFormStyle = css`
  85. .thumbnail {
  86. text-align: center;
  87. img {
  88. width: 180px;
  89. height: 180px;
  90. object-fit: cover;
  91. }
  92. }
  93. `;
  94. const btsStyle = css`
  95. display: flex;
  96. justify-content: end;
  97. `;