@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
1 lines • 8.76 kB
Source Map (JSON)
{"version":3,"file":"rating.cjs","names":["createSlotComponent","ratingStyle","useRating","RatingContext","styled","useRatingContext","useRatingItem","StarIcon","varAttr"],"sources":["../../../../src/components/rating/rating.tsx"],"sourcesContent":["\"use client\"\n\nimport type { CSSProps, HTMLStyledProps, ThemeProps } from \"../../core\"\nimport type { FunctionOrValue, Merge, ReactNodeOrFunction } from \"../../utils\"\nimport type { RatingStyle } from \"./rating.style\"\nimport type { UseRatingItemProps, UseRatingProps } from \"./use-rating\"\nimport { useMemo } from \"react\"\nimport { createSlotComponent, styled, varAttr } from \"../../core\"\nimport { runIfFn } from \"../../utils\"\nimport { StarIcon } from \"../icon\"\nimport { ratingStyle } from \"./rating.style\"\nimport {\n RatingContext,\n useRating,\n useRatingContext,\n useRatingItem,\n} from \"./use-rating\"\n\ninterface ComponentContext\n extends Pick<\n RatingProps,\n | \"color\"\n | \"emptyIcon\"\n | \"filledIcon\"\n | \"groupProps\"\n | \"iconProps\"\n | \"inputProps\"\n | \"itemProps\"\n > {}\n\nexport interface RatingProps\n extends Merge<Omit<HTMLStyledProps, \"children\" | \"color\">, UseRatingProps>,\n ThemeProps<RatingStyle> {\n /**\n * The color of the filled icons.\n */\n color?: FunctionOrValue<number, CSSProps[\"color\"]>\n /**\n * The empty icon for the rating.\n */\n emptyIcon?: ReactNodeOrFunction<number>\n /**\n * The filled icon for the rating.\n */\n filledIcon?: ReactNodeOrFunction<number>\n /**\n * Props for the rating group.\n */\n groupProps?: FunctionOrValue<number, RatingGroupProps>\n /**\n * Props for the rating item.\n */\n iconProps?: FunctionOrValue<number, RatingIconProps>\n /**\n * Props for the input element.\n */\n inputProps?: FunctionOrValue<number, HTMLStyledProps<\"input\">>\n /**\n * Props for the rating item.\n */\n itemProps?: FunctionOrValue<number, RatingItemProps>\n}\n\nconst {\n ComponentContext,\n PropsContext: RatingPropsContext,\n useComponentContext,\n usePropsContext: useRatingPropsContext,\n withContext,\n withProvider,\n} = createSlotComponent<RatingProps, RatingStyle, ComponentContext>(\n \"rating\",\n ratingStyle,\n)\n\nexport { RatingPropsContext, useRatingPropsContext }\n\n/**\n * `Rating` is a component used to allow users to provide ratings.\n *\n * @see https://yamada-ui.com/docs/components/rating\n */\nexport const Rating = withProvider<\"div\", RatingProps>(\n ({\n color,\n emptyIcon,\n filledIcon,\n groupProps,\n iconProps,\n inputProps,\n itemProps,\n ...rest\n }) => {\n const {\n id,\n name,\n count,\n decimal,\n disabled,\n displayValue,\n fractions,\n highlightSelectedOnly,\n hoveredValue,\n interactive,\n outsideRef,\n readOnly,\n required,\n roundedValue,\n setHoveredValue,\n setValue,\n value,\n ariaProps,\n dataProps,\n eventProps,\n getRootProps,\n } = useRating(rest)\n const ratingContext = useMemo(\n () => ({\n id,\n name,\n count,\n decimal,\n disabled,\n displayValue,\n fractions,\n highlightSelectedOnly,\n hoveredValue,\n interactive,\n outsideRef,\n readOnly,\n required,\n roundedValue,\n setHoveredValue,\n setValue,\n value,\n ariaProps,\n dataProps,\n eventProps,\n }),\n [\n id,\n name,\n count,\n decimal,\n disabled,\n fractions,\n highlightSelectedOnly,\n hoveredValue,\n interactive,\n displayValue,\n outsideRef,\n readOnly,\n required,\n roundedValue,\n setHoveredValue,\n setValue,\n value,\n ariaProps,\n dataProps,\n eventProps,\n ],\n )\n const componentContext = useMemo(\n () => ({\n color,\n emptyIcon,\n filledIcon,\n groupProps,\n iconProps,\n inputProps,\n itemProps,\n }),\n [\n color,\n emptyIcon,\n filledIcon,\n groupProps,\n iconProps,\n inputProps,\n itemProps,\n ],\n )\n\n return (\n <RatingContext value={ratingContext}>\n <ComponentContext value={componentContext}>\n <styled.div {...getRootProps()}>\n {Array.from({ length: count }).map((_, index) => (\n <RatingGroup key={index} value={index + 1} />\n ))}\n </styled.div>\n </ComponentContext>\n </RatingContext>\n )\n },\n \"root\",\n)()\n\ninterface RatingGroupProps extends HTMLStyledProps {\n value: number\n}\n\nconst RatingGroup = withContext<\"div\", RatingGroupProps>(\n ({ value, ...rest }) => {\n const { fractions } = useRatingContext()\n const { groupProps } = useComponentContext()\n const count = useMemo(\n () => (value === 1 ? fractions + 1 : fractions),\n [value, fractions],\n )\n\n return (\n <styled.div {...runIfFn(groupProps, value)} {...rest}>\n {Array.from({ length: count }).map((_, index) => (\n <RatingItem key={index} groupValue={value} index={index} />\n ))}\n </styled.div>\n )\n },\n \"group\",\n)()\n\ninterface RatingItemProps\n extends HTMLStyledProps<\"label\">,\n UseRatingItemProps {}\n\nconst RatingItem = withContext<\"label\", RatingItemProps>((props) => {\n const {\n active,\n filled,\n fractionValue,\n groupValue,\n getInputProps,\n getLabelProps,\n } = useRatingItem(props)\n const {\n emptyIcon = <StarIcon fill=\"currentColor\" strokeColor=\"currentColor\" />,\n filledIcon = <StarIcon fill=\"currentColor\" strokeColor=\"currentColor\" />,\n iconProps,\n inputProps,\n itemProps,\n } = useComponentContext()\n const clipPath =\n fractionValue !== 1\n ? `inset(0 ${active ? 100 - fractionValue * 100 : 100}% 0 0)`\n : undefined\n\n return (\n <styled.label {...getLabelProps(runIfFn(itemProps, groupValue))}>\n <styled.input {...getInputProps(runIfFn(inputProps, groupValue))} />\n\n <RatingIcon\n {...{ \"--clip-path\": clipPath }}\n {...runIfFn(iconProps, groupValue)}\n >\n {filled\n ? runIfFn(filledIcon, groupValue)\n : runIfFn(emptyIcon, groupValue)}\n </RatingIcon>\n </styled.label>\n )\n}, \"item\")(undefined, ({ groupValue, ...rest }) => {\n const { color } = useComponentContext()\n\n return {\n \"--filled-color\": varAttr(runIfFn(color, groupValue), \"colors\"),\n groupValue,\n ...rest,\n }\n})\n\ninterface RatingIconProps extends HTMLStyledProps {}\n\nconst RatingIcon = withContext<\"div\", RatingIconProps>(\"div\", \"icon\")()\n"],"mappings":";;;;;;;;;;;;;;;;;AA+DA,MAAM,EACJ,kBACA,cAAc,oBACd,qBACA,iBAAiB,uBACjB,aACA,iBACEA,6CACF,UACAC,iCACD;;;;;;AASD,MAAa,SAAS,cACnB,EACC,OACA,WACA,YACA,YACA,WACA,YACA,UACA,GAAG,WACC;CACJ,MAAM,EACJ,IACA,MACA,OACA,SACA,UACA,cACA,WACA,uBACA,cACA,aACA,YACA,UACA,UACA,cACA,iBACA,UACA,OACA,WACA,WACA,YACA,iBACEC,6BAAU,KAAK;AAoEnB,QACE,2CAACC;EAAc,iCAnER;GACL;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD,GACD;GACE;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACA;GACD,CACF;YAwBG,2CAAC;GAAiB,iCAtBb;IACL;IACA;IACA;IACA;IACA;IACA;IACA;IACD,GACD;IACE;IACA;IACA;IACA;IACA;IACA;IACA;IACD,CACF;aAKK,2CAACC,uBAAO;IAAI,GAAI,cAAc;cAC3B,MAAM,KAAK,EAAE,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,UACrC,2CAAC,eAAwB,OAAO,QAAQ,KAAtB,MAA2B,CAC7C;KACS;IACI;GACL;GAGpB,OACD,EAAE;AAMH,MAAM,cAAc,aACjB,EAAE,MAAO,GAAG,WAAW;CACtB,MAAM,EAAE,cAAcC,qCAAkB;CACxC,MAAM,EAAE,eAAe,qBAAqB;CAC5C,MAAM,iCACG,UAAU,IAAI,YAAY,IAAI,WACrC,CAAC,OAAO,UAAU,CACnB;AAED,QACE,2CAACD,uBAAO;EAAI,kDAAY,YAAY,MAAM;EAAE,GAAI;YAC7C,MAAM,KAAK,EAAE,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,UACrC,2CAAC;GAAuB,YAAY;GAAc;KAAjC,MAA0C,CAC3D;GACS;GAGjB,QACD,EAAE;AAMH,MAAM,aAAa,aAAuC,UAAU;CAClE,MAAM,EACJ,Q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