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

0 is not acceptable by value #1724

Closed
jiggum opened this issue Apr 29, 2020 · 14 comments
Closed

0 is not acceptable by value #1724

jiggum opened this issue Apr 29, 2020 · 14 comments
Labels
bug 🐛 Something isn't working

Comments

@jiggum
Copy link

jiggum commented Apr 29, 2020

Environment

Tech Version
@material-ui/pickers 3.2.10
material-ui 4.9.2
TypeScript 3.6.3
React 16.10.2
Peer library [email protected]

Steps to reproduce

Using TimePicker with value of 0

Expected behavior

Picker should use value of 0

Actual behavior

Using time of now

Description

I think the below line is omitting the value of 0
https://github.com/mui-org/material-ui-pickers/blob/next/lib/src/_helpers/date-utils.ts#L106

const parsedValue = utils.date(value || defaultHighlight || now);
@dmtrKovalenko
Copy link
Member

It should not be acceptable. If you need 00:00 time set value={startOfDay(new Date())}

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 29, 2020

I wonder if we have a prop-type warning in this case. @jiggum Do you have a live reproduction?

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 29, 2020

It doesn't. @dmtrKovalenko should we have a custom prop-type to detect this class of issues? Based on the Material-UI stats, 90% of the developers are using the JavaScript demos, in the user-survey, it's 55% without a typing system.

@dmtrKovalenko
Copy link
Member

dmtrKovalenko commented Apr 29, 2020

I wonder that will be to complicated. Because we accept any value that is parsable by any date library. So any value that acceptable by any library is acceptable by us https://dev.material-ui-pickers.dev/getting-started/parsing

@oliviertassinari
Copy link
Member

@dmtrKovalenko Ok, it's good to know, so maybe there is no DX improvement opportunity 🤷‍♂️.

@jiggum
Copy link
Author

jiggum commented Apr 29, 2020

@dmtrKovalenko Thank you for example. Actually we using addDays(value, 1) when the value is 0.
Can I know the reason why 0 is should not be acceptable?
In Js's Date object, 0 means Unix time's base timestamp. So it had been a little confused to me when the actual input value is current time rather than 0.

@oliviertassinari
Copy link
Member

@jiggum Could you provide a reproduction? What date engine are you using? How does this date engine parse 0?

@jiggum
Copy link
Author

jiggum commented Apr 29, 2020

@oliviertassinari We only depend on typescript's prop typing and there's no type warning.

What date engine are you using?

We using date-fns.
When we parse '00:00:00' at UTC timezone, parse(time, 'HH:mm:ss', 0).valueOf() is return 0

the third parameter(0) is refrenceDate if we set that value to another date, above return another number which is not 0.

In formatting part, we using format(time, 'HH:mm:ss') and it return 00:00:00

Here is that case
https://codesandbox.io/s/date-fns-v2-ndpjq

@dmtrKovalenko
Copy link
Member

Probably it’s a pitfall if || operator and we should use ?? instead. But it is adding some overhead in runtime

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 29, 2020

@dmtrKovalenko We have started using ?? in mui/material-ui#20715. But != null would be as good, it's the approach we use most of the time.

@dmtrKovalenko
Copy link
Member

Yes we are using it already. But it adds a bit bundle size overhead.

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 29, 2020

I think that if @jiggum is able to provide a valid reproduction, we could spend more time on it.

Should we consider a version like

export function parsePickerInputValue(
  now: MaterialUiPickersDate,
  utils: MuiPickersAdapter,
  { value, defaultHighlight }: Pick<BasePickerProps, 'value' | 'defaultHighlight'>
): MaterialUiPickersDate | null {
  return [value, defaultHighlight, now].reduce((acc, item) => {
    const candidate = utils.date(item);

    if (!acc && utils.isValid(candidate)) {
      return candidate;
    }

    return acc;
  }, null);
}

instead of

export function parsePickerInputValue(
  now: MaterialUiPickersDate,
  utils: MuiPickersAdapter,
  { value, defaultHighlight }: Pick<BasePickerProps, 'value' | 'defaultHighlight'>
): MaterialUiPickersDate | null {
  const parsedValue = utils.date(value || defaultHighlight || now);

  return parsedValue && utils.isValid(parsedValue) ? parsedValue : now;
}

?

@jiggum
Copy link
Author

jiggum commented Apr 29, 2020

@oliviertassinari Is reproduction means something like this?
https://codesandbox.io/s/material-ui-date-picker-dyyhp

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 29, 2020

@jiggum Almost, here is what I was looking for https://codesandbox.io/s/material-ui-date-picker-gk7d0 (v4.0.0-alpha.5) :).

The input value is correct, but the displayed value in the popup is wrong.

@oliviertassinari oliviertassinari added the bug 🐛 Something isn't working label Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants