Skip to content

Commit

Permalink
fix issue #467 (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
inter-action authored and e1emeb0t committed Jul 6, 2017
1 parent bc35f85 commit fa99067
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
23 changes: 12 additions & 11 deletions libs/internal/EventRegister.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ const registerMap = window.__registerMap = window.__registerMap || {

const not_null = (t) => (t != null)

const hasRegistered = ({id}) => {
const hasRegistered = ({ id }) => {
return not_null(registerMap.ids[id])
}

const cleanRegister = ({id}) => {
delete registerMap.ids[id]
const cleanRegister = (props) => {
const { target, eventName, func, isUseCapture, id } = props
if (hasRegistered(this.cached)) {
target.removeEventListener(eventName, func, isUseCapture);
delete registerMap.ids[id]
}
}

const doRegister = ({id}) => {
const doRegister = (props) => {
let { id, eventName, func, isUseCapture } = props
registerMap.ids[id] = id
document.addEventListener(eventName, func, isUseCapture)
}

/**
Expand All @@ -26,24 +32,19 @@ const doRegister = ({id}) => {
export default class EventRegister extends Component {

componentDidMount() {
let {target, eventName, func, isUseCapture, id} = this.props
let { eventName, id } = this.props
eventName = eventName.toLowerCase()
eventName = /^on/.test(eventName) ? eventName.substring(2) : eventName
this.cached = Object.assign({}, this.props, { eventName })

require_condition(typeof id === 'string', 'id prop is required')
require_condition(!hasRegistered(this.cached), `id: ${id} has been registered`)

target.addEventListener(eventName, func, isUseCapture)
doRegister(this.cached)
}

componentWillUnmount() {
const {target, eventName, func, isUseCapture} = this.cached
if (hasRegistered(this.cached)) {
target.removeEventListener(eventName, func, isUseCapture)
cleanRegister(this.cached)
}
cleanRegister(this.cached)
}

render() {
Expand Down
16 changes: 7 additions & 9 deletions src/date-picker/BasePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ReactDOM from 'react-dom';
import { PropTypes, Component } from '../../libs';
import { EventRegister } from '../../libs/internal'


import Input from '../input'
import { PLACEMENT_MAP, HAVE_TRIGGER_TYPES, TYPE_VALUE_RESOLVER_MAP, DEFAULT_FORMATS } from './constants'
import { Errors, require_condition, IDGenerator } from '../../libs/utils';
Expand Down Expand Up @@ -104,7 +105,7 @@ export default class BasePicker extends Component {
}

// (string) => Date | null
parseDate(dateStr: string) : NullableDate{
parseDate(dateStr: string): NullableDate{
if (!dateStr) return null
const type = this.type;
const parser = (
Expand Down Expand Up @@ -178,7 +179,7 @@ export default class BasePicker extends Component {

// (state, props)=>ReactElement
pickerPanel(state: any, props: $Subtype<BasePickerProps>) {
throw new Errors.MethodImplementationRequiredError()
throw new Errors.MethodImplementationRequiredError(props)
}

isDateValid(date: ValidDateType) {
Expand All @@ -201,12 +202,12 @@ export default class BasePicker extends Component {
return true
}

handleClickOutside() {
handleClickOutside(evt: SyntheticEvent) {
const {value, pickerVisible} = this.state
if (!this.isInputFocus && !pickerVisible) {
return
}

if (this.domRoot.contains(evt.target)) return
if (this.isDateValid(value)) {
this.setState({ pickerVisible: false })
this.props.onChange(value)
Expand Down Expand Up @@ -278,11 +279,8 @@ export default class BasePicker extends Component {
'is-active': pickerVisible,
'is-filled': !!value
})}
onClick={(evt) => {
evt.stopPropagation()
evt.nativeEvent.stopImmediatePropagation();
return false
} }

ref={v=>this.domRoot=v}
>

<EventRegister
Expand Down

0 comments on commit fa99067

Please sign in to comment.