From 87776b22ca9ec24541e0427f7233d859981b5402 Mon Sep 17 00:00:00 2001 From: Artur Santiago Date: Wed, 22 Jan 2025 23:21:30 -0300 Subject: [PATCH] feat: create RatingField component --- packages/components/src/index.ts | 12 ++-- .../src/molecules/RatingField/RatingField.tsx | 71 +++++++++++++++++++ .../src/molecules/RatingField/index.ts | 2 + .../molecules/RatingField/styles.scss | 44 ++++++++++++ 4 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 packages/components/src/molecules/RatingField/RatingField.tsx create mode 100644 packages/components/src/molecules/RatingField/index.ts create mode 100644 packages/ui/src/components/molecules/RatingField/styles.scss diff --git a/packages/components/src/index.ts b/packages/components/src/index.ts index 252250a912..fcae5e5602 100644 --- a/packages/components/src/index.ts +++ b/packages/components/src/index.ts @@ -67,12 +67,8 @@ export type { CarouselBulletsProps, } from './molecules/Carousel' -export { - default as Card -} from './molecules/Card' -export type { - CardProps -} from './molecules/Card' +export { default as Card } from './molecules/Card' +export type { CardProps } from './molecules/Card' export { default as CartItem, @@ -146,6 +142,8 @@ export { default as RadioGroup, RadioOption } from './molecules/RadioGroup' export type { RadioGroupProps, RadioOptionProps } from './molecules/RadioGroup' export { default as Rating } from './molecules/Rating' export type { RatingProps } from './molecules/Rating' +export { default as RatingField } from './molecules/RatingField' +export type { RatingFieldProps } from './molecules/RatingField' export { default as RegionBar } from './molecules/RegionBar' export type { RegionBarProps } from './molecules/RegionBar' @@ -369,5 +367,5 @@ export { export type { SKUMatrixProps, SKUMatrixTriggerProps, - SKUMatrixSidebarProps + SKUMatrixSidebarProps, } from './organisms/SKUMatrix' diff --git a/packages/components/src/molecules/RatingField/RatingField.tsx b/packages/components/src/molecules/RatingField/RatingField.tsx new file mode 100644 index 0000000000..6ff40d9a6f --- /dev/null +++ b/packages/components/src/molecules/RatingField/RatingField.tsx @@ -0,0 +1,71 @@ +import React from 'react' +import type { MutableRefObject } from 'react' +import { Label, Rating, RatingProps } from '../..' + +interface DefaultProps { + /** + * ID to find this component in testing tools (e.g.: cypress, testing library, and jest). + */ + testId?: string + /** + * ID to identify input and corresponding label. + */ + id: string + /** + * The text displayed to identify the input rating. + */ + label: string + /** + * The error message displayed when an error occurs. + */ + error?: string + /** + * Component's ref. + */ + ratingRef?: MutableRefObject +} + +export type RatingFieldProps = DefaultProps & RatingProps + +const RatingField = ({ + id, + label, + error, + disabled, + length = 5, + value = 0, + onChange, + ratingRef, + testId = 'fs-rating-field', + ...otherProps +}: RatingFieldProps) => { + const shouldDisplayError = !disabled && error && error !== '' + + return ( +
+ + + {shouldDisplayError && ( + {error} + )} +
+ ) +} + +export default RatingField diff --git a/packages/components/src/molecules/RatingField/index.ts b/packages/components/src/molecules/RatingField/index.ts new file mode 100644 index 0000000000..06749f3abe --- /dev/null +++ b/packages/components/src/molecules/RatingField/index.ts @@ -0,0 +1,2 @@ +export { default } from './RatingField' +export type { RatingFieldProps } from './RatingField' diff --git a/packages/ui/src/components/molecules/RatingField/styles.scss b/packages/ui/src/components/molecules/RatingField/styles.scss new file mode 100644 index 0000000000..a0d4299c68 --- /dev/null +++ b/packages/ui/src/components/molecules/RatingField/styles.scss @@ -0,0 +1,44 @@ +[data-fs-rating-field] { + // -------------------------------------------------------- + // Design Tokens for RatingField + // -------------------------------------------------------- + + // Label + --fs-rating-field-label-color: var(--fs-color-text-light); + --fs-rating-field-label-size: var(--fs-text-size-2); + --fs-rating-field-label-line-height: var(--fs-text-size-4); + + // Error + --fs-rating-field-error-message-size: var(--fs-text-size-legend); + --fs-rating-field-error-message-line-height: 1.1; + --fs-rating-field-error-message-color: var(--fs-color-danger-text); + + // -------------------------------------------------------- + // Structural Styles + // -------------------------------------------------------- + display: flex; + flex-direction: column; + + [data-fs-rating-field-label] { + font-size: var(--fs-rating-field-label-size); + line-height: var(--fs-rating-field-label-line-height); + color: var(--fs-rating-field-label-color); + } + + [data-fs-rating-field-error-message] { + font-size: var(--fs-rating-field-error-message-size); + line-height: var(--fs-rating-field-error-message-line-height); + color: var(--fs-rating-field-error-message-color); + } + + // -------------------------------------------------------- + // Variants Styles + // -------------------------------------------------------- + &[data-fs-rating-field-error='true'] { + [data-fs-rating] { + [data-fs-rating-item='empty'] svg { + color: var(--fs-rating-field-error-message-color); + } + } + } +}