Skip to content

Commit

Permalink
feat: Add live mode to rating #16
Browse files Browse the repository at this point in the history
  • Loading branch information
lo5 committed Jul 4, 2022
1 parent 2431d53 commit 7c801bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions docs/rating.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,21 @@ view(f'Your rating was {stars} stars.')


![Screenshot](assets/screenshots/rating_range.png)


## Handle changes immediately

Set `live=True` to handle changes immediately.


```py
stars = 3
while True:
stars = view(
box('Rate your experience', mode='rating', value=stars, live=True),
f'Your rating was {stars} stars.'
)
```


![Screenshot](assets/screenshots/rating_live.png)
11 changes: 11 additions & 0 deletions py/pkg/docs/rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ def rating_min_max(view: View): # height 2
def rating_range(view: View): # height 2
stars = view(box('Rate your experience', mode='rating', value=3, range=(0, 10)))
view(f'Your rating was {stars} stars.')


# ## Handle changes immediately
# Set `live=True` to handle changes immediately.
def rating_live(view: View): # height 2
stars = 3
while True:
stars = view(
box('Rate your experience', mode='rating', value=stars, live=True),
f'Your rating was {stars} stars.'
)
3 changes: 2 additions & 1 deletion web/src/rating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import { BoxProps, make } from './ui';

export const Rating = make(({ box }: BoxProps) => {
const
{ context, text, placeholder, min, max, value } = box,
{ context, text, placeholder, min, max, value, live } = box,
allowZeroStars = isN(min) && min <= 0,
defaultRating = toN(value) ?? (allowZeroStars ? 0 : 1),
onChange = (event: React.FormEvent<HTMLElement>, rating?: number) => {
if (rating === undefined) return
context.record(rating)
if (live) context.commit()
},
render = () => {
return (
Expand Down

0 comments on commit 7c801bf

Please sign in to comment.