WbwDetailBasic.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { useEffect, useState } from "react";
  2. import { useIntl } from "react-intl";
  3. import { Form, Input, Button, Popover } from "antd";
  4. import { Collapse } from "antd";
  5. import { MoreOutlined } from "@ant-design/icons";
  6. import type { IWbw, IWbwField } from "./WbwWord"
  7. import WbwMeaningSelect from "./WbwMeaningSelect";
  8. import WbwDetailFm from "./WbwDetailFm";
  9. import WbwDetailParent2 from "./WbwDetailParent2";
  10. import WbwDetailFactor from "./WbwDetailFactor";
  11. import WbwDetailBasicRelation from "./WbwDetailBasicRelation";
  12. import WbwDetailParent from "./WbwDetailParent";
  13. import WbwDetailCase from "./WbwDetailCase";
  14. import WbwDetailOrder from "./WbwDetailOrder";
  15. const { Panel } = Collapse;
  16. export interface IWordBasic {
  17. meaning?: string[];
  18. case?: string;
  19. factors?: string;
  20. factorMeaning?: string;
  21. parent?: string;
  22. }
  23. interface IWidget {
  24. data: IWbw;
  25. visible?: boolean;
  26. showRelation?: boolean;
  27. readonly?: boolean;
  28. onChange?: Function;
  29. onRelationAdd?: Function;
  30. }
  31. const WbwDetailBasicWidget = ({
  32. data,
  33. visible,
  34. showRelation = true,
  35. readonly = false,
  36. onChange,
  37. onRelationAdd,
  38. }: IWidget) => {
  39. const [form] = Form.useForm();
  40. const intl = useIntl();
  41. const [factors, setFactors] = useState<string[] | undefined>(
  42. data.factors?.value?.split("+")
  43. );
  44. const [openCreate, setOpenCreate] = useState(false);
  45. const [_meaning, setMeaning] = useState<string | undefined>();
  46. const [currTip, setCurrTip] = useState(1);
  47. useEffect(() => {
  48. if (typeof data.meaning?.value === "string") {
  49. setMeaning(data.meaning?.value);
  50. }
  51. }, [data.meaning]);
  52. const onMeaningChange = (value: string) => {
  53. console.log(`Selected: ${value}`);
  54. if (typeof onChange !== "undefined") {
  55. onChange({ field: "meaning", value: value });
  56. }
  57. };
  58. return (
  59. <>
  60. <Form
  61. labelCol={{ span: 4 }}
  62. wrapperCol={{ span: 20 }}
  63. className="wbw_detail_basic"
  64. name="basic"
  65. form={form}
  66. initialValues={{
  67. meaning: data.meaning?.value,
  68. factors: data.factors?.value,
  69. factorMeaning: data.factorMeaning?.value,
  70. parent: data.parent?.value,
  71. case: data.case?.value,
  72. parent2: data.parent2?.value,
  73. grammar2: data.grammar2?.value,
  74. }}
  75. >
  76. <Form.Item
  77. style={{ marginBottom: 6 }}
  78. name="meaning"
  79. label={intl.formatMessage({ id: "forms.fields.meaning.label" })}
  80. tooltip={intl.formatMessage({ id: "forms.fields.meaning.tooltip" })}
  81. >
  82. <div style={{ display: "flex" }}>
  83. <div style={{ display: "flex", width: "100%" }}>
  84. <Input
  85. disabled={readonly}
  86. value={_meaning}
  87. allowClear
  88. placeholder="请输入"
  89. onChange={(e) => {
  90. console.log("meaning input", e.target.value);
  91. setMeaning(e.target.value);
  92. onMeaningChange(e.target.value);
  93. }}
  94. />
  95. <Popover
  96. content={
  97. <WbwMeaningSelect
  98. data={data}
  99. onSelect={(meaning: string) => {
  100. console.log(meaning);
  101. setMeaning(meaning);
  102. form.setFieldsValue({
  103. meaning: meaning,
  104. });
  105. onMeaningChange(meaning);
  106. }}
  107. />
  108. }
  109. trigger={readonly ? "" : "click"}
  110. overlayStyle={{ width: 500 }}
  111. placement="bottom"
  112. open={openCreate}
  113. onOpenChange={(open: boolean) => {
  114. setOpenCreate(open);
  115. }}
  116. >
  117. <Button
  118. disabled={readonly}
  119. type="text"
  120. icon={<MoreOutlined />}
  121. />
  122. </Popover>
  123. </div>
  124. <WbwDetailOrder
  125. sn={5}
  126. visible={visible}
  127. curr={currTip}
  128. onChange={() => setCurrTip(currTip + 1)}
  129. />
  130. </div>
  131. </Form.Item>
  132. <Form.Item
  133. style={{ marginBottom: 6 }}
  134. name="factors"
  135. label={intl.formatMessage({ id: "forms.fields.factors.label" })}
  136. tooltip={intl.formatMessage({ id: "forms.fields.factors.tooltip" })}
  137. >
  138. <div style={{ display: "flex" }}>
  139. <WbwDetailFactor
  140. readonly={readonly}
  141. data={data}
  142. onChange={(value: string) => {
  143. setFactors(value.split("+"));
  144. if (typeof onChange !== "undefined") {
  145. onChange({ field: "factors", value: value });
  146. }
  147. }}
  148. />
  149. <WbwDetailOrder
  150. sn={2}
  151. visible={visible}
  152. curr={currTip}
  153. onChange={() => setCurrTip(currTip + 1)}
  154. />
  155. </div>
  156. </Form.Item>
  157. <Form.Item
  158. style={{ marginBottom: 6 }}
  159. name="factorMeaning"
  160. label={intl.formatMessage({
  161. id: "forms.fields.factor.meaning.label",
  162. })}
  163. tooltip={intl.formatMessage({
  164. id: "forms.fields.factor.meaning.tooltip",
  165. })}
  166. >
  167. <div style={{ display: "flex" }}>
  168. <WbwDetailFm
  169. factors={factors}
  170. readonly={readonly}
  171. value={data.factorMeaning?.value?.split("+")}
  172. onChange={(value: string[]) => {
  173. console.log("fm change", value);
  174. if (typeof onChange !== "undefined") {
  175. onChange({ field: "factorMeaning", value: value.join("+") });
  176. }
  177. }}
  178. onJoin={(value: string) => {
  179. setMeaning(value);
  180. onMeaningChange(value);
  181. }}
  182. />
  183. <WbwDetailOrder
  184. sn={4}
  185. visible={visible}
  186. curr={currTip}
  187. onChange={() => setCurrTip(currTip + 1)}
  188. />
  189. </div>
  190. </Form.Item>
  191. <Form.Item
  192. style={{ marginBottom: 6 }}
  193. label={intl.formatMessage({ id: "forms.fields.case.label" })}
  194. tooltip={intl.formatMessage({ id: "forms.fields.case.tooltip" })}
  195. name="case"
  196. >
  197. <div style={{ display: "flex" }}>
  198. <WbwDetailCase
  199. readonly={readonly}
  200. data={data}
  201. onChange={(value: string) => {
  202. if (typeof onChange !== "undefined") {
  203. onChange({ field: "case", value: value });
  204. }
  205. }}
  206. />
  207. <WbwDetailOrder
  208. sn={3}
  209. visible={visible}
  210. curr={currTip}
  211. onChange={() => setCurrTip(currTip + 1)}
  212. />
  213. </div>
  214. </Form.Item>
  215. <Form.Item
  216. style={{ marginBottom: 6 }}
  217. name="parent"
  218. label={intl.formatMessage({
  219. id: "forms.fields.parent.label",
  220. })}
  221. tooltip={intl.formatMessage({
  222. id: "forms.fields.parent.tooltip",
  223. })}
  224. >
  225. <div style={{ display: "flex" }}>
  226. <WbwDetailParent
  227. readonly={readonly}
  228. data={data}
  229. onChange={(value: string) => {
  230. if (typeof onChange !== "undefined") {
  231. onChange({ field: "parent", value: value });
  232. }
  233. }}
  234. />
  235. <WbwDetailOrder
  236. sn={1}
  237. visible={visible}
  238. curr={currTip}
  239. onChange={() => setCurrTip(currTip + 1)}
  240. />
  241. </div>
  242. </Form.Item>
  243. <Collapse bordered={false}>
  244. <Panel header="词源" key="parent2">
  245. <WbwDetailParent2
  246. data={data}
  247. onChange={(e: IWbwField) => {
  248. if (typeof onChange !== "undefined") {
  249. onChange(e);
  250. }
  251. }}
  252. />
  253. </Panel>
  254. </Collapse>
  255. <WbwDetailBasicRelation
  256. data={data}
  257. showRelation={showRelation}
  258. onChange={(e: IWbwField) => {
  259. if (typeof onChange !== "undefined") {
  260. onChange(e);
  261. }
  262. }}
  263. onRelationAdd={onRelationAdd}
  264. />
  265. </Form>
  266. </>
  267. );
  268. };
  269. export default WbwDetailBasicWidget;