Skip to content

Commit

Permalink
新增layout布局 (#1821)
Browse files Browse the repository at this point in the history
* feat(layout): add a new component

feat #1701

* fix: 修复代码

* fix: 修复PR问题
  • Loading branch information
wuping97 authored Mar 22, 2023
1 parent 7275ed2 commit 9b0c82a
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 6 deletions.
3 changes: 2 additions & 1 deletion example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"pages/image-viewer/image-viewer",
"pages/result/result",
"pages/result/result-page",
"pages/link/link"
"pages/link/link",
"pages/col/col"
],
"subpackages": [
{
Expand Down
9 changes: 5 additions & 4 deletions example/pages/home/data/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ const base = {
name: 'Link',
label: '链接',
},
// {
// name: 'Layout',
// label: '布局',
// },
{
name: 'Layout',
label: '布局',
path: '/pages/col/col',
},
// {
// name: 'Typography',
// label: '字体',
Expand Down
8 changes: 8 additions & 0 deletions src/col/_example/col.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"navigationBarTitleText": "Layout",
"navigationBarBackgroundColor": "#f6f6f6",
"usingComponents": {
"t-col": "tdesign-miniprogram/col/col",
"t-row": "tdesign-miniprogram/row/row"
}
}
17 changes: 17 additions & 0 deletions src/col/_example/col.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.dark {
background-color: #0052d9;
}

.light {
background-color: #366ef4;
}

.dark,
.light {
background-clip: content-box;
color: #fff;
font-size: 28rpx;
line-height: 96rpx;
margin-bottom: 32rpx;
text-align: center;
}
1 change: 1 addition & 0 deletions src/col/_example/col.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Page({});
30 changes: 30 additions & 0 deletions src/col/_example/col.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<view class="demo">
<view class="demo-title">Layout 布局</view>
<view class="demo-desc">
以规则的网格阵列来指导和规范页面中的版面布局以及信息分布,提高界面内布局的一致性,节约成本。
</view>
<t-demo title="01 组件类型" desc="基础用法">
<t-row>
<t-col span="8" t-class="dark">col-8</t-col>
<t-col span="8" t-class="light">col-8</t-col>
<t-col span="8" t-class="dark">col-8</t-col>
</t-row>

<t-row>
<t-col span="4" t-class="dark">col-4</t-col>
<t-col span="16" offset="4" t-class="light">col-16 col-offset-4</t-col>
</t-row>

<t-row>
<t-col offset="12" span="12" t-class="dark">col-12 col-offset-12</t-col>
</t-row>
</t-demo>

<t-demo desc="在列元素之间增加间距">
<t-row gutter="16">
<t-col span="8" t-class="dark">col-8</t-col>
<t-col span="8" t-class="light">col-8</t-col>
<t-col span="8" t-class="dark">col-8</t-col>
</t-row>
</t-demo>
</view>
4 changes: 4 additions & 0 deletions src/col/col.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}
17 changes: 17 additions & 0 deletions src/col/col.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@import '../common/index.less';

.@{prefix}-col {
box-sizing: border-box;
float: left;
}

.col-generate(@n, @i: 1) when(@i <= @n) {
.@{prefix}-col--@{i} {
width: (@i * 100% / @n);
}
.@{prefix}-col--offset-@{i} {
margin-left: (@i * 100% / @n);
}
.col-generate(@n, (@i + 1));
}
.col-generate(24);
30 changes: 30 additions & 0 deletions src/col/col.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SuperComponent, wxComponent, RelationsOptions } from '../common/src/index';
import config from '../common/config';

const { prefix } = config;
const name = `${prefix}-col`;

@wxComponent()
export default class Col extends SuperComponent {
externalClasses = [`${prefix}-class`];

properties = {
span: {
type: Number,
},
offset: {
type: Number,
},
};

data = {
prefix,
classPrefix: name,
};

relations: RelationsOptions = {
'../row/row': {
type: 'parent',
},
};
}
9 changes: 9 additions & 0 deletions src/col/col.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<wxs src="../common/utils.wxs" module="_" />
<wxs src="./col.wxs" module="utils" />

<view
class="class {{prefix}}-class {{ _.cls(classPrefix, [_.formatNumToString(span)]) }} {{ offset ? classPrefix + '--offset-' + offset : '' }}"
style="{{ utils.getColStyles({ gutter }) }}"
>
<slot />
</view>
16 changes: 16 additions & 0 deletions src/col/col.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var utils = require('../common/utils.wxs');

function getColStyles(data) {
if (!data.gutter) {
return '';
}

return utils._style({
'padding-right': utils.addUnit(data.gutter / 2),
'padding-left': utils.addUnit(data.gutter / 2),
});
}

module.exports = {
getColStyles: getColStyles,
};
11 changes: 10 additions & 1 deletion src/common/utils.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* addUnit */
// 为 css 添加单位
function addUnit(value) {
var REGEXP = getRegExp('^\d+(.\d+)?$');
var REGEXP = getRegExp('^-?\d+(.\d+)?$');
if (value == null) {
return undefined;
}
Expand Down Expand Up @@ -124,6 +124,14 @@ function _style(styles) {
return styles;
}

function formatNumToString(value) {
var res = '';
if (!isNaN(value) && typeof value === 'number') {
res = value.toString();
}
return res;
}

module.exports = {
addUnit: addUnit,
isString: isString,
Expand All @@ -134,4 +142,5 @@ module.exports = {
cls: cls,
getBadgeAriaLabel: getBadgeAriaLabel,
_style: _style,
formatNumToString: formatNumToString,
};
Empty file added src/row/README.md
Empty file.
4 changes: 4 additions & 0 deletions src/row/row.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}
5 changes: 5 additions & 0 deletions src/row/row.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.t-row:after {
clear: both;
content: '';
display: table;
}
46 changes: 46 additions & 0 deletions src/row/row.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { SuperComponent, wxComponent, RelationsOptions } from '../common/src/index';
import config from '../common/config';

const { prefix } = config;
@wxComponent()
export default class Row extends SuperComponent {
externalClasses = [];

properties = {
gutter: {
type: Number,
},
};

data = {
prefix,
};

relations: RelationsOptions = {
'../col/col': {
type: 'child',
linked(target) {
const { gutter } = this.data;
if (gutter) {
target.setData({ gutter });
}
},
},
};

observers = {
gutter() {
this.setGutter();
},
};

methods = {
setGutter() {
const { gutter } = this.data;
const cols = this.$children;
cols.forEach((col) => {
col.setData({ gutter });
});
},
};
}
5 changes: 5 additions & 0 deletions src/row/row.wxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<wxs src="./row.wxs" module="utils" />

<view class="class {{prefix}}-row" style="{{ utils.getRowStyles({ gutter }) }}">
<slot />
</view>
16 changes: 16 additions & 0 deletions src/row/row.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var utils = require('../common/utils.wxs');

function getRowStyles(data) {
if (!data.gutter) {
return '';
}

return utils._style({
'margin-right': utils.addUnit(-data.gutter / 2),
'margin-left': utils.addUnit(-data.gutter / 2),
});
}

module.exports = {
getRowStyles: getRowStyles,
};

0 comments on commit 9b0c82a

Please sign in to comment.