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

[Bug] Picker value shouldn't strict equality #6540

Open
Yueyanc opened this issue Feb 5, 2024 · 0 comments
Open

[Bug] Picker value shouldn't strict equality #6540

Yueyanc opened this issue Feb 5, 2024 · 0 comments
Labels

Comments

@Yueyanc
Copy link

Yueyanc commented Feb 5, 2024

Version of antd-mobile

5.34.0

Operating system and its version

Others

Browser and its version

No response

Sandbox to reproduce

No response

What happened?

由于time的初始值设为了['2024','xx','xx','xx'],代码中!column.some(item => item.value === value)用了严格等于,导致触发了onChange.
考虑换成==

  useIsomorphicLayoutEffect(() => {
    if (column.length === 0) {
      if (value !== null) {
        onSelect(null);
      }
    } else {
      if (!column.some(item => item.value === value)) { // 这里
        const firstItem = column[0];
        onSelect(firstItem.value);
      }
    }
  }, [column, value]);

Relevant log output

import React, { useState, useMemo } from "react";
import { Button, PickerView, Popup } from "antd-mobile";
import _ from "lodash";
import dayjs from "dayjs";
const deafultTimeColumns = [
  _.range(2000, 2024).map((item) => ({ label: `${item}年`, value: item })),
  _.range(1, 13).map((item) => ({ label: `${item}月`, value: item })),
  _.range(1, 32).map((item) => ({ label: `${item}日`, value: item })),
  _.range(0, 24).map((item) => ({ label: `${item}时`, value: item })),
];
export default function App() {
  const [open, setOpen] = useState(false);
  const [time, setTime] = useState(dayjs().format("YYYY-MM-DD-HH").split("-"));
  const timeColumns = useMemo(() => {
    const daysInMonth = dayjs(`${time[0]}-${time[1]}`).daysInMonth();
    const newColumns = [...deafultTimeColumns];
    newColumns[2] = _.range(1, daysInMonth + 1).map((item) => ({
      label: `${item}日`,
      value: item,
    }));
    return newColumns;
  }, [time]);
  return (
    <div className="App">
      <h1>Hello Antd Mobile</h1>
      <Button
        onClick={() => {
          setOpen(true);
        }}
      >
        打开
      </Button>
      <Popup visible={open}>
        <PickerView
          columns={timeColumns}
          value={time}
          onChange={(val, extend) => {
            setTime(val);
            console.log("onChange", val, extend.items);
          }}
        />
      </Popup>
    </div>
  );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant