SaveForm.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const btsStyle = css`
  86. display: flex;
  87. justify-content: end;
  88. `;