|
| 1 | +from typing import List, Optional,Union |
| 2 | +from pydantic import Field, field_validator, validator |
| 3 | +from zt_backend.models.components.zt_component import ZTComponent |
| 4 | +from zt_backend.models.components.validations import validate_color |
| 5 | +from zt_backend.models.state.user_state import UserContext |
| 6 | + |
| 7 | +class Switch(ZTComponent): |
| 8 | + """A slider component that allows a user to select a range of values""" |
| 9 | + component: str = Field("v-switch", description="Vue component name") |
| 10 | + value: str = Field ('', description="The input text value") |
| 11 | + hint: Optional[str] = Field('', description="Hint text for switch") |
| 12 | + disabled: Optional[bool] = Field(None, description="If true, the input is disabled") |
| 13 | + color: str = Field('primary', pre=True, description="Color of the switch. Can be custom or standard Material color") |
| 14 | + label: Optional[str] = Field(None,description= 'A label for your switch') |
| 15 | + multiple: Optional[bool] = Field(None, description="Determines if multiple selections are allowed") |
| 16 | + width: Union[int,str] = Field('100%', description="Width of the switch") |
| 17 | + triggerEvent: str = Field('update:modelValue',description="Trigger event for when to trigger a run") |
| 18 | + readonly: Optional[bool] = Field(None, description="Determines if the Switch is read-only") |
| 19 | + |
| 20 | + @field_validator('color') |
| 21 | + def validate_color(cls, color): |
| 22 | + return validate_color(color) |
| 23 | + |
| 24 | + @validator('value', always=True) #TODO: debug and replace with field validator |
| 25 | + def get_value_from_global_state(cls, value, values): |
| 26 | + id = values['id'] # Get the id if it exists in the field values |
| 27 | + execution_state = UserContext.get_state() |
| 28 | + try: |
| 29 | + if execution_state and id and id in execution_state.component_values: # Check if id exists in global_state |
| 30 | + return execution_state.component_values[id] # Return the value associated with id in global_state |
| 31 | + except Exception as e: |
| 32 | + e |
| 33 | + return value # If id doesn't exist in global_state, return the original value |
| 34 | + |
0 commit comments