MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useDefaultProps

Function useDefaultProps

packages/react-stately/src/datepicker/utils.ts:285–314  ·  view source on GitHub ↗
(
  v: DateValue | null,
  granularity: Granularity | undefined
)

Source from the content-addressed store, hash-verified

283}
284
285export 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}

Callers 3

useDateRangePickerStateFunction · 0.90
useDatePickerStateFunction · 0.90
useDateFieldStateFunction · 0.90

Calls 1

toStringMethod · 0.65

Tested by

no test coverage detected