react-native-picker icon indicating copy to clipboard operation
react-native-picker copied to clipboard

example中的时间选择,ios中即使日期改变为2011-02-28,再点击onPickerConfirm后还是会返回2011-02-31

Open shenshenwlb opened this issue 6 years ago • 5 comments

showTimePicker = () => { const years = [] const time = timeGroup

for (let i = 1; i < 30; i++) {
  years.push(i + 2010 + '年')
}

let pickerData = []
let selectedValue = []
const date = new Date()

if (this.props.showTime) {
  pickerData = [years, monthGroup, dayGroup, time]
  selectedValue = [
    date.getFullYear(),
    date.getMonth() + 1,
    date.getDate(),
    date.getTime()
  ]
} else {
  pickerData = [years, monthGroup, dayGroup]
  selectedValue = [
    date.getFullYear(),
    date.getMonth() + 1,
    date.getDate()
  ]
}
Picker.init({
  pickerData,
  selectedValue,
  pickerConfirmBtnText: '确定',
  pickerCancelBtnText: '取消',
  pickerToolBarBg: backGroundColor,
  pickerBg: backGroundColor,
  pickerTitleText: this.props.title,
  pickerTextEllipsisLen: 20,
  onPickerConfirm: pickedValue => {
    console.log('xxx pickedValue',pickedValue)
    this.props.onRequestClose()
    this.props.onRequestSave(pickedValue)
  },
  onPickerCancel: pickedValue => {
    this.props.onRequestClose()
  },
  onPickerSelect: pickedValue => {
    const targetValue = [...pickedValue]
    if (Number.parseInt(targetValue[1]) === 2) {
      if (Number.parseInt(targetValue[0]) % 4 === 0 && Number.parseInt(targetValue[2]) > 29) {
        targetValue[2] = 29 + '日'
      } else if (Number.parseInt(targetValue[0]) % 4 !== 0 && Number.parseInt(targetValue[2]) > 28) {
        targetValue[2] = 28 + '日'
      }
    } else if (Number.parseInt(targetValue[1]) in { 4: 1, 6: 1, 9: 1, 11: 1 } && Number.parseInt(targetValue[2]) > 30) {
      targetValue[2] = 30 + '日'

    }
    // forbidden some value such as some 2.29, 4.31, 6.31...
    if (JSON.stringify(targetValue) !== JSON.stringify(pickedValue)) {
      // android will return String all the time,but we put Number into picker at first
      // so we need to convert them to Number again
      targetValue.map((v, k) => {
        if (k !== 3) {
          targetValue[k] = v
        }
      })
      Picker.select(targetValue)
      pickedValue = targetValue
      console.log('xxx pickedValue',pickedValue)
    }
  }
})
Picker.show()

}

shenshenwlb avatar Jul 09 '18 06:07 shenshenwlb

i also found that! onPickerConfirm and onPickerSelect are not synchronous .

sunny635533 avatar Oct 29 '18 09:10 sunny635533

@shenshenwlb I found a solution to solve that ,just add a handler postDelay add confirmTV.setOnClickListener method,like: // ======= confirmTV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (curStatus) { case 0: returnData = pickerViewAlone.getSelectedData(); break; case 1: returnData = pickerViewLinkage.getSelectedData(); break; }

                // ======= fix confirm click bug
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        commonEvent(EVENT_KEY_CONFIRM);
                    }
                }, 500);
                hide();
            }
        });

sunny635533 avatar Oct 30 '18 07:10 sunny635533

@sunny635533 在哪里加呀

liuliu66666 avatar Nov 09 '18 02:11 liuliu66666

@liuliu66666 liu就在confirmTV.setOnClickListener这个方法里添加啊,只需要延时下发送事件就OK了

sunny635533 avatar Nov 09 '18 03:11 sunny635533

我也遇到这个问题了

EliteCRM avatar Jun 01 '20 06:06 EliteCRM