|
1 |
| -import {MultiCompBuilder, withDefault} from "../../generators"; |
2 |
| -import { keyValueListControl } from "../../controls/keyValueListControl"; |
3 |
| - |
4 |
| -export const VariablesComp = new MultiCompBuilder( |
5 |
| - { |
6 |
| - variables: withDefault(keyValueListControl(false, [], "variable"), [{ key: "", value: "" }]), |
7 |
| - }, |
8 |
| - (props) => props //props.variables |
9 |
| - ) |
10 |
| - .setPropertyViewFn((children) => ( |
| 1 | +import { simpleMultiComp } from "../../generators"; |
| 2 | +import { SimpleNameComp } from "@lowcoder-ee/comps/comps/simpleNameComp"; |
| 3 | +import { StringControl } from "@lowcoder-ee/comps/controls/codeControl"; |
| 4 | +import { list } from "@lowcoder-ee/comps/generators/list"; |
| 5 | +import { Input } from "components/Input"; |
| 6 | +import { ControlPropertyViewWrapper } from "components/control"; |
| 7 | +import { ReactNode, useContext, useState } from "react"; |
| 8 | +import { KeyValueList } from "components/keyValueList"; |
| 9 | +import { trans } from "i18n"; |
| 10 | +import { PopupCard } from "components/popupCard"; |
| 11 | +import { EditorContext, EditorState } from "@lowcoder-ee/comps/editorState"; |
| 12 | +import { migrateOldData } from "@lowcoder-ee/comps/generators/simpleGenerators"; |
| 13 | + |
| 14 | +interface VariablesParams { |
| 15 | + // variables: string[]; todo support parse variables |
| 16 | +} |
| 17 | + |
| 18 | +const VariableKey = ({children, dispatch}: any) => { |
| 19 | + const [editing, setEditing] = useState(false); |
| 20 | + const [error, setError] = useState<string | undefined>(undefined); |
| 21 | + const editorState = useContext(EditorContext); |
| 22 | + |
| 23 | + return ( |
11 | 24 | <>
|
12 |
| - {children.variables.propertyView({})} |
| 25 | + <div style={{ flexGrow: 1, marginRight: "8px" }}> |
| 26 | + <Input |
| 27 | + value={children.key.getView()} |
| 28 | + onBlur={(e) => { |
| 29 | + const { value } = e.target; |
| 30 | + value !== children.key.getView() && |
| 31 | + editorState.rename(children.key.getView(), value) |
| 32 | + }} |
| 33 | + onPressEnter={(e) => { |
| 34 | + const { value } = e.target as HTMLInputElement; |
| 35 | + value !== children.key.getView() && |
| 36 | + editorState.rename(children.key.getView(), value) |
| 37 | + }} |
| 38 | + onChange={(e) => { |
| 39 | + const nextName = e.target.value; |
| 40 | + setError(editorState.checkRename(children.key.getView(), nextName)); |
| 41 | + }} |
| 42 | + /> |
| 43 | + <PopupCard |
| 44 | + editorFocus={!!error} |
| 45 | + title={error ? trans("error") : ""} |
| 46 | + content={error} |
| 47 | + hasError={!!error} |
| 48 | + /> |
| 49 | + </div> |
13 | 50 | </>
|
14 |
| - )) |
15 |
| - .build(); |
| 51 | + ) |
| 52 | +} |
| 53 | +const VariableItem = class extends simpleMultiComp({ |
| 54 | + key: SimpleNameComp, |
| 55 | + value: StringControl, |
| 56 | +}) { |
| 57 | + propertyView(params: VariablesParams): ReactNode { |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <div style={{ display: "flex", gap: "8px", flexGrow: 1 }}> |
| 61 | + <VariableKey |
| 62 | + children={this.children} |
| 63 | + dispatch={this.dispatch} |
| 64 | + /> |
| 65 | + <div style={{ width: "232px", flexGrow: 1 }}> |
| 66 | + {this.children.value.propertyView({ placeholder: "value" })} |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </> |
| 70 | + ) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +const VariableListPropertyViewWrapper = ({children}: any) => { |
| 75 | + const editorState = useContext(EditorContext); |
| 76 | + return children(editorState); |
| 77 | +} |
| 78 | + |
| 79 | +export const VariablesComp = class extends list(VariableItem) { |
| 80 | + genNewName(editorState: EditorState) { |
| 81 | + const name = editorState.getNameGenerator().genItemName("variable"); |
| 82 | + return name; |
| 83 | + } |
| 84 | + |
| 85 | + add(editorState: EditorState, extraInfo?: any) { |
| 86 | + const name = this.genNewName(editorState); |
| 87 | + |
| 88 | + this.dispatch( |
| 89 | + this.pushAction({ |
| 90 | + key: name, |
| 91 | + value: '', |
| 92 | + }) |
| 93 | + ); |
| 94 | + } |
| 95 | + propertyView(params: VariablesParams): ReactNode { |
| 96 | + return ( |
| 97 | + <VariableListPropertyViewWrapper> |
| 98 | + {(editorState: EditorState) => ( |
| 99 | + <ControlPropertyViewWrapper {...params}> |
| 100 | + <KeyValueList |
| 101 | + list={this.getView().map((child) => child.propertyView(params))} |
| 102 | + onAdd={() => this.add(editorState)} |
| 103 | + onDelete={(item, index) => this.dispatch(this.deleteAction(index))} |
| 104 | + /> |
| 105 | + </ControlPropertyViewWrapper> |
| 106 | + )} |
| 107 | + </VariableListPropertyViewWrapper> |
| 108 | + ); |
| 109 | + } |
| 110 | +}; |
0 commit comments