Coder Social home page Coder Social logo

Comments (4)

thought2 avatar thought2 commented on June 28, 2024 1

@garyb Agree, UX wise it's not a good idea. Already dropped that idea in my real use case. So no urgency :)
However I thought it's worth mentioning here, because it might be a bug: I checked the example in React and Elm and both work according to my expectation.

In Halogen I tried onValueInput but the behavior is the same. onValueChange works after the first enter hit but not after the second.

from purescript-halogen.

thomashoneyman avatar thomashoneyman commented on June 28, 2024

You're setting the state to its same value (the value was "x", then you type "xa" and set the state to "x"), so no re-render is happening and the input field is diverging from your state. You'll see the behavior you expect so long as your state is changing. To force a state update even if you're setting the text to its same value, you could add an additional field to your state like this:

type State = { value :: String, force :: Int }

and if you are setting your new state to the same value as is present in the old state, you can also increment force. This will guarantee a state update and re-render.

from purescript-halogen.

thought2 avatar thought2 commented on June 28, 2024

I tried this, here's my modified code:

  (...)
  
  initialState :: i -> _
  initialState _ = { str: "", num: 0 }

  render state =
    HH.div []
      [ HH.input
          [ HA.type_ HA.InputText
          , HE.onInput (\event -> (unsafeCoerce event).target.value)
          , HA.value state.str
          ]
      , HH.text $ "state: " <> (show state)
      ]

  handleAction (msg :: String) = do
    modify_ $ \state -> { str: if msg == "xa" then "x" else msg, num: state.num + 1 }

I am still seeing the same behavior:
halogen-bug2

from purescript-halogen.

garyb avatar garyb commented on June 28, 2024

Does it work if you use HE.onValueChange? I think what's happening is the value is being captured in the event cycle too soon, so you'd have to preventDefault the event.

Both onValueChange and onValueInput give you the new value rather than the Event also, saving that questionable unsafeCoerce.

I do have one other thought on this though... don't do it 😄. From a UX perspective preventing input is bad, it's better to allow any input and show a validation message about why it's disallowed instead.

from purescript-halogen.

Related Issues (20)

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.