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.06 kB
{"version":3,"file":"use-format-number.cjs","names":["i18nCache","useI18n","options"],"sources":["../../../../src/components/format/use-format-number.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 getFormatNumber = i18nCache(Intl.NumberFormat)\n\nexport interface FormatNumberOptions extends Intl.NumberFormatOptions {\n /**\n * The locale string to use for formatting.\n *\n * @default 'en-US'\n */\n locale?: AnyString | Locale\n}\n\n/**\n * `useFormatNumber` is a custom hook that returns the formatted number.\n *\n * @see https://yamada-ui.com/docs/hooks/use-format-number\n */\nexport const useFormatNumber = (\n value: number,\n options?: FormatNumberOptions,\n) => {\n const numberFormat = useNumberFormat(options)\n\n return numberFormat(value)\n}\n\nexport type UseFormatNumberReturn = ReturnType<typeof useFormatNumber>\n\nexport const useNumberFormat = ({\n locale,\n ...options\n}: FormatNumberOptions = {}) => {\n const { locale: defaultLocale } = useI18n()\n const memoizedOptions = useRef(options)\n\n locale ??= defaultLocale\n\n const numberFormat = useCallback(\n (value: number, options?: Intl.NumberFormatOptions) => {\n const formatter = getFormatNumber(locale, {\n ...memoizedOptions.current,\n ...options,\n })\n\n return formatter.format(value)\n },\n [locale],\n )\n\n return numberFormat\n}\n\nexport type UseNumberFormatReturn = ReturnType<typeof useNumberFormat>\n"],"mappings":";;;;;;;;;;AAOA,MAAa,kBAAkBA,6BAAU,KAAK,aAAa;;;;;;AAgB3D,MAAa,mBACX,OACA,YACG;AAGH,QAFqB,gBAAgB,QAAQ,CAEzB,MAAM;;AAK5B,MAAa,mBAAmB,EAC9B,OACA,GAAG,YACoB,EAAE,KAAK;CAC9B,MAAM,EAAE,QAAQ,kBAAkBC,+BAAS;CAC3C,MAAM,oCAAyB,QAAQ;AAEvC,YAAW;AAcX,gCAXG,OAAe,cAAuC;AAMrD,SALkB,gBAAgB,QAAQ;GACxC,GAAG,gBAAgB;GACnB,GAAGC;GACJ,CAAC,CAEe,OAAO,MAAM;IAEhC,CAAC,OAAO,CACT"}