Skip to content

Commit

Permalink
feat: add category dropdown component
Browse files Browse the repository at this point in the history
This commit introduces a new CategoryDropDown component that allows users to select a job category from a dropdown list.

- The component
 displays an outlined text field with a dropdown icon.
- When clicked, it expands to show a list of available categories.
- Selecting a category triggers an event to update the selected category state.
  • Loading branch information
OkelloSam21 committed Aug 11, 2024
1 parent 5399c77 commit fae89f5
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.samuelokello.kazihub.presentation.business.home.components

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExposedDropdownMenuBox
import androidx.compose.material3.ExposedDropdownMenuDefaults
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.samuelokello.kazihub.domain.model.job.category.Category
import com.samuelokello.kazihub.presentation.business.job.event.CreateJobUiEvent

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CategoryDropDown(
categories: List<Category>,
selectedCategory: Category?,
onCreateJobUiEvent: (CreateJobUiEvent) -> Unit,
) {
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = !expanded },
) {
OutlinedTextField(
value = selectedCategory?.name?:"Select Category",
onValueChange = { },
readOnly = true,
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
colors = ExposedDropdownMenuDefaults.outlinedTextFieldColors(),
shape = RoundedCornerShape(10.dp),
modifier = Modifier
.menuAnchor()
.fillMaxWidth(),
)

ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
modifier = Modifier.exposedDropdownSize(),
) {
categories.forEach { category ->
DropdownMenuItem(
onClick = {
onCreateJobUiEvent(CreateJobUiEvent.OnCategoryChange(category))
expanded = false
},
text = {
Text(text = category.name)
},
)
}
}
}
}

0 comments on commit fae89f5

Please sign in to comment.