Giter Site home page Giter Site logo

chiptextfield's Introduction

ChipTextField

Editable and customizable chips for Compose Multiplatform. Available on these platforms:

  • Android (Jetpack Compose)
  • Desktop
  • Browser (wasmJs and canvas)

iOS support is currently lacking, see #131.

Usage

Gradle dependency Maven Central:

// Material 2
implementation "io.github.dokar3:chiptextfield:latest_version"

// Material 3
implementation "io.github.dokar3:chiptextfield-m3:latest_version"

Default (filled style)

var value by remember { mutableStateOf("Initial text") }
val state = rememberChipTextFieldState<Chip>()
ChipTextField(
    state = state,
    value = value,
    onValueChange = { value = it },
    onSubmit = { text -> Chip(text) },
)

Simplified version if do not care about the text field value:

val state = rememberChipTextFieldState<Chip>()
ChipTextField(
    state = state,
    onSubmit = ::Chip,
)

Outlined

val state = rememberChipTextFieldState<Chip>()
OutlinedChipTextField(
    state = state,
    onSubmit = ::Chip,
)

Need a classic underline style?

val state = rememberChipTextFieldState<Chip>()
ChipTextField(
    state = state,
    onSubmit = ::Chip,
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = Color.Transparent,
    ),
    contentPadding = PaddingValues(bottom = 8.dp),
)

Checkable chips

class CheckableChip(text: String, isChecked: Boolean = false) : Chip(text) {
    var isChecked by mutableStateOf(isChecked)
}

val state = rememberChipTextFieldState(
    chips = listOf(CheckableChip(""), /*...*/),
)
BasicChipTextField(
    state = state,
    onSubmit = { null },
    readOnly = true, // Disable editing
    chipLeadingIcon = { chip -> CheckIcon(chip) }, // Show check icon if checked
    chipTrailingIcon = {}, // Hide default close button
    onChipClick = { chip -> chip.isChecked = !chip.isChecked }
)

@Composable
fun CheckIcon(chip: CheckableChip, modifier: Modifier = Modifier) { /*...*/ }

Avatar chips

class AvatarChip(text: String, val avatarUrl: String) : Chip(text)

val state = rememberChipTextFieldState<AvatarChip>()
ChipTextField(
    state = state,
    onSubmit = { AvatarChip(it.text, AVATAR_URL) },
    chipLeadingIcon = { chip -> Avatar(chip) } // Load and display avatar
)

@Composable
fun Avatar(chip: AvatarChip, modifier: Modifier = Modifier) { /*...*/ }

Material 3

- import com.dokar.chiptextfield.OutlinedChipTextField
+ import com.dokar.chiptextfield.m3.OutlinedChipTextField

License

Copyright 2021 dokar3

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

chiptextfield's People

Contributors

dependabot[bot] avatar ditqk avatar dokar3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

chiptextfield's Issues

maxLines parameter

Hello,

How can I control the maxLines parameter, present in the TexField?

Thank you in advance.

Trying to keep List<String> and ChipTextFieldState in sync

(whoops, pressed Enter accidentally ๐Ÿ˜…)

I'm currently making an app for a school project and using ChipTextField as it looks awesome!
The basic app is almost like the Inventory App Example from the Compose Tutorial.

Please keep in mind, I'm quite new to Kotlin / Jetpack Compose / Android development.

My code (Screen):

        val chipState = rememberChipTextFieldState<Chip>()
        var syncTags by remember { mutableStateOf(false) }
        OutlinedChipTextField(
            state = chipState,
            onSubmit = {
                syncTags = true
                Chip(it)
            },
            enabled = enabled,
            label = { Text(stringResource(R.string.form_field_tags_header)) },
        )
        DisposableEffect(chipState.chips, syncTags) {
            if (syncTags) {
                appointmentDetails.tags = chipState.chips.map { it.text }
                syncTags = false
            }

            onDispose {
                appointmentDetails.tags = chipState.chips.map { it.text }
            }
        }

My code (ViewModel):

data class AppointmentDetails(
    val id: Int = 0,
    val title: String = "",
    val description: String = "",
    val date: LocalDate = LocalDate.now(),
    val time: LocalTime = LocalTime.now().noSeconds(),
    var tags: List<String> = listOf()
)

My issue is, I want to have my tags all saved in the appointmentDetails.tags list. But as I didn't found out how to make that with the ChipTextFieldState, I had to ask ChatGPT, which came up with DisposableEffect. This works... kinda.
When entering stuff, it works! But when editing something (loading from the appointmentDetails.tags), it doesn't show anything.

So, my question: Is there a way to somehow keep the ChipTextFieldState in sync with the tags List?

Focusing a non-empty ChipTextField

Hello, I have a non-empty ChipTextField that I would like to auto focus when presented. I'm using code similar to this:

 val state = rememberChipTextFieldState(initialSelectedLabels.map {
    LabelChipView(it)
  })

  val focusRequester = remember { FocusRequester() }

  ChipTextField(
    state = state,
    modifier = Modifier .focusRequester(focusRequester)
  )

  LaunchedEffect(Unit) {
    focusRequester.requestFocus()
  }

When I run this the text is focused in the first chip in the text field. Is there a way to pass the focus to the very end of the text field?

Feature request: Support KMP

Hi, It would be nice if you could update the project to support KMP, probably it doesn't require much work since m3 is already supported there. From what I saw the only Android specific thing would be awaitFrame

`No virtual method unbox-impl()Ljava/lang/Object;` in v0.7.0-alpha01

The exception:

java.lang.NoSuchMethodError: No virtual method unbox-impl()Ljava/lang/Object; in class Landroidx/compose/ui/input/key/KeyEvent; or its super classes
                                                                	at com.dokar.chiptextfield.BasicChipTextFieldKt$Input$4$1.invoke(BasicChipTextField.kt:612)
                                                                	at androidx.compose.ui.input.key.KeyInputNode.onPreKeyEvent-ZmokQxo(KeyInputModifier.kt:81)

