-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #320 from mousheng/mention_component
Add mention component
- Loading branch information
Showing
10 changed files
with
320 additions
and
3 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
client/packages/lowcoder-design/src/icons/icon-mention-comp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
client/packages/lowcoder/src/comps/comps/textInputComp/mentionComp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,268 @@ | ||
import { useState, useEffect } from "react"; | ||
import { | ||
NameConfig, | ||
NameConfigPlaceHolder, | ||
NameConfigRequired, | ||
withExposingConfigs, | ||
} from "comps/generators/withExposing"; | ||
import { Section, sectionNames } from "lowcoder-design"; | ||
import { BoolControl } from "../../controls/boolControl"; | ||
import { AutoHeightControl } from "../../controls/autoHeightControl"; | ||
import { UICompBuilder } from "../../generators"; | ||
import { FormDataPropertyView } from "../formComp/formDataConstants"; | ||
import { | ||
checkMentionListData, | ||
textInputChildren, | ||
} from "./textInputConstants"; | ||
import { | ||
withMethodExposing, | ||
refMethods, | ||
} from "../../generators/withMethodExposing"; | ||
import { styleControl } from "comps/controls/styleControl"; | ||
import styled from "styled-components"; | ||
import { | ||
InputLikeStyle, | ||
InputLikeStyleType, | ||
} from "comps/controls/styleControlConstants"; | ||
import { | ||
disabledPropertyView, | ||
hiddenPropertyView, | ||
maxLengthPropertyView, | ||
minLengthPropertyView, | ||
readOnlyPropertyView, | ||
requiredPropertyView, | ||
} from "comps/utils/propertyUtils"; | ||
import { booleanExposingStateControl } from "comps/controls/codeStateControl"; | ||
import { trans } from "i18n"; | ||
import { RefControl } from "comps/controls/refControl"; | ||
import { TextAreaRef } from "antd/lib/input/TextArea"; | ||
import { Mentions, ConfigProvider } from "antd"; | ||
import { blurMethod, focusWithOptions } from "comps/utils/methodUtils"; | ||
import type { MentionsOptionProps } from "antd/es/mentions"; | ||
import { | ||
textInputValidate, | ||
} from "../textInputComp/textInputConstants"; | ||
import { jsonControl } from "@lowcoder-ee/comps/controls/codeControl"; | ||
// 事件控制 | ||
import { | ||
submitEvent, | ||
eventHandlerControl, | ||
mentionEvent, | ||
focusEvent, | ||
blurEvent, | ||
changeEvent | ||
} from "comps/controls/eventHandlerControl"; | ||
|
||
const Wrapper = styled.div<{ | ||
$style: InputLikeStyleType; | ||
}>` | ||
height: 100%; | ||
.ant-input-clear-icon { | ||
opacity: 0.45; | ||
color: ${(props) => props.$style.text}; | ||
top: 10px; | ||
&:hover { | ||
opacity: 0.65; | ||
color: ${(props) => props.$style.text}; | ||
} | ||
} | ||
`; | ||
|
||
const EventOptions = [ | ||
focusEvent, | ||
blurEvent, | ||
changeEvent, | ||
mentionEvent, | ||
submitEvent, | ||
] as const; | ||
|
||
let MentionTmpComp = (function () { | ||
const childrenMap = { | ||
...textInputChildren, | ||
viewRef: RefControl<TextAreaRef>, | ||
allowClear: BoolControl, | ||
autoHeight: AutoHeightControl, | ||
style: styleControl(InputLikeStyle), | ||
mentionList: jsonControl(checkMentionListData, { | ||
"@": ["Li Lei", "Han Meimei"], | ||
"#": ["123", "456", "789"], | ||
}), | ||
onEvent: eventHandlerControl(EventOptions), | ||
invalid: booleanExposingStateControl("invalid"), | ||
}; | ||
|
||
return new UICompBuilder(childrenMap, (props) => { | ||
const { mentionList } = props; | ||
const [validateState, setvalidateState] = useState({}); | ||
const [activationFlag, setActivationFlag] = useState(false); | ||
const [prefix, setPrefix] = useState<PrefixType>("@"); | ||
type PrefixType = "@" | keyof typeof mentionList; | ||
|
||
// 获取提及搜索关键字 | ||
const onSearch = (_: string, newPrefix: PrefixType) => { | ||
setPrefix(newPrefix); | ||
}; | ||
const onChange = (value: string) => { | ||
props.value.onChange(value); | ||
props.onEvent("change"); | ||
}; | ||
|
||
const onPressEnter = (e: any) => { | ||
if (e.shiftKey) { | ||
e.preventDefault(); | ||
props.onEvent("submit"); | ||
} | ||
}; | ||
|
||
const onSelect = (option: MentionsOptionProps) => { | ||
props.onEvent("mention"); | ||
}; | ||
const getValidate = (value: any): "" | "warning" | "error" | undefined => { | ||
if ( | ||
value.hasOwnProperty("validateStatus") && | ||
value["validateStatus"] === "error" | ||
) | ||
return "error"; | ||
return ""; | ||
}; | ||
|
||
const getTextInputValidate = () => { | ||
return { | ||
value: { value: props.value.value }, | ||
required: props.required, | ||
minLength: props?.minLength ?? 0, | ||
maxLength: props?.maxLength ?? 0, | ||
validationType: props.validationType, | ||
regex: props.regex, | ||
customRule: props.customRule, | ||
}; | ||
}; | ||
|
||
useEffect(() => { | ||
if (activationFlag) { | ||
const temp = textInputValidate(getTextInputValidate()); | ||
setvalidateState(temp); | ||
props.invalid.onChange(temp.validateStatus !== ""); | ||
} | ||
}, [ | ||
props.value.value, | ||
props.required, | ||
props?.minLength, | ||
props?.maxLength, | ||
props.validationType, | ||
props.regex, | ||
props.customRule, | ||
]); | ||
return props.label({ | ||
required: props.required, | ||
children: ( | ||
<Wrapper $style={props.style}> | ||
<ConfigProvider | ||
theme={{ | ||
token: { | ||
colorBgContainer: props.style.background, | ||
colorBorder: props.style.border, | ||
borderRadius: parseInt(props.style.radius), | ||
colorText: props.style.text, | ||
colorPrimary: props.style.accent, | ||
}, | ||
}} | ||
> | ||
<Mentions | ||
prefix={Object.keys(mentionList)} | ||
onFocus={() => { | ||
setActivationFlag(true); | ||
props.onEvent("focus"); | ||
}} | ||
onBlur={() => props.onEvent("blur")} | ||
onPressEnter={onPressEnter} | ||
onSearch={onSearch} | ||
onChange={onChange} | ||
onSelect={onSelect} | ||
placeholder={props.placeholder} | ||
value={props.value.value} | ||
disabled={props.disabled} | ||
status={getValidate(validateState)} | ||
options={(mentionList[prefix] || []).map((value: string) => ({ | ||
key: value, | ||
value, | ||
label: value, | ||
}))} | ||
autoSize={props.autoHeight} | ||
style={{ height: "100%", maxHeight: "100%", resize: "none", padding: props.style.padding }} | ||
readOnly={props.readOnly} | ||
/> | ||
</ConfigProvider> | ||
</Wrapper> | ||
), | ||
style: props.style, | ||
...validateState, | ||
}); | ||
}) | ||
.setPropertyViewFn((children) => ( | ||
<> | ||
<Section name={sectionNames.basic}> | ||
{children.mentionList.propertyView({ | ||
label: trans("mention.mentionList"), | ||
})} | ||
{children.value.propertyView({ label: trans("prop.defaultValue") })} | ||
{children.placeholder.propertyView({ | ||
label: trans("prop.placeholder"), | ||
})} | ||
</Section> | ||
<FormDataPropertyView {...children} /> | ||
{children.label.getPropertyView()} | ||
|
||
<Section name={sectionNames.interaction}> | ||
{children.onEvent.getPropertyView()} | ||
{disabledPropertyView(children)} | ||
</Section> | ||
|
||
<Section name={sectionNames.advanced}> | ||
{readOnlyPropertyView(children)} | ||
</Section> | ||
|
||
<Section name={sectionNames.validation}> | ||
{requiredPropertyView(children)} | ||
{children.validationType.propertyView({ | ||
label: trans("prop.textType"), | ||
})} | ||
{minLengthPropertyView(children)} | ||
{maxLengthPropertyView(children)} | ||
{children.customRule.propertyView({})} | ||
</Section> | ||
|
||
<Section name={sectionNames.layout}> | ||
{children.autoHeight.getPropertyView()} | ||
{hiddenPropertyView(children)} | ||
</Section> | ||
|
||
<Section name={sectionNames.style}> | ||
{children.style.getPropertyView()} | ||
</Section> | ||
</> | ||
)) | ||
.build(); | ||
})(); | ||
|
||
MentionTmpComp = class extends MentionTmpComp { | ||
override autoHeight(): boolean { | ||
return this.children.autoHeight.getView(); | ||
} | ||
}; | ||
|
||
const TextareaTmp2Comp = withMethodExposing( | ||
MentionTmpComp, | ||
refMethods([focusWithOptions, blurMethod]) | ||
); | ||
|
||
export const MentionComp = withExposingConfigs(TextareaTmp2Comp, [ | ||
new NameConfig("value", trans("export.inputValueDesc")), | ||
NameConfigPlaceHolder, | ||
NameConfigRequired, | ||
new NameConfig("invalid", trans("export.invalidDesc")), | ||
new NameConfig("hidden", trans("export.hiddenDesc")), | ||
new NameConfig("disabled", trans("export.disabledDesc")), | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.