Skip to content

Add example of date picker textfield opening picker dialog on click #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package com.example.compose.snippets.components

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.awaitEachGesture
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.waitForUpOrCancellation
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -49,12 +52,24 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Popup
import com.example.compose.snippets.touchinput.userinteractions.MyAppTheme
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

@Preview
@Composable
private fun DatePickerPreview() {
MyAppTheme {
DatePickerExamples()
}
}

// [START android_compose_components_datepicker_examples]
// [START_EXCLUDE]
@Composable
Expand All @@ -77,6 +92,9 @@ fun DatePickerExamples() {
Text("Docked date picker:")
DatePickerDocked()

Text("Open modal picker on click")
DatePickerFieldToModal()

Text("Modal date pickers:")
Button(onClick = { showModal = true }) {
Text("Show Modal Date Picker")
Expand Down Expand Up @@ -259,6 +277,43 @@ fun DatePickerDocked() {
}
}

@Composable
fun DatePickerFieldToModal(modifier: Modifier = Modifier) {
var selectedDate by remember { mutableStateOf<Long?>(null) }
var showModal by remember { mutableStateOf(false) }

OutlinedTextField(
value = selectedDate?.let { convertMillisToDate(it) } ?: "",
onValueChange = { },
label = { Text("DOB") },
placeholder = { Text("MM/DD/YYYY") },
trailingIcon = {
Icon(Icons.Default.DateRange, contentDescription = "Select date")
},
modifier = modifier
.fillMaxWidth()
.pointerInput(selectedDate) {
awaitEachGesture {
// Modifier.clickable doesn't work for text fields, so we use Modifier.pointerInput
// in the Initial pass to observe events before the text field consumes them
// in the Main pass.
awaitFirstDown(pass = PointerEventPass.Initial)
val upEvent = waitForUpOrCancellation(pass = PointerEventPass.Initial)
if (upEvent != null) {
showModal = true
}
}
}
)

if (showModal) {
DatePickerModal(
onDateSelected = { selectedDate = it },
onDismiss = { showModal = false }
)
}
}

fun convertMillisToDate(millis: Long): String {
val formatter = SimpleDateFormat("MM/dd/yyyy", Locale.getDefault())
return formatter.format(Date(millis))
Expand Down