@stack-spot/citric-react 0.41.1 → 0.42.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. package/CHANGELOG.md +13 -13
  2. package/dist/citric.css +3090 -2846
  3. package/dist/components/Accordion.d.ts +1 -1
  4. package/dist/components/Accordion.js +1 -1
  5. package/dist/components/Alert.d.ts +1 -1
  6. package/dist/components/Alert.js +1 -1
  7. package/dist/components/AsyncContent.d.ts +1 -1
  8. package/dist/components/AsyncContent.js +1 -1
  9. package/dist/components/Autocomplete/Autocomplete.d.ts +211 -0
  10. package/dist/components/Autocomplete/Autocomplete.d.ts.map +1 -0
  11. package/dist/components/Autocomplete/Autocomplete.js +409 -0
  12. package/dist/components/Autocomplete/Autocomplete.js.map +1 -0
  13. package/dist/components/Autocomplete/index.d.ts +3 -0
  14. package/dist/components/Autocomplete/index.d.ts.map +1 -0
  15. package/dist/components/Autocomplete/index.js +2 -0
  16. package/dist/components/Autocomplete/index.js.map +1 -0
  17. package/dist/components/Avatar.d.ts +1 -1
  18. package/dist/components/Avatar.js +1 -1
  19. package/dist/components/AvatarGroup.d.ts +1 -1
  20. package/dist/components/AvatarGroup.js +1 -1
  21. package/dist/components/Badge.d.ts +1 -1
  22. package/dist/components/Badge.js +1 -1
  23. package/dist/components/Blockquote.d.ts +1 -1
  24. package/dist/components/Blockquote.js +1 -1
  25. package/dist/components/Breadcrumb.d.ts +1 -1
  26. package/dist/components/Breadcrumb.js +1 -1
  27. package/dist/components/Button.d.ts +1 -1
  28. package/dist/components/Button.js +1 -1
  29. package/dist/components/ButtonLink.d.ts +1 -1
  30. package/dist/components/ButtonLink.js +1 -1
  31. package/dist/components/Card.d.ts +1 -1
  32. package/dist/components/Card.js +1 -1
  33. package/dist/components/Checkbox.d.ts +1 -1
  34. package/dist/components/Checkbox.d.ts.map +1 -1
  35. package/dist/components/Checkbox.js +2 -2
  36. package/dist/components/Checkbox.js.map +1 -1
  37. package/dist/components/CheckboxGroup.d.ts +1 -1
  38. package/dist/components/CheckboxGroup.js +1 -1
  39. package/dist/components/Circle.d.ts +1 -1
  40. package/dist/components/Circle.js +1 -1
  41. package/dist/components/CitricComponent.d.ts +1 -1
  42. package/dist/components/CitricComponent.d.ts.map +1 -1
  43. package/dist/components/Divider.d.ts +1 -1
  44. package/dist/components/Divider.js +1 -1
  45. package/dist/components/ErrorBoundary.d.ts +1 -1
  46. package/dist/components/ErrorBoundary.js +1 -1
  47. package/dist/components/ErrorMessage.d.ts +1 -1
  48. package/dist/components/ErrorMessage.js +1 -1
  49. package/dist/components/FallbackBoundary.d.ts +1 -1
  50. package/dist/components/FallbackBoundary.js +1 -1
  51. package/dist/components/Favorite.d.ts +1 -1
  52. package/dist/components/Favorite.js +1 -1
  53. package/dist/components/FieldGroup.d.ts +1 -1
  54. package/dist/components/FieldGroup.js +1 -1
  55. package/dist/components/Form.d.ts +2 -2
  56. package/dist/components/Form.js +1 -1
  57. package/dist/components/FormGroup.d.ts +1 -1
  58. package/dist/components/FormGroup.js +1 -1
  59. package/dist/components/Icon.d.ts +1 -1
  60. package/dist/components/Icon.js +1 -1
  61. package/dist/components/IconBox.d.ts +3 -3
  62. package/dist/components/IconBox.js +1 -1
  63. package/dist/components/ImageBox.d.ts +3 -3
  64. package/dist/components/ImageBox.js +1 -1
  65. package/dist/components/ImageWithFallback.d.ts +1 -1
  66. package/dist/components/ImageWithFallback.js +1 -1
  67. package/dist/components/Input.d.ts +1 -1
  68. package/dist/components/Input.js +1 -1
  69. package/dist/components/Link.d.ts +1 -1
  70. package/dist/components/Link.js +1 -1
  71. package/dist/components/LoadingPanel.d.ts +1 -1
  72. package/dist/components/LoadingPanel.js +1 -1
  73. package/dist/components/MenuOverlay/Menu.d.ts +1 -1
  74. package/dist/components/MenuOverlay/Menu.js +1 -1
  75. package/dist/components/MenuOverlay/index.d.ts +1 -1
  76. package/dist/components/MenuOverlay/index.js +1 -1
  77. package/dist/components/Overlay/index.d.ts +1 -1
  78. package/dist/components/Overlay/index.d.ts.map +1 -1
  79. package/dist/components/Overlay/index.js +7 -6
  80. package/dist/components/Overlay/index.js.map +1 -1
  81. package/dist/components/Pagination.d.ts +1 -1
  82. package/dist/components/Pagination.js +1 -1
  83. package/dist/components/ProgressBar.d.ts +1 -1
  84. package/dist/components/ProgressBar.js +1 -1
  85. package/dist/components/ProgressCircular.d.ts +1 -1
  86. package/dist/components/ProgressCircular.js +1 -1
  87. package/dist/components/RadioGroup.d.ts +1 -1
  88. package/dist/components/RadioGroup.js +1 -1
  89. package/dist/components/Rating.d.ts +1 -1
  90. package/dist/components/Rating.js +1 -1
  91. package/dist/components/Select/MultiSelect.d.ts +1 -1
  92. package/dist/components/Select/MultiSelect.js +1 -1
  93. package/dist/components/Select/RichSelect.d.ts +1 -1
  94. package/dist/components/Select/RichSelect.js +1 -1
  95. package/dist/components/Select/SimpleSelect.d.ts +1 -1
  96. package/dist/components/Select/SimpleSelect.js +1 -1
  97. package/dist/components/Select/index.d.ts +1 -1
  98. package/dist/components/Select/index.js +1 -1
  99. package/dist/components/SelectBox.d.ts +1 -1
  100. package/dist/components/SelectBox.js +1 -1
  101. package/dist/components/Skeleton.d.ts +1 -1
  102. package/dist/components/Skeleton.js +1 -1
  103. package/dist/components/Slider.d.ts +1 -1
  104. package/dist/components/Slider.js +1 -1
  105. package/dist/components/SmartTable.d.ts +1 -1
  106. package/dist/components/SmartTable.js +1 -1
  107. package/dist/components/Stepper.d.ts +1 -1
  108. package/dist/components/Stepper.js +1 -1
  109. package/dist/components/Table.d.ts +3 -3
  110. package/dist/components/Table.js +1 -1
  111. package/dist/components/Tabs/index.d.ts +1 -1
  112. package/dist/components/Tabs/index.js +1 -1
  113. package/dist/components/Textarea.d.ts +1 -1
  114. package/dist/components/Textarea.js +1 -1
  115. package/dist/components/Tooltip.d.ts +1 -1
  116. package/dist/components/Tooltip.js +1 -1
  117. package/dist/context/CitricProvider.d.ts +1 -1
  118. package/dist/context/CitricProvider.js +1 -1
  119. package/dist/index.d.ts +1 -0
  120. package/dist/index.d.ts.map +1 -1
  121. package/dist/index.js +1 -0
  122. package/dist/index.js.map +1 -1
  123. package/dist/overlay.js +1 -1
  124. package/dist/theme.css +415 -415
  125. package/package.json +1 -1
  126. package/scripts/build-css.ts +49 -49
  127. package/src/components/Accordion.tsx +130 -130
  128. package/src/components/Alert.tsx +24 -24
  129. package/src/components/AsyncContent.tsx +75 -75
  130. package/src/components/Autocomplete/Autocomplete.tsx +794 -0
  131. package/src/components/Autocomplete/index.ts +3 -0
  132. package/src/components/Avatar.tsx +45 -45
  133. package/src/components/AvatarGroup.tsx +49 -49
  134. package/src/components/Badge.tsx +47 -47
  135. package/src/components/Blockquote.tsx +18 -18
  136. package/src/components/Breadcrumb.tsx +33 -33
  137. package/src/components/Button.tsx +105 -105
  138. package/src/components/ButtonLink.tsx +45 -45
  139. package/src/components/Card.tsx +68 -68
  140. package/src/components/Checkbox.tsx +52 -51
  141. package/src/components/CheckboxGroup.tsx +153 -153
  142. package/src/components/Circle.tsx +43 -43
  143. package/src/components/CitricComponent.ts +47 -47
  144. package/src/components/Divider.tsx +24 -24
  145. package/src/components/ErrorBoundary.tsx +75 -75
  146. package/src/components/ErrorMessage.tsx +11 -11
  147. package/src/components/FallbackBoundary.tsx +40 -40
  148. package/src/components/Favorite.tsx +57 -57
  149. package/src/components/FieldGroup.tsx +46 -46
  150. package/src/components/Form.tsx +36 -36
  151. package/src/components/FormGroup.tsx +57 -57
  152. package/src/components/Icon.tsx +35 -35
  153. package/src/components/IconBox.tsx +134 -134
  154. package/src/components/ImageBox.tsx +125 -125
  155. package/src/components/ImageWithFallback.tsx +65 -65
  156. package/src/components/Input.tsx +49 -49
  157. package/src/components/Link.tsx +55 -55
  158. package/src/components/LoadingPanel.tsx +12 -12
  159. package/src/components/MenuOverlay/Menu.tsx +158 -158
  160. package/src/components/MenuOverlay/context.ts +20 -20
  161. package/src/components/MenuOverlay/index.tsx +55 -55
  162. package/src/components/MenuOverlay/keyboard.ts +60 -60
  163. package/src/components/MenuOverlay/types.ts +171 -171
  164. package/src/components/Overlay/context.ts +10 -10
  165. package/src/components/Overlay/index.tsx +182 -181
  166. package/src/components/Overlay/types.ts +75 -75
  167. package/src/components/Pagination.tsx +133 -133
  168. package/src/components/ProgressBar.tsx +45 -45
  169. package/src/components/ProgressCircular.tsx +45 -45
  170. package/src/components/RadioGroup.tsx +147 -147
  171. package/src/components/Rating.tsx +98 -98
  172. package/src/components/Select/MultiSelect.tsx +217 -217
  173. package/src/components/Select/RichSelect.tsx +128 -128
  174. package/src/components/Select/SimpleSelect.tsx +73 -73
  175. package/src/components/Select/hooks.ts +133 -133
  176. package/src/components/Select/index.tsx +35 -35
  177. package/src/components/Select/types.ts +134 -134
  178. package/src/components/SelectBox.tsx +167 -167
  179. package/src/components/Skeleton.tsx +53 -53
  180. package/src/components/Slider.tsx +89 -89
  181. package/src/components/SmartTable.tsx +227 -227
  182. package/src/components/Stepper.tsx +163 -163
  183. package/src/components/Table.tsx +234 -234
  184. package/src/components/Tabs/TabController.ts +54 -54
  185. package/src/components/Tabs/index.tsx +106 -106
  186. package/src/components/Tabs/types.ts +67 -67
  187. package/src/components/Tabs/utils.ts +6 -6
  188. package/src/components/Text.ts +111 -111
  189. package/src/components/Textarea.tsx +27 -27
  190. package/src/components/Tooltip.tsx +83 -83
  191. package/src/components/layout.tsx +101 -101
  192. package/src/context/CitricContext.tsx +4 -4
  193. package/src/context/CitricProvider.tsx +14 -14
  194. package/src/context/hooks.ts +6 -6
  195. package/src/index.ts +59 -58
  196. package/src/overlay.ts +348 -348
  197. package/src/types.ts +235 -235
  198. package/src/utils/ValueController.ts +28 -28
  199. package/src/utils/acessibility.ts +92 -92
  200. package/src/utils/checkbox.ts +121 -121
  201. package/src/utils/css.ts +119 -119
  202. package/src/utils/options.ts +9 -9
  203. package/src/utils/radio.ts +93 -93
  204. package/src/utils/react.ts +6 -6
  205. package/src/utils/time.ts +5 -5
  206. package/tsconfig.json +10 -10
