Skip to content

Commit

Permalink
Merge pull request #896 from zauberzeug/dynamic-props
Browse files Browse the repository at this point in the history
evaluate dynamic props on the client
  • Loading branch information
falkoschindler authored May 26, 2023
2 parents eef48de + ff6fb17 commit 4d7f169
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if TYPE_CHECKING:
from .client import Client

PROPS_PATTERN = re.compile(r'([\w\-]+)(?:=(?:("[^"\\]*(?:\\.[^"\\]*)*")|([\w\-.%:\/]+)))?(?:$|\s)')
PROPS_PATTERN = re.compile(r'([:\w\-]+)(?:=(?:("[^"\\]*(?:\\.[^"\\]*)*")|([\w\-.%:\/]+)))?(?:$|\s)')


class Element(Visibility):
Expand Down
6 changes: 6 additions & 0 deletions nicegui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
style: Object.entries(element.style).reduce((str, [p, val]) => `${str}${p}:${val};`, '') || undefined,
...element.props,
};
Object.entries(props).forEach(([key, value]) => {
if (key.startsWith(':')) {
props[key.substring(1)] = eval(value);
delete props[key];
}
});
element.events.forEach((event) => {
let event_name = 'on' + event.type[0].toLocaleUpperCase() + event.type.substring(1);
event.specials.forEach(s => event_name += s[0].toLocaleUpperCase() + s.substring(1));
Expand Down
14 changes: 14 additions & 0 deletions tests/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ def test_date_with_range_and_multi_selection(screen: Screen):
screen.click('25')
screen.click('28')
screen.should_contain('8 days')


def test_date_with_filter(screen: Screen):
d = ui.date().props('''default-year-month=2023/01 :options="date => date <= '2023/01/15'"''')
ui.label().bind_text_from(d, 'value')

screen.open('/')
screen.click('14')
screen.should_contain('2023-01-14')
screen.click('15')
screen.should_contain('2023-01-15')
screen.click('16')
screen.wait(0.5)
screen.should_not_contain('2023-01-16')
1 change: 1 addition & 0 deletions website/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def tooltips_demo():
NiceGUI uses the [Quasar Framework](https://quasar.dev/) version 1.0 and hence has its full design power.
Each NiceGUI element provides a `props` method whose content is passed [to the Quasar component](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components):
Have a look at [the Quasar documentation](https://quasar.dev/vue-components/button#design) for all styling props.
Props with a leading `:` can contain JavaScript expressions that are evaluated on the client.
You can also apply [Tailwind CSS](https://tailwindcss.com/) utility classes with the `classes` method.
If you really need to apply CSS, you can use the `styles` method. Here the delimiter is `;` instead of a blank space.
Expand Down
8 changes: 8 additions & 0 deletions website/more_documentation/date_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ def date():
ui.icon('edit_calendar').on('click', lambda: menu.open()).classes('cursor-pointer')
with ui.menu() as menu:
ui.date().bind_value(date)

@text_demo('Date filter', '''
This demo shows how to filter the dates in a date picker.
In order to pass a function to the date picker, we use the `:options` property.
The leading `:` tells NiceGUI that the value is a JavaScript expression.
''')
def date_filter():
ui.date().props('''default-year-month=2023/01 :options="date => date <= '2023/01/15'"''')

0 comments on commit 4d7f169

Please sign in to comment.