generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 11
kmdc-select (#22) #80
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1b0e85f
kmdc-select (#22)
Kiryushin-Andrey 4005b00
Merge branch 'master' into mdc-select
mpetuska f528419
Address review comments
Kiryushin-Andrey d95d6a9
fixup! Address review comments
Kiryushin-Andrey 4a9dfa8
Merge branch 'master' into mdc-select
mpetuska 54d46a6
Address review comments - part 2
Kiryushin-Andrey 049c188
Some minor changes
Kiryushin-Andrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import util.mdcVersion | ||
|
||
plugins { | ||
id("plugin.library-compose") | ||
id("plugin.publishing-mpp") | ||
} | ||
|
||
description = "Compose Multiplatform Kotlin/JS wrappers for @material/select" | ||
|
||
kotlin { | ||
sourceSets { | ||
commonMain | ||
jsMain { | ||
dependencies { | ||
api(project(":kmdc:kmdc-core")) | ||
api(project(":kmdc:kmdc-list")) | ||
api(project(":kmdc:kmdc-menu-surface")) | ||
api(npm("@material/select", mdcVersion)) | ||
api(compose.web.svg) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package dev.petuska.kmdc.select | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.petuska.kmdc.core.Builder | ||
import dev.petuska.kmdc.core.MDCDsl | ||
import dev.petuska.kmdc.core.aria | ||
import dev.petuska.kmdc.core.initialiseMDC | ||
import dev.petuska.kmdc.core.mdc | ||
import dev.petuska.kmdc.core.rememberUniqueDomElementId | ||
import dev.petuska.kmdc.core.role | ||
import dev.petuska.kmdc.list.MDCList | ||
import dev.petuska.kmdc.list.MDCListItem | ||
import dev.petuska.kmdc.list.MDCListItemGraphic | ||
import dev.petuska.kmdc.list.MDCListItemText | ||
import dev.petuska.kmdc.menu.surface.MDCMenuSurface | ||
import org.jetbrains.compose.web.attributes.AttrsBuilder | ||
import org.jetbrains.compose.web.attributes.name | ||
import org.jetbrains.compose.web.dom.Div | ||
import org.jetbrains.compose.web.dom.ElementScope | ||
import org.jetbrains.compose.web.dom.HiddenInput | ||
import org.jetbrains.compose.web.dom.Text | ||
import org.w3c.dom.HTMLDivElement | ||
import org.w3c.dom.HTMLSpanElement | ||
|
||
@JsModule("@material/select/dist/mdc.select.css") | ||
private external val MDCSelectCSS: dynamic | ||
|
||
public data class MDCSelectOpts<T>( | ||
var type: Type = Type.Filled, | ||
var value: T? = null, | ||
var itemValue: T.() -> String = { toString() }, | ||
var itemDisabled: T.() -> Boolean = { false }, | ||
var label: String? = null, | ||
var required: Boolean = false, | ||
var disabled: Boolean = false, | ||
var hiddenInputName: String? = null, | ||
var helperText: String? = null, | ||
var helperTextType: HelperTextType = HelperTextType.Default, | ||
var leadingIcon: String? = null, | ||
var leadingIconClickable: Boolean = false, | ||
var leadingIconClasses: List<String> = listOf("material-icons") | ||
) { | ||
public enum class Type(public val klass: String) { | ||
Outlined("mdc-select--outlined"), | ||
Filled("mdc-select--filled") | ||
} | ||
|
||
public enum class HelperTextType(public vararg val classes: String) { | ||
Default, | ||
Validation("mdc-select-helper-text--validation-msg"), | ||
PersistentValidation("mdc-select-helper-text--validation-msg", "mdc-select-helper-text--validation-msg-persistent") | ||
} | ||
} | ||
|
||
public class MDCSelectAttrsScope<T> private constructor() : AttrsBuilder<HTMLDivElement>() | ||
|
||
/** | ||
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-select) | ||
*/ | ||
@MDCDsl | ||
@Composable | ||
public fun <T> MDCSelect( | ||
items: List<T>, | ||
opts: Builder<MDCSelectOpts<T>>? = null, | ||
attrs: Builder<MDCSelectAttrsScope<T>>? = null, | ||
renderItem: @Composable ElementScope<HTMLSpanElement>.(T) -> Unit = { Text(it.toString()) } | ||
) { | ||
MDCSelectCSS | ||
val labelId = rememberUniqueDomElementId() | ||
val selectedTextId = rememberUniqueDomElementId() | ||
val helperTextId = rememberUniqueDomElementId() | ||
val options = MDCSelectOpts<T>().apply { opts?.invoke(this) } | ||
val hasLeadingIcon = options.leadingIcon != null | ||
|
||
fun T.itemValue() = with(options) { itemValue() } | ||
fun T.itemDisabled() = with(options) { itemDisabled() } | ||
|
||
Div( | ||
attrs = { | ||
with(options) { | ||
classes("mdc-select", type.klass) | ||
if (label == null) classes("mdc-select--no-label") | ||
if (required) classes("mdc-select--required") | ||
if (disabled) classes("mdc-select--disabled") | ||
if (required && value?.itemValue().isNullOrBlank()) classes("mdc-select--invalid") | ||
if (hasLeadingIcon) classes("mdc-select--with-leading-icon") | ||
if (helperText != null) { | ||
aria("controls", helperTextId) | ||
aria("describedby", helperTextId) | ||
} | ||
} | ||
initialiseMDC( | ||
mdcInit = { MDCSelectModule.MDCSelect.attachTo<T>(it) }, | ||
postInit = { _, mdc -> mdc.items = items } | ||
) | ||
attrs?.invoke(this.unsafeCast<MDCSelectAttrsScope<T>>()) | ||
} | ||
) { | ||
|
||
DomSideEffect(options.required) { | ||
it.mdc<MDCSelectModule.MDCSelect<T>> { required = options.required } | ||
} | ||
DomSideEffect(options.disabled) { | ||
it.mdc<MDCSelectModule.MDCSelect<T>> { disabled = options.disabled } | ||
} | ||
DomSideEffect(options.value) { | ||
it.mdc<MDCSelectModule.MDCSelect<T>> { value = options.value?.itemValue() } | ||
} | ||
|
||
options.hiddenInputName?.let { | ||
HiddenInput { name(it) } | ||
} | ||
|
||
MDCSelectAnchor(labelId, options, selectedTextId, items, renderItem) | ||
|
||
MDCMenuSurface( | ||
opts = { fullwidth = true }, | ||
attrs = { | ||
classes("mdc-select__menu", "mdc-menu") | ||
} | ||
) { | ||
MDCList( | ||
opts = { singleSelection = true }, | ||
attrs = { | ||
options.label?.let { aria("label", it) } | ||
} | ||
) { | ||
items.forEach { item -> | ||
mpetuska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
MDCListItem( | ||
opts = { | ||
this.selected = item == options.value | ||
this.disabled = item.itemDisabled() | ||
}, | ||
attrs = { | ||
attr("data-value", item.itemValue()) | ||
aria("selected", (item == options.value).toString()) | ||
if (item.itemDisabled()) aria("disabled", "true") | ||
role("option") | ||
} | ||
) { | ||
if (hasLeadingIcon) { | ||
MDCListItemGraphic() | ||
mpetuska marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
MDCListItemText { | ||
renderItem(item) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
options.helperText?.let { helperText -> | ||
MDCSelectHelperText(helperTextId, helperText, options.helperTextType) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package dev.petuska.kmdc.select | ||
|
||
import androidx.compose.runtime.Composable | ||
AE20 | import dev.petuska.kmdc.core.aria | |
import dev.petuska.kmdc.core.role | ||
import org.jetbrains.compose.web.dom.Div | ||
import org.jetbrains.compose.web.dom.ElementScope | ||
import org.jetbrains.compose.web.dom.I | ||
import org.jetbrains.compose.web.dom.Span | ||
import org.jetbrains.compose.web.dom.Text | ||
import org.w3c.dom.HTMLSpanElement | ||
|
||
@Composable | ||
internal fun <T> MDCSelectAnchor( | ||
labelId: String, | ||
options: MDCSelectOpts<T>, | ||
selectedTextId: String, | ||
items: List<T>, | ||
renderItem: @Composable ElementScope<HTMLSpanElement>.(T) -> Unit | ||
) { | ||
fun T.itemValue() = with(options) { itemValue() } | ||
|
||
Div( | ||
attrs = { | ||
classes("mdc-select__anchor") | ||
role("button") | ||
aria("haspopup", "listbox") | ||
aria("expanded", "false") | ||
aria("labelledby", labelId) | ||
} | ||
) { | ||
|
||
MDCSelectLabel(options, labelId) | ||
|
||
options.leadingIcon?.let { icon -> | ||
MDCSelectLeadingIcon(options, icon) | ||
} | ||
|
||
Span( | ||
attrs = { | ||
classes("mdc-select__selected-text-container") | ||
} | ||
) { | ||
Span( | ||
attrs = { | ||
classes("mdc-select__selected-text") | ||
id(selectedTextId) | ||
} | ||
) { | ||
options.value?.itemValue()?.let { selectedValue -> | ||
items.firstOrNull { it.itemValue() == selectedValue }?.let { | ||
renderItem(it) | ||
} | ||
} | ||
} | ||
} | ||
|
||
MDCSelectDownArrowIcon() | ||
|
||
Span(attrs = { classes("mdc-line-ripple") }) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun <T> MDCSelectLeadingIcon(options: MDCSelectOpts<T>, icon: String) { | ||
I( | ||
attrs = { | ||
classes("mdc-select__icon") | ||
if (options.leadingIconClickable) { | ||
tabIndex(0) | ||
role("button") | ||
} | ||
classes(*options.leadingIconClasses.toTypedArray()) | ||
} | ||
) { | ||
Text(icon) | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
kmdc/kmdc-select/src/jsMain/kotlin/MDCSelectDropdownIcon.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.petuska.kmdc.select | ||
|
||
import androidx.compose.runtime.Composable | ||
import org.jetbrains.compose.web.ExperimentalComposeWebSvgApi | ||
import org.jetbrains.compose.web.dom.Span | ||
import org.jetbrains.compose.web.svg.Polygon | ||
import org.jetbrains.compose.web.svg.Svg | ||
|
||
/** | ||
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-select) | ||
*/ | ||
@OptIn(ExperimentalComposeWebSvgApi::class) | ||
@Composable | ||
internal fun MDCSelectDownArrowIcon() { | ||
Span( | ||
attrs = { classes("mdc-select__dropdown-icon") } | ||
) { | ||
Svg( | ||
viewBox = "7 10 10 5", | ||
attrs = { | ||
attr("focusable", "false") | ||
classes("mdc-select__dropdown-icon-graphic") | ||
} | ||
) { | ||
Polygon( | ||
7, 10, 12, 15, 17, 10, | ||
attrs = { | ||
classes("mdc-select__dropdown-icon-inactive") | ||
attr("stroke", "none") | ||
attr("fill-rule", "evenodd") | ||
} | ||
) | ||
Polygon( | ||
7, 15, 12, 10, 17, 15, | ||
attrs = { | ||
classes("mdc-select__dropdown-icon-active") | ||
attr("stroke", "none") | ||
attr("fill-rule", "evenodd") | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.petuska.kmdc.select | ||
|
||
import androidx.compose.runtime.Composable | ||
import dev.petuska.kmdc.core.initialiseMDC | ||
import org.jetbrains.compose.web.dom.P | ||
import org.jetbrains.compose.web.dom.Text | ||
|
||
@Composable | ||
internal fun MDCSelectHelperText(id: String, text: String, type: MDCSelectOpts.HelperTextType) { | ||
P( | ||
attrs = { | ||
id(id) | ||
classes("mdc-select-helper-text") | ||
classes(*type.classes) | ||
initialiseMDC(MDCSelectModule.MDCSelectHelperText.Companion::attachTo) | ||
} | ||
) { | ||
Text(text) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.petuska.kmdc.select | ||
|
||
import androidx.compose.runtime.Composable | ||
import org.jetbrains.compose.web.dom.Span | ||
import org.jetbrains.compose.web.dom.Text | ||
|
||
/** | ||
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-select) | ||
*/ | ||
@Composable | ||
internal fun <T> MDCSelectLabel( | ||
options: MDCSelectOpts<T>, | ||
labelId: String | ||
) { | ||
when (options.type) { | ||
MDCSelectOpts.Type.Filled -> { | ||
Span(attrs = { classes("mdc-select__ripple") }) | ||
options.label?.let { | ||
MDCSelectFloatingLabel(labelId, it) | ||
} | ||
} | ||
MDCSelectOpts.Type.Outlined -> { | ||
Span( | ||
attrs = { classes("mdc-notched-outline") } | ||
) { | ||
Span(attrs = { classes("mdc-notched-outline__leading") }) | ||
options.label?.let { | ||
Span(attrs = { classes("mdc-notched-outline__notch") }) { | ||
MDCSelectFloatingLabel(labelId, it) | ||
} | ||
} | ||
Span(attrs = { classes("mdc-notched-outline__trailing") }) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* [JS API](https://github.com/material-components/material-components-web/tree/v13.0.0/packages/mdc-select) | ||
*/ | ||
@Composable | ||
private fun MDCSelectFloatingLabel(id: String, label: String) { | ||
Span( | ||
attrs = { | ||
classes("mdc-floating-label", "mdc-floating-label--float-above") | ||
id(id) | ||
} | ||
) { | ||
Text(label) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.