( v: DateValue | null, granularity: Granularity | undefined )
| 283 | } |
| 284 | |
| 285 | export function useDefaultProps( |
| 286 | v: DateValue | null, |
| 287 | granularity: Granularity | undefined |
| 288 | ): [Granularity, string | undefined] { |
| 289 | // Compute default granularity and time zone from the value. If the value becomes null, keep the last values. |
| 290 | let defaultTimeZone = v && 'timeZone' in v ? v.timeZone : undefined; |
| 291 | let defaultGranularity: Granularity = v && 'minute' in v ? 'minute' : 'day'; |
| 292 | |
| 293 | // props.granularity must actually exist in the value if one is provided. |
| 294 | if (v && granularity && !(granularity in v)) { |
| 295 | throw new Error('Invalid granularity ' + granularity + ' for value ' + v.toString()); |
| 296 | } |
| 297 | |
| 298 | let [lastValue, setLastValue] = useState<[Granularity, string | undefined]>([ |
| 299 | defaultGranularity, |
| 300 | defaultTimeZone |
| 301 | ]); |
| 302 | |
| 303 | // If the granularity or time zone changed, update the last value. |
| 304 | if (v && (lastValue[0] !== defaultGranularity || lastValue[1] !== defaultTimeZone)) { |
| 305 | setLastValue([defaultGranularity, defaultTimeZone]); |
| 306 | } |
| 307 | |
| 308 | if (!granularity) { |
| 309 | granularity = v ? defaultGranularity : lastValue[0]; |
| 310 | } |
| 311 | |
| 312 | let timeZone = v ? defaultTimeZone : lastValue[1]; |
| 313 | return [granularity, timeZone]; |
| 314 | } |
no test coverage detected