UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 2.11 kB
{"version":3,"file":"use-format-date-time.cjs","names":["i18nCache","useI18n","options"],"sources":["../../../../src/components/format/use-format-date-time.ts"],"sourcesContent":["\"use client\"\n\nimport type { Locale } from \"../../providers/i18n-provider\"\nimport type { AnyString } from \"../../utils\"\nimport { useCallback, useRef } from \"react\"\nimport { i18nCache, useI18n } from \"../../providers/i18n-provider\"\n\nexport const getFormatDateTime = i18nCache(Intl.DateTimeFormat)\n\nexport interface FormatDateTimeOptions extends Intl.DateTimeFormatOptions {\n /**\n * The locale string to use for formatting.\n *\n * @default 'en-US'\n */\n locale?: AnyString | Locale\n}\n\n/**\n * `useFormatDateTime` is a custom hook that returns the formatted date time.\n *\n * @see https://yamada-ui.com/docs/hooks/use-format-date-time\n */\nexport const useFormatDateTime = (\n value: Date,\n options?: FormatDateTimeOptions,\n) => {\n const dateTimeFormat = useDateTimeFormat(options)\n\n return dateTimeFormat(value)\n}\n\nexport type UseFormatDateTimeReturn = ReturnType<typeof useFormatDateTime>\n\nexport const useDateTimeFormat = ({\n locale,\n ...options\n}: FormatDateTimeOptions = {}) => {\n const { locale: defaultLocale } = useI18n()\n const memoizedOptions = useRef(options)\n\n locale ??= defaultLocale\n\n const dateTimeFormat = useCallback(\n (value: Date, options?: Intl.DateTimeFormatOptions) => {\n const formatter = getFormatDateTime(locale, {\n ...memoizedOptions.current,\n ...options,\n })\n\n return formatter.format(value)\n },\n [locale],\n )\n\n return dateTimeFormat\n}\n\nexport type UseDateTimeFormatReturn = ReturnType<typeof useDateTimeFormat>\n"],"mappings":";;;;;;;;;;AAOA,MAAa,oBAAoBA,6BAAU,KAAK,eAAe;;;;;;AAgB/D,MAAa,qBACX,OACA,YACG;AAGH,QAFuB,kBAAkB,QAAQ,CAE3B,MAAM;;AAK9B,MAAa,qBAAqB,EAChC,OACA,GAAG,YACsB,EAAE,KAAK;CAChC,MAAM,EAAE,QAAQ,kBAAkBC,+BAAS;CAC3C,MAAM,oCAAyB,QAAQ;AAEvC,YAAW;AAcX,gCAXG,OAAa,cAAyC;AAMrD,SALkB,kBAAkB,QAAQ;GAC1C,GAAG,gBAAgB;GACnB,GAAGC;GACJ,CAAC,CAEe,OAAO,MAAM;IAEhC,CAAC,OAAO,CACT"}