Skip to content

Commit c0baf1b

Browse files
committed
fix #16, release v1.5.0
1 parent e1f7cf8 commit c0baf1b

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,16 @@ function App() {
270270
271271
### :cyclone: Core
272272
273-
| :thinking: | Prop | Description | Type | Default | Required |
274-
| ------------------- | --------------- | -------------------------------------------------------------------------- | ----------------------------------------------- | --------- | ------------------------------- |
275-
| :green_circle: | `value` | An integer from 0 to `items`. It can be a float if `readOnly` is **true**. | number | undefined | :white_check_mark: |
276-
| :large_blue_circle: | `onChange` | Setter or custom function to update the rating. | RatingChange | () => {} | Only if `readOnly` is **false** |
277-
| :large_blue_circle: | `onHoverChange` | Callback to execute while navigating the rating items. | (hoveredValue: number) => void | () => {} | :x: |
278-
| :green_circle: | `items` | Rating items to display. | 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 | 5 | :x: |
279-
| :green_circle: | `readOnly` | Whether to render an accessible image element or not. | boolean | false | :x: |
280-
| :large_blue_circle: | `isDisabled` | Whether to disable the radio group or not. | boolean | false | :x: |
281-
| :large_blue_circle: | `isRequired` | Whether users should be able to set rating to 0 or not. | boolean | false | :x: |
273+
| :thinking: | Prop | Description | Type | Default | Required |
274+
| ------------------- | ---------------- | ------------------------------------------------------------------------------ | ----------------------------------------------- | --------- | ------------------------------- |
275+
| :green_circle: | `value` | An integer from 0 to `items`. It can be a float if `readOnly` is **true**. | number | undefined | :white_check_mark: |
276+
| :large_blue_circle: | `onChange` | Setter or custom function to update the rating. | RatingChange | () => {} | Only if `readOnly` is **false** |
277+
| :large_blue_circle: | `onHoverChange` | Callback to execute while navigating the rating items. | (hoveredValue: number) => void | () => {} | :x: |
278+
| :green_circle: | `items` | Rating items to display. | 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9 \| 10 | 5 | :x: |
279+
| :green_circle: | `readOnly` | Whether to render an accessible image element or not. | boolean | false | :x: |
280+
| :large_blue_circle: | `isDisabled` | Whether to disable the radio group or not. | boolean | false | :x: |
281+
| :large_blue_circle: | `isRequired` | Whether users should be able to set rating to 0 or not. | boolean | false | :x: |
282+
| :green_circle: | `preventDefault` | Whether or not to call `event.preventDefault` on click and Enter/Space select. | `click` \| `keydown` \| `all` \| `none` | `all` | :x: |
282283
283284
`ref`, `id`, `className`, `style`, `onBlur`, `onFocus` are also available.
284285

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@smastrom/react-rating",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"private": false,
55
"keywords": [
66
"react",

src/Rating.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const Rating: typeof RatingComponent = forwardRef<HTMLDivElement, RatingP
5656
onHoverChange = noop,
5757
onFocus = noop,
5858
onBlur = noop,
59+
preventDefault = 'all',
5960
isDisabled = false,
6061
highlightOnlySelected = false,
6162
orientation = OrientationProps.HORIZONTAL,
@@ -232,7 +233,9 @@ export const Rating: typeof RatingComponent = forwardRef<HTMLDivElement, RatingP
232233
}
233234

234235
function handleStarClick(event: MouseEvent, clickedIndex: number) {
235-
event.preventDefault()
236+
if (preventDefault === 'all' || preventDefault === 'keydown') {
237+
event.preventDefault()
238+
}
236239
event.stopPropagation()
237240

238241
if (!isRequired && activeStarIndex === clickedIndex) {
@@ -318,11 +321,12 @@ export const Rating: typeof RatingComponent = forwardRef<HTMLDivElement, RatingP
318321

319322
case 'Enter':
320323
case 'Space':
321-
event.preventDefault()
324+
if (preventDefault === 'all' || preventDefault === 'click') {
325+
event.preventDefault()
326+
}
322327
return onChange(isResetBtn ? 0 : childIndex + 1)
323328
}
324329

325-
event.preventDefault()
326330
event.stopPropagation()
327331
}
328332

src/exportedTypes.ts

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export type InputProps = {
110110
visibleItemLabelIds?: string[]
111111
/** Accessible label for the invisible reset radio. Effective if `isRequired` is set to **false**. */
112112
resetLabel?: string
113+
/** Whether or not to call event.preventDefault on click and Enter/Space select. */
114+
preventDefault?: 'click' | 'keydown' | 'all' | 'none'
113115
}
114116

115117
export type RatingProps = SharedProps & ReadOnlyProps & InputProps

0 commit comments

Comments
 (0)