@@ -1,147 +1,147 @@
1
- import { isNil } from 'lodash'
2
- import { useMemo } from 'react'
3
- import { WithColorScheme } from '../types'
4
- import { defaultRenderKey, defaultRenderLabel } from '../utils/options'
5
- import { withRef } from '../utils/react'
6
- import { CitricComponent } from './CitricComponent'
7
- import { Column } from './layout'
8
-
9
- export interface BaseRadioGroupProps<T> extends WithColorScheme {
10
- /**
11
- * The field name.
12
- */
13
- name?: string,
14
- /**
15
- * The current value.
16
- */
17
- value?: T,
18
- /**
19
- * All the items (radio buttons) to render.
20
- */
21
- options: T[],
22
- /**
23
- * Called whenever the selected radio button changes.
24
- * @param value the currently selected item.
25
- */
26
- onChange?: (value: T) => void,
27
- /**
28
- * A function to render the item label.
29
- *
30
- * Attention: ignored if `renderItem` is set.
31
- *
32
- * @example
33
- * `(option) => option.name`
34
- * @default "the item's toString() result."
35
- * @param option the item to render.
36
- * @returns a React Node to render.
37
- */
38
- renderLabel?: (option: T) => React.ReactNode,
39
- /**
40
- * A function to render the item with the radio button. When set, `renderLabel` is ignored.
41
- * @example
42
- * ```
43
- * (radio, option) => <label>{radio} {option.name}</label>
44
- * ```
45
- * @param radio the radio button.
46
- * @param option the current item.
47
- * @returns a React Node to render.
48
- */
49
- renderItem?: (radio: React.ReactElement, option: T) => React.ReactNode,
50
- /**
51
- * A function to render the item value, a unique identifier for the option.
52
- * @example
53
- * `(option) => option.id`
54
- * @default "if the item is a string or a number, the stringified item. Otherwise, undefined."
55
- * @param option the item to compute a key for.
56
- * @returns a string key.
57
- */
58
- renderKey?: (option: T) => string | number | undefined,
59
- /**
60
- * If this function returns true for the item, this option is disabled.
61
- * @default "nothing is disabled"
62
- * @param option the item to calculate "disabled" for.
63
- * @returns true if the item should be disabled, false otherwise.
64
- */
65
- isDisabled?: (option: T) => boolean,
66
- /**
67
- * The space between options.
68
- *
69
- * @default "8px"
70
- */
71
- gap?: string,
72
- /**
73
- * If set to false, the checkboxes will have tabIndex = -1.
74
- *
75
- * @default true
76
- */
77
- focusable?: boolean,
78
- }
79
-
80
- export type RadioGroupProps<T> = Omit<React.JSX.IntrinsicElements['div'], 'onChange' | 'children'> & BaseRadioGroupProps<T>
81
-
82
- /**
83
- * Renders a list of radio button for single-selection. One radio button is rendered for each option.
84
- *
85
- * Attention: single radio buttons don't make sense, this is why they're not provided by this library.
86
- *
87
- * This component can be heavily customized via its properties. Check the storybook for complex examples.
88
- *
89
- * Tip: if you need to implement features like "search", use the hook `useRadioGroupControls`.
90
- *
91
- * @example
92
- *
93
- * ```
94
- * const options = useMemo(() => [
95
- * { id: 1, name: 'Option 1' },
96
- * { id: 2, name: 'Option 2' },
97
- * { id: 3, name: 'Option 3' },
98
- * ], [])
99
- *
100
- * const [value, setValue] = useState<(typeof options)[number] | undefined>()
101
- *
102
- * return <RadioGroup options={options} renderLabel={o => o.name} renderKey={o => o.id} value={value} setValue={setValue} />
103
- * ```
104
- */
105
- export const RadioGroup = withRef(
106
- function RadioGroup<T>({
107
- name,
108
- value,
109
- options,
110
- onChange,
111
- renderLabel = defaultRenderLabel,
112
- renderKey = defaultRenderKey,
113
- renderItem,
114
- isDisabled,
115
- colorScheme,
116
- gap = '8px',
117
- focusable = true,
118
- style,
119
- ...props
120
- }: RadioGroupProps<T>) {
121
- const items = useMemo(() => {
122
- const valueKey = value ? renderKey(value) : undefined
123
- return options.map((o) => {
124
- const key = renderKey(o)
125
- const radio = <CitricComponent
126
- tag="input"
127
- component="radio"
128
- type="radio"
129
- name={name}
130
- value={key}
131
- checked={value === o || (!isNil(key) && valueKey === key)}
132
- onChange={() => onChange?.(o)}
133
- disabled={isDisabled?.(o)}
134
- tabIndex={focusable ? undefined : -1}
135
- colorScheme={colorScheme}
136
- />
137
- return renderItem ? renderItem(radio, o) : (
138
- <CitricComponent tag="label" component="radio-row" key={key} colorScheme={colorScheme}>
139
- {radio}
140
- {renderLabel(o)}
141
- </CitricComponent>
142
- )
143
- })
144
- }, [options, value, name, colorScheme])
145
- return <Column {...props} style={{ gap, ...style }}>{items}</Column>
146
- },
147
- )
1
+ import { isNil } from 'lodash'
2
+ import { useMemo } from 'react'
3
+ import { WithColorScheme } from '../types'
4
+ import { defaultRenderKey, defaultRenderLabel } from '../utils/options'
5
+ import { withRef } from '../utils/react'
6
+ import { CitricComponent } from './CitricComponent'
7
+ import { Column } from './layout'
8
+
9
+ export interface BaseRadioGroupProps<T> extends WithColorScheme {
10
+ /**
11
+ * The field name.
12
+ */
13
+ name?: string,
14
+ /**
15
+ * The current value.
16
+ */
17
+ value?: T,
18
+ /**
19
+ * All the items (radio buttons) to render.
20
+ */
21
+ options: T[],
22
+ /**
23
+ * Called whenever the selected radio button changes.
24
+ * @param value the currently selected item.
25
+ */
26
+ onChange?: (value: T) => void,
27
+ /**
28
+ * A function to render the item label.
29
+ *
30
+ * Attention: ignored if `renderItem` is set.
31
+ *
32
+ * @example
33
+ * `(option) => option.name`
34
+ * @default "the item's toString() result."
35
+ * @param option the item to render.
36
+ * @returns a React Node to render.
37
+ */
38
+ renderLabel?: (option: T) => React.ReactNode,
39
+ /**
40
+ * A function to render the item with the radio button. When set, `renderLabel` is ignored.
41
+ * @example
42
+ * ```
43
+ * (radio, option) => <label>{radio} {option.name}</label>
44
+ * ```
45
+ * @param radio the radio button.
46
+ * @param option the current item.
47
+ * @returns a React Node to render.
48
+ */
49
+ renderItem?: (radio: React.ReactElement, option: T) => React.ReactNode,
50
+ /**
51
+ * A function to render the item value, a unique identifier for the option.
52
+ * @example
53
+ * `(option) => option.id`
54
+ * @default "if the item is a string or a number, the stringified item. Otherwise, undefined."
55
+ * @param option the item to compute a key for.
56
+ * @returns a string key.
57
+ */
58
+ renderKey?: (option: T) => string | number | undefined,
59
+ /**
60
+ * If this function returns true for the item, this option is disabled.
61
+ * @default "nothing is disabled"
62
+ * @param option the item to calculate "disabled" for.
63
+ * @returns true if the item should be disabled, false otherwise.
64
+ */
65
+ isDisabled?: (option: T) => boolean,
66
+ /**
67
+ * The space between options.
68
+ *
69
+ * @default "8px"
70
+ */
71
+ gap?: string,
72
+ /**
73
+ * If set to false, the checkboxes will have tabIndex = -1.
74
+ *
75
+ * @default true
76
+ */
77
+ focusable?: boolean,
78
+ }
79
+
80
+ export type RadioGroupProps<T> = Omit<React.JSX.IntrinsicElements['div'], 'onChange' | 'children'> & BaseRadioGroupProps<T>
81
+
82
+ /**
83
+ * Renders a list of radio button for single-selection. One radio button is rendered for each option.
84
+ *
85
+ * Attention: single radio buttons don't make sense, this is why they're not provided by this library.
86
+ *
87
+ * This component can be heavily customized via its properties. Check the storybook for complex examples.
88
+ *
89
+ * Tip: if you need to implement features like "search", use the hook `useRadioGroupControls`.
90
+ *
91
+ * @example
92
+ *
93
+ * ```
94
+ * const options = useMemo(() => [
95
+ * { id: 1, name: 'Option 1' },
96
+ * { id: 2, name: 'Option 2' },
97
+ * { id: 3, name: 'Option 3' },
98
+ * ], [])
99
+ *
100
+ * const [value, setValue] = useState<(typeof options)[number] | undefined>()
101
+ *
102
+ * return <RadioGroup options={options} renderLabel={o => o.name} renderKey={o => o.id} value={value} setValue={setValue} />
103
+ * ```
104
+ */
105
+ export const RadioGroup = withRef(
106
+ function RadioGroup<T>({
107
+ name,
108
+ value,
109
+ options,
110
+ onChange,
111
+ renderLabel = defaultRenderLabel,
112
+ renderKey = defaultRenderKey,
113
+ renderItem,
114
+ isDisabled,
115
+ colorScheme,
116
+ gap = '8px',
117
+ focusable = true,
118
+ style,
119
+ ...props
120
+ }: RadioGroupProps<T>) {
121
+ const items = useMemo(() => {
122
+ const valueKey = value ? renderKey(value) : undefined
123
+ return options.map((o) => {
124
+ const key = renderKey(o)
125
+ const radio = <CitricComponent
126
+ tag="input"
127
+ component="radio"
128
+ type="radio"
129
+ name={name}
130
+ value={key}
131
+ checked={value === o || (!isNil(key) && valueKey === key)}
132
+ onChange={() => onChange?.(o)}
133
+ disabled={isDisabled?.(o)}
134
+ tabIndex={focusable ? undefined : -1}
135
+ colorScheme={colorScheme}
136
+ />
137
+ return renderItem ? renderItem(radio, o) : (
138
+ <CitricComponent tag="label" component="radio-row" key={key} colorScheme={colorScheme}>
139
+ {radio}
140
+ {renderLabel(o)}
141
+ </CitricComponent>
142
+ )
143
+ })
144
+ }, [options, value, name, colorScheme])
145
+ return <Column {...props} style={{ gap, ...style }}>{items}</Column>
146
+ },
147
+ )
@@ -1,98 +1,98 @@
1
- import { useCallback } from 'react'
2
- import { withRef } from '../utils/react'
3
- import { CitricComponent } from './CitricComponent'
4
-
5
- export type RatingValue = 1 | 2 | 3 | 4 | 5
6
-
7
- export interface BaseRatingProps {
8
- /**
9
- * The current value of the rating.
10
- */
11
- value: RatingValue | undefined,
12
- /**
13
- * Callback when the value changes.
14
- */
15
- onChange?: (value: RatingValue) => void,
16
- /**
17
- * The name of this input.
18
- */
19
- name?: string,
20
- /**
21
- * Whether the rating inputs are disabled.
22
- */
23
- disabled?: boolean,
24
- /**
25
- * Whether the rating inputs are readonly.
26
- */
27
- readonly?: boolean,
28
- }
29
-
30
- export type RatingProps = Omit<React.JSX.IntrinsicElements['div'], 'onChange' | 'children'> & BaseRatingProps
31
-
32
- /**
33
- * Renders a 5 star rating UI. The user can select 1 to 5 stars.
34
- *
35
- * @example
36
- * ```
37
- * const [value, setValue] = useState<RatingValue | undefined>()
38
- * return <Rating value={value} onChange={setValue} />
39
- * ```
40
- * In the example above, `RatingValue` is any integer from 1 to 5.
41
- */
42
- export const Rating = withRef(({ value, onChange, name, disabled, readonly, ...props }: RatingProps) => {
43
- const handleChange = useCallback((newValue: RatingValue) => {
44
- if (!disabled && !readonly) {
45
- onChange?.(newValue)
46
- }
47
- }, [disabled, readonly, onChange])
48
-
49
- return (
50
- <CitricComponent tag="div" component="rating" {...props}>
51
- <input
52
- type="radio"
53
- value="1"
54
- name={name}
55
- checked={value === 5}
56
- onChange={() => handleChange(5)}
57
- disabled={disabled}
58
- readOnly={readonly}
59
- />
60
- <input
61
- type="radio"
62
- value="2"
63
- name={name}
64
- checked={value === 4}
65
- onChange={() => handleChange(4)}
66
- disabled={disabled}
67
- readOnly={readonly}
68
- />
69
- <input
70
- type="radio"
71
- value="3"
72
- name={name}
73
- checked={value === 3}
74
- onChange={() => handleChange(3)}
75
- disabled={disabled}
76
- readOnly={readonly}
77
- />
78
- <input
79
- type="radio"
80
- value="4"
81
- name={name}
82
- checked={value === 2}
83
- onChange={() => handleChange(2)}
84
- disabled={disabled}
85
- readOnly={readonly}
86
- />
87
- <input
88
- type="radio"
89
- value="5"
90
- name={name}
91
- checked={value === 1}
92
- onChange={() => handleChange(1)}
93
- disabled={disabled}
94
- readOnly={readonly}
95
- />
96
- </CitricComponent>
97
- )
98
- })
1
+ import { useCallback } from 'react'
2
+ import { withRef } from '../utils/react'
3
+ import { CitricComponent } from './CitricComponent'
4
+
5
+ export type RatingValue = 1 | 2 | 3 | 4 | 5
6
+
7
+ export interface BaseRatingProps {
8
+ /**
9
+ * The current value of the rating.
10
+ */
11
+ value: RatingValue | undefined,
12
+ /**
13
+ * Callback when the value changes.
14
+ */
15
+ onChange?: (value: RatingValue) => void,
16
+ /**
17
+ * The name of this input.
18
+ */
19
+ name?: string,
20
+ /**
21
+ * Whether the rating inputs are disabled.
22
+ */
23
+ disabled?: boolean,
24
+ /**
25
+ * Whether the rating inputs are readonly.
26
+ */
27
+ readonly?: boolean,
28
+ }
29
+
30
+ export type RatingProps = Omit<React.JSX.IntrinsicElements['div'], 'onChange' | 'children'> & BaseRatingProps
31
+
32
+ /**
33
+ * Renders a 5 star rating UI. The user can select 1 to 5 stars.
34
+ *
35
+ * @example
36
+ * ```
37
+ * const [value, setValue] = useState<RatingValue | undefined>()
38
+ * return <Rating value={value} onChange={setValue} />
39
+ * ```
40
+ * In the example above, `RatingValue` is any integer from 1 to 5.
41
+ */
42
+ export const Rating = withRef(({ value, onChange, name, disabled, readonly, ...props }: RatingProps) => {
43
+ const handleChange = useCallback((newValue: RatingValue) => {
44
+ if (!disabled && !readonly) {
45
+ onChange?.(newValue)
46
+ }
47
+ }, [disabled, readonly, onChange])
48
+
49
+ return (
50
+ <CitricComponent tag="div" component="rating" {...props}>
51
+ <input
52
+ type="radio"
53
+ value="1"
54
+ name={name}
55
+ checked={value === 5}
56
+ onChange={() => handleChange(5)}
57
+ disabled={disabled}
58
+ readOnly={readonly}
59
+ />
60
+ <input
61
+ type="radio"
62
+ value="2"
63
+ name={name}
64
+ checked={value === 4}
65
+ onChange={() => handleChange(4)}
66
+ disabled={disabled}
67
+ readOnly={readonly}
68
+ />
69
+ <input
70
+ type="radio"
71
+ value="3"
72
+ name={name}
73
+ checked={value === 3}
74
+ onChange={() => handleChange(3)}
75
+ disabled={disabled}
76
+ readOnly={readonly}
77
+ />
78
+ <input
79
+ type="radio"
80
+ value="4"
81
+ name={name}
82
+ checked={value === 2}
83
+ onChange={() => handleChange(2)}
84
+ disabled={disabled}
85
+ readOnly={readonly}
86
+ />
87
+ <input
88
+ type="radio"
89
+ value="5"
90
+ name={name}
91
+ checked={value === 1}
92
+ onChange={() => handleChange(1)}
93
+ disabled={disabled}
94
+ readOnly={readonly}
95
+ />
96
+ </CitricComponent>
97
+ )
98
+ })