Coverage for gws-app/gws/plugin/model_widget/select/__init__.py: 0%

25 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Select widget. 

2 

3Drop-down list with a fixed set of items, optionally with a search field. 

4Each item has a value, which is stored in the field, and a text to display, 

5which defaults to the value. The ``extraText`` and ``level`` options of an 

6item are not passed to the client. 

7 

8Example:: 

9 

10 fields+ { 

11 name "kind" 

12 type "text" 

13 widget { 

14 type "select" 

15 withSearch true 

16 items [ 

17 { value "a" text "Type A" } 

18 { value "b" text "Type B" } 

19 ] 

20 } 

21 } 

22""" 

23 

24from typing import Optional, Any 

25 

26import gws 

27import gws.base.model.widget 

28 

29 

30# see also js/ui/select 

31class ListItem(gws.Data): 

32 """Item in the select widget.""" 

33 

34 value: Any 

35 """Value of the item.""" 

36 text: str 

37 """Text to display for the item.""" 

38 extraText: Optional[str] 

39 """Additional text to display for the item.""" 

40 level: Optional[int] 

41 """Optional level for hierarchical items, used for indentation.""" 

42 

43 

44class ListItemConfig(gws.Config): 

45 """Option of a select widget.""" 

46 

47 value: Any 

48 """Value stored in the field when the item is selected.""" 

49 text: Optional[str] 

50 """Label shown for the item.""" 

51 extraText: Optional[str] 

52 """Additional text to display for the item.""" 

53 level: Optional[int] 

54 """Optional level for hierarchical items, used for indentation.""" 

55 

56 

57@gws.ext.config.modelWidget('select') 

58class Config(gws.base.model.widget.Config): 

59 """Drop-down list with a fixed set of values.""" 

60 

61 items: list[ListItemConfig] 

62 """Options to choose from.""" 

63 withSearch: bool = False 

64 """Show a search field to filter the options.""" 

65 

66 

67@gws.ext.props.modelWidget('select') 

68class Props(gws.base.model.widget.Props): 

69 items: list[ListItem] 

70 withSearch: bool 

71 

72 

73@gws.ext.object.modelWidget('select') 

74class Object(gws.base.model.widget.Object): 

75 """Select widget object.""" 

76 

77 def props(self, user): 

78 # fmt: off 

79 items = [ 

80 ListItem(value=it.value, text=it.text or str(it.value)) 

81 for it in self.cfg('items', default=[]) 

82 ] 

83 # fmt: on 

84 return gws.u.merge( 

85 super().props(user), 

86 items=items, 

87 withSearch=bool(self.cfg('withSearch')), 

88 )