Caused by the wrong publish configuration.

Crash on AGP 8.2.0

AGP 8.2.0 and the latest version of your library crashes at runtime while minify is enabled and debuggable disabled.
But seriously, why is reflection used in this library?

Non-fatal Exception: java.lang.NoSuchFieldException: No field focusedTextColor in class Landroidx/compose/material3/cd; (declaration of 'androidx.compose.material3.cd' appears in /data/app/~~T11x0NL06y8UFu6aZRE_2A==/eu.lepicekmichal.fooswars-uWw35dXM74BkibnmcsOXAQ==/base.apk)
       at java.lang.Class.getDeclaredField(Class.java)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt$textFieldColorsFields$2.invoke$field(TextFieldColorsConverter.kt:120)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt$textFieldColorsFields$2.invoke(TextFieldColorsConverter.kt:122)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt$textFieldColorsFields$2.invoke(TextFieldColorsConverter.kt:118)
       at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt.getTextFieldColorsFields(TextFieldColorsConverter.kt:118)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt.readColors(TextFieldColorsConverter.kt:82)
       at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt.toChipTextFieldColors(TextFieldColorsConverter.kt:16)
       at com.dokar.chiptextfield.m3.ChipTextFieldKt.ChipTextField-ssrxHw0(ChipTextField.kt:290)
       at com.dokar.chiptextfield.m3.ChipTextFieldKt.ChipTextField-ssrxHw0(ChipTextField.kt:200)

Bug

Bug occurs with implementation 'androidx.compose.ui:ui:1.5.1' and implementation 'io.github.dokar3:chiptextfield:0.6.0' downgrading to 0.5.0 vesions and 0.4.9 give no result.

FATAL EXCEPTION: main
Process: com.easemble, PID: 10236
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/compose/ui/platform/LocalSoftwareKeyboardController;
at com.dokar.chiptextfield.BasicChipTextFieldKt.BasicChipTextField-6hQAwQA(BasicChipTextField.kt:302)
at com.dokar.chiptextfield.OutlinedChipTextFieldKt.OutlinedChipTextField-oTzSxmA(OutlinedChipTextField.kt:274)
at com.dokar.chiptextfield.OutlinedChipTextFieldKt.OutlinedChipTextField-QSzSp5g(OutlinedChipTextField.kt:81)
at
com.easemble.screens.setYourProfileScreen.SetYourProfileScreenKt$SetYourProfileScreen$3$2$7.invoke(SetYourProfileScreen.kt:467)
at
com.easemble.screens.setYourProfileScreen.SetYourProfileScreenKt$SetYourProfileScreen$3$2$7.invoke(SetYourProfileScreen.kt:461)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:117)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:35)
at androidx.compose.material.ExposedDropdownMenuKt.ExposedDropdownMenuBox(ExposedDropdownMenu.kt:135)

Add support for material 3

Can't use this library on material 3 currently

In my app I am using material 3 only. I don't want to mix both material 3 and 2.

java.lang.NoSuchMethodError: No virtual method containerColor$material3_release

Trying to use your ChipTextField M3 composable with the latest alpha version of Compose Material3 (1.3.0-alpha04) in my application results in the following runtime exception:

FATAL EXCEPTION: main

Process: com.edricchan.studybuddy.debug, PID: 15038

java.lang.NoSuchMethodError: No virtual method containerColor$material3_release(ZZLandroidx/compose/foundation/interaction/InteractionSource;Landroidx/compose/runtime/Composer;I)Landroidx/compose/runtime/State; in class Landroidx/compose/material3/TextFieldColors; or its super classes (declaration of 'androidx.compose.material3.TextFieldColors' appears in /data/app/~~vbA7OQNACTyE_6u8mGXFWA==/com.edricchan.studybuddy.debug-Bon-hhim6VYMu3d0aAH8JA==/base.apk)
at com.dokar.chiptextfield.m3.TextFieldColorsConverterKt$toChipTextFieldColors$1.backgroundColor(TextFieldColorsConverter.kt:38)
at com.dokar.chiptextfield.m3.OutlinedChipTextFieldKt.OutlinedChipTextField-oTzSxmA(OutlinedChipTextField.kt:273)
at com.dokar.chiptextfield.m3.OutlinedChipTextFieldKt.OutlinedChipTextField-oTzSxmA(OutlinedChipTextField.kt:186)
...

Looking at the source code, I believe this is because the containerColor method that's used in the TextFieldColorsConverter.kt file has since been made to return a Color rather than a State<Color>:

@Composable
override fun backgroundColor(
enabled: Boolean,
isError: Boolean,
interactionSource: InteractionSource
): State<Color> {
return this@toChipTextFieldColors.containerColor(
enabled = enabled,
isError = isError,
interactionSource = interactionSource,
)
}

(I presume this change most likely landed in 1.3.0-alpha02, but there's no user-facing release note so I can't confirm that)

OutlinedChipTextField auto can't be removed- when using in a form

i am using this libarary and to get chips in outlined textfields . (OutlinedChipTextFiled)
while using this with other compose outlined TextFields it get the focus when navigation to that form screen.

Trying to stop that from happening. as my top field are getting scrolled to bottom and get focused on OutlinedChipTextField().

Any Help/support/feedback on this -

iOS support

Not ready yet, but as always, it's open to contribute!

Support MVVM approch

Currently, chips are handled by ChipTextFieldState internally.
Chips should come from outside like ViewModel.
Also there should be Events or Listeners while removing chips that is currently handled internally.

For example, LIKE TextField's value and onValueChanged design (unidirectional)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.