Ratings
Please refer to MUI's official docs for more details on component's usage guide and API documentation.
Use name
prop to name the rating and use value
or defaultValue
prop to set any initial value to a rating.
Controlled
Read only
Disabled
No rating given
- TSX
- JS
// React Imports
import { useState } from 'react'
// MUI Imports
import Rating from '@mui/material/Rating'
import Typography from '@mui/material/Typography'
const RatingsBasic = () => {
// States
const [value, setValue] = useState<number | null>(2)
return (
<>
<div className='mbe-3'>
<Typography className='font-medium'>Controlled</Typography>
<Rating value={value} name='basic-controlled' onChange={(event, newValue) => setValue(newValue)} />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Read only</Typography>
<Rating readOnly value={value} name='read-only' />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Disabled</Typography>
<Rating disabled value={value} name='disabled' />
</div>
<div>
<Typography className='font-medium'>No rating given</Typography>
<Rating value={null} name='no-value' />
</div>
</>
)
}
export default RatingsBasic
// React Imports
import { useState } from 'react'
// MUI Imports
import Rating from '@mui/material/Rating'
import Typography from '@mui/material/Typography'
const RatingsBasic = () => {
// States
const [value, setValue] = useState(2)
return (
<>
<div className='mbe-3'>
<Typography className='font-medium'>Controlled</Typography>
<Rating value={value} name='basic-controlled' onChange={(event, newValue) => setValue(newValue)} />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Read only</Typography>
<Rating readOnly value={value} name='read-only' />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Disabled</Typography>
<Rating disabled value={value} name='disabled' />
</div>
<div>
<Typography className='font-medium'>No rating given</Typography>
<Rating value={null} name='no-value' />
</div>
</>
)
}
export default RatingsBasic
Use icon
or emptyIcon
prop to change default icon or empty icon respectively,
max
prop to set number of ratings and IconContainerComponent
prop to change every icons in the ratings.
Custom empty icon
Custom icon and color
10 stars
Custom icon set
- TSX
- JS
// MUI Imports
import Typography from '@mui/material/Typography'
import Rating from '@mui/material/Rating'
import type { IconContainerProps } from '@mui/material/Rating'
type CustomIcons = {
[index: string]: { icon: string; label: string }
}
const customIcons: CustomIcons = {
1: {
label: 'Very Dissatisfied',
icon: 'bx-sad'
},
2: {
label: 'Neutral',
icon: 'bx-meh'
},
3: {
label: 'Satisfied',
icon: 'bx-happy'
},
4: {
label: 'Very Satisfied',
icon: 'bx-happy-heart-eyes'
}
}
const IconContainer = (props: IconContainerProps) => {
const { value } = props
return (
<span {...props}>
<i className={customIcons[value].icon} />
</span>
)
}
const RatingsCustomized = () => {
return (
<>
<div className='mbe-3'>
<Typography className='font-medium'>Custom empty icon</Typography>
<Rating name='customized-empty' defaultValue={2} precision={0.5} emptyIcon={<i className='bx-bxs-star' />} />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Custom icon and color</Typography>
<Rating
precision={0.5}
defaultValue={3}
name='customized-color'
icon={<i className='bx-bxs-heart flex-shrink-0 text-error' />}
emptyIcon={<i className='bx-bxs-heart' />}
/>
</div>
<div className='mbe-3'>
<Typography className='font-medium'>10 stars</Typography>
<Rating name='customized-10' defaultValue={7} max={10} />
</div>
<>
<Typography className='font-medium'>Custom icon set</Typography>
<Rating name='customized-icons' defaultValue={2} max={4} IconContainerComponent={IconContainer} />
</>
</>
)
}
export default RatingsCustomized
// MUI Imports
import Typography from '@mui/material/Typography'
import Rating from '@mui/material/Rating'
const customIcons = {
1: {
label: 'Very Dissatisfied',
icon: 'bx-sad'
},
2: {
label: 'Neutral',
icon: 'bx-meh'
},
3: {
label: 'Satisfied',
icon: 'bx-happy'
},
4: {
label: 'Very Satisfied',
icon: 'bx-happy-heart-eyes'
}
}
const IconContainer = props => {
const { value } = props
return (
<span {...props}>
<i className={customIcons[value].icon} />
</span>
)
}
const RatingsCustomized = () => {
return (
<>
<div className='mbe-3'>
<Typography className='font-medium'>Custom empty icon</Typography>
<Rating name='customized-empty' defaultValue={2} precision={0.5} emptyIcon={<i className='bx-bxs-star' />} />
</div>
<div className='mbe-3'>
<Typography className='font-medium'>Custom icon and color</Typography>
<Rating
precision={0.5}
defaultValue={3}
name='customized-color'
icon={<i className='bx-bxs-heart flex-shrink-0 text-error' />}
emptyIcon={<i className='bx-bxs-heart' />}
/>
</div>
<div className='mbe-3'>
<Typography className='font-medium'>10 stars</Typography>
<Rating name='customized-10' defaultValue={7} max={10} />
</div>
<>
<Typography className='font-medium'>Custom icon set</Typography>
<Rating name='customized-icons' defaultValue={2} max={4} IconContainerComponent={IconContainer} />
</>
</>
)
}
export default RatingsCustomized
Use precision
prop to define the minimum increment value change allowed.
Half Ratings
Read only
// MUI Imports
import Rating from '@mui/material/Rating'
import Typography from '@mui/material/Typography'
const RatingsHalf = () => {
return (
<>
<div className='mbe-3'>
<Typography className='font-medium'>Half Ratings</Typography>
<Rating defaultValue={2.5} precision={0.5} name='half-rating' />
</div>
<>
<Typography className='font-medium'>Read only</Typography>
<Rating readOnly defaultValue={2.5} precision={0.5} name='read-only' />
</>
</>
)
}
export default RatingsHalf
Use size={'small' | 'large'}
prop for different sizes of ratings.
// MUI Imports
import Rating from '@mui/material/Rating'
const RatingsSizes = () => {
return (
<div className='flex flex-col items-start gap-4'>
<Rating defaultValue={2} name='size-small' size='small' />
<Rating defaultValue={2} name='size-medium' />
<Rating defaultValue={2} name='size-large' size='large' />
</div>
)
}
export default RatingsSizes
You can display a label on hover to help users pick the correct rating value. The demo uses the onChangeActive
prop.
Poor+
- TSX
- JS
// React Imports
import { useState } from 'react'
// MUI Imports
import Rating from '@mui/material/Rating'
import Typography from '@mui/material/Typography'
const labels: { [index: string]: string } = {
0.5: 'Useless',
1: 'Useless+',
1.5: 'Poor',
2: 'Poor+',
2.5: 'Ok',
3: 'Ok+',
3.5: 'Good',
4: 'Good+',
4.5: 'Excellent',
5: 'Excellent+'
}
const RatingsHoverFeedback = () => {
// States
const [hover, setHover] = useState<number>(-1)
const [value, setValue] = useState<number | null>(2)
return (
<div className='flex items-center'>
<Rating
value={value}
precision={0.5}
name='hover-feedback'
className='mie-4'
onChange={(event, newValue) => setValue(newValue)}
onChangeActive={(event, newHover) => setHover(newHover)}
/>
{value !== null && <Typography>{labels[hover !== -1 ? hover : value]}</Typography>}
</div>
)
}
export default RatingsHoverFeedback
// React Imports
import { useState } from 'react'
// MUI Imports
import Rating from '@mui/material/Rating'
import Typography from '@mui/material/Typography'
const labels = {
0.5: 'Useless',
1: 'Useless+',
1.5: 'Poor',
2: 'Poor+',
2.5: 'Ok',
3: 'Ok+',
3.5: 'Good',
4: 'Good+',
4.5: 'Excellent',
5: 'Excellent+'
}
const RatingsHoverFeedback = () => {
// States
const [hover, setHover] = useState(-1)
const [value, setValue] = useState(2)
return (
<div className='flex items-center'>
<Rating
value={value}
precision={0.5}
name='hover-feedback'
className='mie-4'
onChange={(event, newValue) => setValue(newValue)}
onChangeActive={(event, newHover) => setHover(newHover)}
/>
{value !== null && <Typography>{labels[hover !== -1 ? hover : value]}</Typography>}
</div>
)
}
export default RatingsHoverFeedback