Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(slider): enable async render #2115

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/common/bus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export default class Bus {
listeners: Map<string, any>;
emited: Set<string>;

constructor() {
this.listeners = new Map();
this.emited = new Set();
}

on(evtName: string, listener) {
if (this.emited.has(evtName)) {
listener();
return;
}
const target = this.listeners.get(evtName) || [];

target.push(listener);
Expand All @@ -18,6 +24,7 @@ export default class Bus {

if (listeners) {
listeners.forEach((func) => func());
this.emited.add(evtName);
}
}
}
12 changes: 11 additions & 1 deletion src/slider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ dragstart | \- | 开始拖动时触发

## FAQ

当 slider 外层使用 `hidden` 包裹,需要在 `hidden = false` 时,重新调用组件的 `getInitialStyle` 方法,才能正常渲染。
当 slider 外层使用 `hidden` 包裹,需要在 `hidden = false` 时,重新调用组件的 `init` 方法,才能正常渲染。如下:

```html
<t-slider id="slider" />
```

```js
const $slider = this.selectComponent('#slider');

$slider.init();
```
61 changes: 33 additions & 28 deletions src/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,11 @@ export default class Slider extends SuperComponent {
this.handlePropsChange(newValue);
},
_value(newValue: SliderValue) {
const { min, max, range } = this.properties;
const { maxRange } = this.data;

if (range) {
const left = (maxRange * (newValue[0] - Number(min))) / (Number(max) - Number(min));
const right = (maxRange * (Number(max) - newValue[1])) / (Number(max) - Number(min));
// 因为要计算点相对于线的绝对定位,所以要取整条线的长度而非可滑动的范围
this.setLineStyle(left, right);
} else {
this.setSingleBarWidth(newValue as number);
}

this.setData({
isVisibleToScreenReader: true,
});
setTimeout(() => {
this.setData({
isVisibleToScreenReader: false,
});
}, 2e3);
this.bus.on('initial', () => this.renderLine(newValue));
this.toggleA11yTips();
},
marks(val) {
if (this.data.initialLeft != null) {
this.handleMark(val);
} else {
this.bus.on('initial', () => this.handleMark(val));
}
this.bus.on('initial', () => this.handleMark(val));
},
};

Expand All @@ -116,10 +94,35 @@ export default class Slider extends SuperComponent {
attached() {
const { value } = this.properties;
if (!value) this.handlePropsChange(0);
this.getInitialStyle();
this.init();
},
};

toggleA11yTips() {
this.setData({
isVisibleToScreenReader: true,
});
setTimeout(() => {
this.setData({
isVisibleToScreenReader: false,
});
}, 2000);
}

renderLine(val) {
const { min, max, range } = this.properties;
const { maxRange } = this.data;

if (range) {
const left = (maxRange * (val[0] - Number(min))) / (Number(max) - Number(min));
const right = (maxRange * (Number(max) - val[1])) / (Number(max) - Number(min));
// 因为要计算点相对于线的绝对定位,所以要取整条线的长度而非可滑动的范围
this.setLineStyle(left, right);
} else {
this.setSingleBarWidth(val as number);
}
}

triggerValue(value?: SliderValue) {
if (this.preval === value) return;
this.preval = value;
Expand All @@ -139,7 +142,7 @@ export default class Slider extends SuperComponent {

// 基本样式未初始化,等待初始化后在改变数据。
if (this.data.maxRange === 0) {
this.getInitialStyle().then(setValueAndTrigger);
this.init().then(setValueAndTrigger);
return;
}

Expand Down Expand Up @@ -189,7 +192,7 @@ export default class Slider extends SuperComponent {
});
}

async getInitialStyle() {
async init() {
const line: boundingClientRect = await getRect(this, '#sliderLine');
const { blockSize } = this.data;
const { theme } = this.properties;
Expand All @@ -198,6 +201,8 @@ export default class Slider extends SuperComponent {
let initialLeft = line.left;
let initialRight = line.right;

if (initialLeft === 0 && initialRight === 0) return;

if ((theme as any) === 'capsule') {
maxRange = maxRange - Number(blockSize) - 6; // 6 是边框宽度
initialLeft -= halfBlock;
Expand Down