Added "label" property for entity-form field. (#93)

This commit is contained in:
Matija Sosic 2020-02-05 19:04:09 +01:00 committed by GitHub
parent 138ea983cb
commit 1beb49f2b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 3 deletions

View File

@ -72,7 +72,9 @@ export default class {= formName =} extends React.Component {
{=# show =}
<div>
<FormControlLabel
label="{= name =}"
{=# label =}
label="{= label =}"
{=/ label =}
control={
<Switch
checked={this.getField('{= name =}')}
@ -88,7 +90,9 @@ export default class {= formName =} extends React.Component {
{=# show =}
<div>
<TextField
label="{= name =}"
{=# label =}
label="{= label =}"
{=/ label =}
{=# placeholder =}
placeholder="{= placeholder =}"
{=/ placeholder =}

View File

@ -52,6 +52,7 @@ entity-form<Task> NewTaskForm {
fields: {
description: {
show: true,
label: none,
placeholder: "What needs to be done?"
},
isDone: {

View File

@ -46,6 +46,7 @@ data FormFieldTemplateData = FormFieldTemplateData
, _fieldShow :: !Bool
, _fieldDefaultValue :: !WEF.DefaultValue
, _fieldPlaceholder :: Maybe String
, _fieldLabel :: Maybe String
} deriving (Show)
instance ToJSON FormFieldTemplateData where
@ -58,6 +59,7 @@ instance ToJSON FormFieldTemplateData where
(WEF.DefaultValueString s) -> s
(WEF.DefaultValueBool b) -> Util.toLowerFirst $ show b
, "placeholder" .= _fieldPlaceholder f
, "label" .= _fieldLabel f
]
-- | Given entity and an entity form for it, creates a single data structure
@ -89,6 +91,7 @@ createFormFieldTD entityForm entityField = FormFieldTemplateData
id
$ formFieldConfig >>= WEF._fieldDefaultValue
, _fieldPlaceholder = formFieldConfig >>= WEF._fieldPlaceholder
, _fieldLabel = fieldLabel
}
where
-- Configuration of a form field within entity-form, if there is any.
@ -105,6 +108,16 @@ createFormFieldTD entityForm entityField = FormFieldTemplateData
defaultValueIfNothingInForm =
getDefaultValueForFieldWithType $ Wasp.entityFieldType entityField
fieldLabel :: Maybe String
fieldLabel = case (formFieldConfig >>= WEF._fieldLabel) of
-- Label property is not provided -> in that case we set label to the
-- default value of entity field name (e.g. "description").
Nothing -> Just $ Wasp.entityFieldName entityField
-- Label property is provided and explicitly disabled ('label: none').
Just Nothing -> Nothing
-- Label property is provided and set to a specific value ('label: "something"').
Just (Just label) -> Just label
-- | Generates entity creation form.
generateEntityCreateForm :: Wasp -> WEF.EntityForm -> FD.FileDraft

View File

@ -8,7 +8,7 @@ module Parser.Entity.EntityForm
, submitConfig
) where
import Text.Parsec (choice)
import Text.Parsec (choice, (<|>))
import Text.Parsec.String (Parser)
import qualified Wasp.EntityForm as WEF
@ -123,12 +123,14 @@ createFieldConfig (fieldName, options) = WEF.Field
, WEF._fieldShow = maybeGetFieldOptionShow options
, WEF._fieldDefaultValue = maybeGetFieldOptionDefaultValue options
, WEF._fieldPlaceholder = maybeGetFieldOptionPlaceholder options
, WEF._fieldLabel = maybeGetFieldOptionLabel options
}
data FieldOption
= FieldOptionShow Bool
| FieldOptionDefaultValue WEF.DefaultValue
| FieldOptionPlaceholder String
| FieldOptionLabel (Maybe String)
deriving (Show, Eq)
-- | Parses a single field option, e.g. "show" or "defaultValue".
@ -137,6 +139,7 @@ fieldOption = choice
[ FieldOptionShow <$> P.waspPropertyBool "show"
, FieldOptionDefaultValue <$> defaultValue
, FieldOptionPlaceholder <$> P.waspPropertyStringLiteral "placeholder"
, FieldOptionLabel <$> fieldOptionLabel
]
defaultValue :: Parser WEF.DefaultValue
@ -145,6 +148,11 @@ defaultValue = P.waspProperty "defaultValue" $ choice
, WEF.DefaultValueBool <$> L.bool
]
fieldOptionLabel :: Parser (Maybe String)
fieldOptionLabel = P.waspProperty "label" labelValue
where
labelValue = (Just <$> L.stringLiteral) <|> (L.symbol "none" *> pure Nothing)
maybeGetFieldOptionShow :: [FieldOption] -> Maybe Bool
maybeGetFieldOptionShow options = U.headSafe [b | FieldOptionShow b <- options]
@ -153,3 +161,6 @@ maybeGetFieldOptionDefaultValue options = U.headSafe [dv | FieldOptionDefaultVa
maybeGetFieldOptionPlaceholder :: [FieldOption] -> Maybe String
maybeGetFieldOptionPlaceholder options = U.headSafe [s | FieldOptionPlaceholder s <- options]
maybeGetFieldOptionLabel :: [FieldOption] -> Maybe (Maybe String)
maybeGetFieldOptionLabel options = U.headSafe [ms | FieldOptionLabel ms <- options]

View File

@ -66,6 +66,23 @@ data Field = Field
, _fieldShow :: Maybe Bool
, _fieldDefaultValue :: Maybe DefaultValue
, _fieldPlaceholder :: Maybe String
-- NOTE(matija): We use Maybe (Maybe String) here to differentiate between the 3
-- possible states:
--
-- Nothing -> property not provided (by user)
-- Just Nothing -> property was provided and explicitly set to Nothing
-- Just (Just val) -> property was provided and explicitly set to some value.
--
-- We introduced this because we need to differentiate between the case when user did not
-- provide a property (we want to display a label with a default value) and a case when user
-- explicitly disabled the label (we want not to display the label at all).
--
-- This is an experiment, we are not sure if this will prove to be practical. With introducing
-- this new type of "none", the question is where else it can be applied etc.
--
-- Alternative solution would be to introduce another property, e.g. "showLabel: true|false"
-- then we would have avoided the need to introduce this new "type".
, _fieldLabel :: Maybe (Maybe String)
} deriving (Show, Eq)
data DefaultValue

View File

@ -61,4 +61,5 @@ formFieldIsDone = GEF.FormFieldTemplateData
, GEF._fieldShow = True
, GEF._fieldDefaultValue = EF.DefaultValueBool True
, GEF._fieldPlaceholder = Nothing
, GEF._fieldLabel = Nothing
}

View File

@ -78,12 +78,14 @@ spec_parseWasp =
{ EF._fieldName = "description"
, EF._fieldShow = Just True
, EF._fieldDefaultValue = Just $ EF.DefaultValueString "doable task"
, EF._fieldLabel = Just Nothing
, EF._fieldPlaceholder = Just "What will you do?"
}
, EF.Field
{ EF._fieldName = "isDone"
, EF._fieldShow = Just False
, EF._fieldDefaultValue = Just $ EF.DefaultValueBool False
, EF._fieldLabel = Nothing
, EF._fieldPlaceholder = Nothing
}
]

View File

@ -54,6 +54,7 @@ entity-form<Task> CreateTaskForm {
description: {
show: true,
defaultValue: "doable task",
label: none,
placeholder: "What will you do?"
},
isDone: {