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(picker): append to body #8702

Merged
merged 8 commits into from
Apr 27, 2021
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
1 change: 0 additions & 1 deletion packages/taro-components/__tests__/audio.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './polyfill'
import React from 'react'
import { Audio } from '../h5/react'
import { mount } from './test-tools'
Expand Down
109 changes: 48 additions & 61 deletions packages/taro-components/__tests__/picker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Picker } from '../h5/react'
import { mount } from './test-tools'
import { waitForChange } from './utils'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const h = React.createElement

describe('Picker', () => {
Expand All @@ -25,10 +24,10 @@ describe('Picker', () => {
scratch = null
})

async function srcollToNextItem (wrapper, columnIndex = 0) {
const content = wrapper.find('.weui-picker__content')
const column = wrapper.findAll('.weui-picker__group')[columnIndex]
const cur = wrapper.find('.weui-picker__indicator')
async function srcollToNextItem (columnIndex = 0) {
const content = document.querySelector('.weui-picker__content')
const column = document.querySelectorAll('.weui-picker__group')[columnIndex]
const cur = document.querySelector('.weui-picker__indicator')
const curRect = cur.getBoundingClientRect()
const startY = curRect.top + curRect.height / 2
const endY = curRect.top - curRect.height / 2
Expand Down Expand Up @@ -72,34 +71,30 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const mask = wrapper.find('.weui-mask')
const slider = wrapper.find('.weui-picker')
const [cancel] = wrapper.findAll('.weui-picker__action')
await mount(app, scratch)
const overlay = document.querySelector('.weui-picker__overlay')
const mask = document.querySelector('.weui-mask')
const cancel = document.querySelectorAll('.weui-picker__action')[0]

assert(mask.style.display === 'none')
assert(slider.style.display === 'none')
assert(overlay.style.display === 'none')

// 成功打开
domRef.current.click()
await waitForChange(slider)
assert(mask.style.display !== 'none')
assert(slider.style.display !== 'none')
await waitForChange(overlay)
assert(overlay.style.display !== 'none')

// 点击蒙层可以关闭
mask.click()
await waitForChange(slider)
assert(mask.style.display === 'none')
assert(slider.style.display === 'none')
await waitForChange(overlay)
assert(overlay.style.display === 'none')
assert(onCancel.callCount === 1)

// 点击取消按钮可以关闭
domRef.current.click()
await waitForChange(slider)
await waitForChange(overlay)
cancel.click()
await waitForChange(slider)
assert(mask.style.display === 'none')
assert(slider.style.display === 'none')
await waitForChange(overlay)
assert(overlay.style.display === 'none')
assert(onCancel.callCount === 2)
})

Expand All @@ -118,17 +113,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

confirm.click()

Expand Down Expand Up @@ -170,17 +164,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

assert(onColumnChange.calledOnceWith({ column: 0, value: 1 }))

Expand All @@ -204,17 +197,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

confirm.click()

Expand All @@ -236,17 +228,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper, 1)
await srcollToNextItem(1)

confirm.click()

Expand All @@ -272,17 +263,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

confirm.click()

Expand All @@ -308,17 +298,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === selected)

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper, 1)
await srcollToNextItem(1)

confirm.click()

Expand All @@ -341,17 +330,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === '2016-09')

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

confirm.click()

Expand All @@ -374,17 +362,16 @@ describe('Picker', () => {
<div ref={domRef}>Picker</div>
</Picker>
)
const wrapper = await mount(app, scratch)
const { node } = wrapper
const slider = wrapper.find('.weui-picker')
const [, confirm] = wrapper.findAll('.weui-picker__action')
const { node } = await mount(app, scratch)
const slider = document.querySelector('.weui-picker')
const confirm = document.querySelectorAll('.weui-picker__action')[1]

assert(node.value === '2016')

domRef.current.click()
await waitForChange(slider)

await srcollToNextItem(wrapper)
await srcollToNextItem()

confirm.click()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import '../dist/taro-components/taro-components.css'

// eslint-disable-next-line no-undef
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000
let applied = false
if (!applied) {
// 此文件只要 import 一次即可
applyPolyfills().then(() => {
defineCustomElements(window)
applied = true
})
}

applyPolyfills().then(() => {
defineCustomElements(window)
})
1 change: 0 additions & 1 deletion packages/taro-components/__tests__/video.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './polyfill'
import React from 'react'
import * as assert from 'assert'
// import simulant from 'simulant'
Expand Down
8 changes: 4 additions & 4 deletions packages/taro-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"build-h5": "babel -d dist-h5 h5",
"build": "npm run build-h5 & stencil build --config stencil.config.ts",
"dev": "stencil build --config stencil.config.ts --watch",
"test": "karmatic --files '__tests__/**.spec.js' --coverage false",
"test:ci": "karmatic --files '__tests__/**.spec.js' --coverage false",
"test:debug": "karmatic debug --files '__tests__/**.spec.js' --coverage false",
"test:coverage": "karmatic --files '__tests__/**.spec.js'"
"test": "karmatic --files '__tests__/setup.js,__tests__/**.spec.js' --coverage false",
"test:ci": "karmatic --files '__tests__/setup.js,__tests__/**.spec.js' --coverage false",
"test:debug": "karmatic debug --files '__tests__/setup.js,__tests__/**.spec.js' --coverage false",
"test:coverage": "karmatic --files '__tests__/setup.js,__tests__/**.spec.js'"
},
"repository": {
"type": "git",
Expand Down
39 changes: 28 additions & 11 deletions packages/taro-components/src/components/picker/picker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Component, h, ComponentInterface, Prop, Event, EventEmitter, Host, State, Watch, Element } from '@stencil/core'
import classNames from 'classnames'
import {
Expand Down Expand Up @@ -36,6 +35,7 @@ export interface PickerDate {
export class Picker implements ComponentInterface {
private index: number[] = []
private pickerDate: PickerDate
private overlay?: HTMLElement

@Element() el: HTMLElement

Expand Down Expand Up @@ -78,6 +78,16 @@ export class Picker implements ComponentInterface {
set: val => (this.value = val),
configurable: true
})

if (this.overlay) {
document.body.appendChild(this.overlay)
}
}

disconnectedCallback () {
if (this.overlay) {
this.overlay.parentNode?.removeChild(this.overlay)
}
}

@Watch('mode')
Expand Down Expand Up @@ -476,19 +486,26 @@ export class Picker implements ComponentInterface {
<Host>
<div onClick={this.showPicker}>
<slot />
<input type='hidden' name={name} value={formatValue(this.pickerValue)} />
</div>
<div style={shouldDivHidden} class={clsMask} onClick={this.handleCancel} />
<div style={shouldDivHidden} class={clsSlider}>
<div class='weui-picker__hd'>
<div class='weui-picker__action' onClick={this.handleCancel}>
取消
</div>
<div class='weui-picker__action' onClick={this.handleChange}>
确定
<div
class='weui-picker__overlay'
style={shouldDivHidden}
ref={el => { this.overlay = el }}
>
<div class={clsMask} onClick={this.handleCancel} />
<div class={clsSlider}>
<div class='weui-picker__hd'>
<div class='weui-picker__action' onClick={this.handleCancel}>
取消
</div>
<div class='weui-picker__action' onClick={this.handleChange}>
确定
</div>
</div>
<div class='weui-picker__bd'>{pickerGroup}</div>
<input type='hidden' name={name} value={formatValue(this.pickerValue)} />
</div>
<div class='weui-picker__bd'>{pickerGroup}</div>
<input type='hidden' name={name} value={formatValue(this.pickerValue)} />
</div>
</Host>
)
Expand Down