阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解
前言
之前我們介紹了lowcode-engine一些基礎(chǔ)相關(guān)內(nèi)容,接下來我們通過引擎實(shí)戰(zhàn)一些內(nèi)容,在一些場(chǎng)景當(dāng)中,我們的默認(rèn)容器不是頁面,而是需要自定義容器。例如,在常見的低代碼平臺(tái)中默認(rèn)容器是表單容器,通過表單容器類提供布局能力。接下來我們就去實(shí)現(xiàn)這個(gè)功能。
物料【FormContainer】開發(fā)
該物料組件主要是用來存放所有FormItem,可以設(shè)置列數(shù)。
物料組件
接下來我們看代碼內(nèi)容
export interface IFormContainerProps {
// 列數(shù)
cols: number;
}
/**
* Form容器包裝器。lowcode-engine不支持直接使用hooks組件,需要包裹一層,要不內(nèi)容元素沒辦法直接嵌入
*/
export const FormContainerWrapper = forwardRef<IFormRef | undefined, IFormContainerProps>(function FormContainer({
cols,
children
}, ref) {
const [form] = Form.useForm();
React.useImperativeHandle(ref, () => {
return {
formRef: form
}
}, [])
const getChildren = () => {
if (React.Children.count(children) <= 1) {
return children;
}
const newArray = groupArray(React.Children.toArray(children), cols);
return newArray.map(childs => {
return <Row key={childs?.[0]?.key} gutter={[16, 24]} className={generatorClass('form-container-row')}>
{
childs.map(child => {
const { props } = child;
const name = props.componentId || props.__id;
return <Col key={name} span={24 / cols}>
<Form.Item
label=""
name={name}
rules={[{ required: props.isRequired, message: `${props.title}不能為空!` }]}
>
{child}
</Form.Item>
</Col>;
})
}
</Row>
})
}
const rootClassNames = classNames(generatorClass('form-container'));
return (
<Form form={form} className={rootClassNames}>
{getChildren()}
</Form>
)
});
/**
* 容器組件
*/
export class FormContainer extends React.Component<IFormContainerProps, any> {
render() {
return (
<FormContainerWrapper {...this.props} />
)
}
}
在實(shí)現(xiàn)的過程中開始使用的hooks組件,發(fā)現(xiàn)會(huì)有問題,我們用class組件包裝了下,就沒什么問題了。后續(xù)遷去看看源碼引擎是怎么加載物料的,再來回答這個(gè)問題。
物料Meta信息
上面的物料組件就有一個(gè)cols需要配置,對(duì)用的setter我們可以使用官方提供的RadioGroupSetter. 由于整個(gè)配置模版內(nèi)容比較多,我只把關(guān)鍵點(diǎn)configure配置內(nèi)容說下。
"configure": {
"props": [
{
"title": {
"label": {
"type": "i18n",
"en-US": "cols",
"zh-CN": "列數(shù)"
}
},
"name": "cols",
"setter": {
"title": "列數(shù)",
"componentName": "RadioGroupSetter",
"isRequired": true,
"props": {
"options": [{
"label": "1列",
"value": 1,
}, {
"label": "2列",
"value": 2,
}, {
"label": "3列",
"value": 3,
}, {
"label": "4列",
"value": 4
}]
},
"initialValue": 1
}
}
],
"supports": {
},
"component": {
// 是否是容器組件,如果是容器組件,別的組件可以放置內(nèi)容
isContainer: true
},
}
我們看配置內(nèi)容,一個(gè)是設(shè)置setter, 還有比較重要的一點(diǎn)就是在component下需要配置isContainer。如果為true,表示內(nèi)容其它組件可以拖到該容器內(nèi)。
模版內(nèi)容修改
通過研究lowcode-engine,我們可以知道內(nèi)容的渲染是通過schema.json來渲染內(nèi)容。我們只需修改下初始的 schema.json。加上容器組件,模版內(nèi)容為
"componentName": "Page",
"id": "node_dockcviv8fo1",
...
"title": "頁面",
"isLocked": false,
"condition": true,
"conditionGroup": "",
"children": [
{
// 容器組件
"componentName": "FormContainer",
"id": "node_oclcdgs7nr1",
"props": {
"cols": 2
},
"hidden": false,
"title": "",
"isLocked": false,
"condition": true,
"conditionGroup": ""
}
]
我們看引擎的大綱內(nèi)容,默認(rèn)就有表單組件了。

這里有一點(diǎn)需要注意,有些場(chǎng)景,我們需要把容器組件toolbar上的操作給禁用掉,這塊比較簡(jiǎn)單,可以看下官網(wǎng)怎么設(shè)置。
案例展示
我們看一個(gè)完整的例子。

結(jié)束語
以上就是lowcode-engine設(shè)置默認(rèn)容器。后續(xù)我們會(huì)接著把案例完善起來。能做到創(chuàng)建表單,表單預(yù)覽,數(shù)據(jù)的提交等功能。
更多關(guān)于lowcode engine設(shè)置默認(rèn)容器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
React關(guān)于antd table中select的設(shè)值更新問題
這篇文章主要介紹了React關(guān)于antd table中select的設(shè)值更新問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
create-react-app構(gòu)建項(xiàng)目慢的解決方法
這篇文章主要介紹了create-react-app構(gòu)建項(xiàng)目慢的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
React手寫簽名組件react-signature實(shí)現(xiàn)簽字demo
這篇文章主要為大家介紹了React手寫簽名組件react-signature實(shí)現(xiàn)簽字demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
useReducer使用詳解及其應(yīng)用場(chǎng)景
這篇文章主要介紹了useReducer使用詳解及其應(yīng)用場(chǎng)景,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
淺談使用React.setState需要注意的三點(diǎn)
本篇文章主要介紹了淺談使用React.setState需要注意的三點(diǎn),提出了三點(diǎn)對(duì) React 新手來說是很容易忽略的地方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
react antd實(shí)現(xiàn)動(dòng)態(tài)增減表單
antd是react流行的ui框架庫,本文主要介紹了react antd實(shí)現(xiàn)動(dòng)態(tài)增減表單,分享給大家,感興趣的小伙伴們可以參考一下2021-06-06
2023年最新react面試題總結(jié)大全(附詳細(xì)答案)
React是一種廣泛使用的JavaScript庫,為構(gòu)建用戶界面提供了強(qiáng)大的工具和技術(shù),這篇文章主要給大家介紹了關(guān)于2023年最新react面試題的相關(guān)資料,文中還附有詳細(xì)答案,需要的朋友可以參考下2023-10-10
教你快速搭建 React Native 開發(fā)環(huán)境
這篇文章主要介紹了搭建 React Native 開發(fā)環(huán)境的詳細(xì)過程,本文通過圖文指令給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

