|
| 1 | +-- | This example component shows how submitPreventDefault works in Halogen Formless |
| 2 | +module Example.Readme.Component (component) where |
| 3 | + |
| 4 | +import Prelude |
| 5 | + |
| 6 | +import Data.Either (Either(..)) |
| 7 | +import Data.Int as Int |
| 8 | +import Data.Maybe (Maybe(..)) |
| 9 | +import Data.Newtype (class Newtype, unwrap) |
| 10 | +import Effect.Aff.Class (class MonadAff) |
| 11 | +import Effect.Class.Console (logShow) |
| 12 | +import Example.App.UI.Element as UI |
| 13 | +import Example.App.Validation (class ToText) |
| 14 | +import Formless as F |
| 15 | +import Halogen as H |
| 16 | +import Halogen.HTML as HH |
| 17 | +import Halogen.HTML.Events as HE |
| 18 | +import Halogen.HTML.Properties as HP |
| 19 | +import Type.Proxy (Proxy(..)) |
| 20 | + |
| 21 | +type Dog = { name :: String, age :: Age } |
| 22 | + |
| 23 | +newtype Age = Age Int |
| 24 | + |
| 25 | +derive instance newtypeAge :: Newtype Age _ |
| 26 | + |
| 27 | +instance showAge :: Show Age where |
| 28 | + show = show <<< unwrap |
| 29 | + |
| 30 | +data AgeError = TooLow | TooHigh | InvalidInt |
| 31 | + |
| 32 | +newtype DogForm (r :: Row Type -> Type) f = DogForm (r |
| 33 | + -- error input output |
| 34 | + ( name :: f Void String String |
| 35 | + , age :: f AgeError String Age |
| 36 | + )) |
| 37 | + |
| 38 | +derive instance newtypeDogForm :: Newtype (DogForm r f) _ |
| 39 | + |
| 40 | +instance ToText AgeError where |
| 41 | + toText = case _ of |
| 42 | + InvalidInt -> "Age must be an integer" |
| 43 | + TooLow -> "Age cannot be negative" |
| 44 | + TooHigh -> "No dog has lived past 30 before" |
| 45 | + |
| 46 | +input :: forall m. Monad m => F.Input' DogForm m |
| 47 | +input = |
| 48 | + { initialInputs: Nothing -- same as: Just (F.wrapInputFields { name: "", age: "" }) |
| 49 | + , validators: DogForm |
| 50 | + { name: F.noValidation |
| 51 | + , age: F.hoistFnE_ \str -> case Int.fromString str of |
| 52 | + Nothing -> Left InvalidInt |
| 53 | + Just n |
| 54 | + | n < 0 -> Left TooLow |
| 55 | + | n > 30 -> Left TooHigh |
| 56 | + | otherwise -> Right (Age n) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +spec :: forall input m. Monad m => F.Spec' DogForm Dog input m |
| 61 | +spec = F.defaultSpec { render = render, handleEvent = F.raiseResult } |
| 62 | + where |
| 63 | + render st@{ form } = |
| 64 | + UI.formContent_ |
| 65 | + [ HH.form |
| 66 | + [ -- Using a form forces us to deal with an event. Using '\_ -> F.submit' here |
| 67 | + -- would fire the event and cause the page to reload. Instead, we use |
| 68 | + -- 'F.submitPreventDefault' to avoid firing the event unnecessarily |
| 69 | + HE.onSubmit F.submitPreventDefault |
| 70 | + ] |
| 71 | + [ UI.input |
| 72 | + { label: "Name" |
| 73 | + , help: Right "Write your dog's name" |
| 74 | + , placeholder: "Mila" |
| 75 | + } |
| 76 | + [ HP.value $ F.getInput _name st.form |
| 77 | + , HE.onValueInput (F.setValidate _name) |
| 78 | + ] |
| 79 | + , UI.input |
| 80 | + { label: "Age" |
| 81 | + , help: UI.resultToHelp "Write your dog's age" $ F.getResult _age st.form |
| 82 | + , placeholder: "3" |
| 83 | + } |
| 84 | + [ HP.value $ F.getInput _age form |
| 85 | + , HE.onValueInput $ F.setValidate _age |
| 86 | + ] |
| 87 | + , UI.buttonPrimary |
| 88 | + [] |
| 89 | + [ HH.text "Submit" ] |
| 90 | + ] |
| 91 | + ] |
| 92 | + where |
| 93 | + _name = Proxy :: Proxy "name" |
| 94 | + _age = Proxy :: Proxy "age" |
| 95 | + |
| 96 | +data Action = HandleDogForm Dog |
| 97 | + |
| 98 | +component :: forall q i o m. MonadAff m => H.Component q i o m |
| 99 | +component = H.mkComponent |
| 100 | + { initialState: const unit |
| 101 | + , render: const render |
| 102 | + , eval: H.mkEval $ H.defaultEval { handleAction = handleAction } |
| 103 | + } |
| 104 | + where |
| 105 | + handleAction (HandleDogForm dog) = logShow (dog :: Dog) |
| 106 | + |
| 107 | + render = |
| 108 | + UI.section_ |
| 109 | + [ UI.h1_ [ HH.text "Formless" ] |
| 110 | + , UI.h2_ [ HH.text "The form from the readme" ] |
| 111 | + , HH.slot F._formless unit (F.component (const input) spec) unit HandleDogForm |
| 112 | + ] |
0 commit comments