mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-12-26 02:23:21 +03:00
Added "label" property for entity-form field. (#93)
This commit is contained in:
parent
138ea983cb
commit
1beb49f2b2
@ -72,7 +72,9 @@ export default class {= formName =} extends React.Component {
|
|||||||
{=# show =}
|
{=# show =}
|
||||||
<div>
|
<div>
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
label="{= name =}"
|
{=# label =}
|
||||||
|
label="{= label =}"
|
||||||
|
{=/ label =}
|
||||||
control={
|
control={
|
||||||
<Switch
|
<Switch
|
||||||
checked={this.getField('{= name =}')}
|
checked={this.getField('{= name =}')}
|
||||||
@ -88,7 +90,9 @@ export default class {= formName =} extends React.Component {
|
|||||||
{=# show =}
|
{=# show =}
|
||||||
<div>
|
<div>
|
||||||
<TextField
|
<TextField
|
||||||
label="{= name =}"
|
{=# label =}
|
||||||
|
label="{= label =}"
|
||||||
|
{=/ label =}
|
||||||
{=# placeholder =}
|
{=# placeholder =}
|
||||||
placeholder="{= placeholder =}"
|
placeholder="{= placeholder =}"
|
||||||
{=/ placeholder =}
|
{=/ placeholder =}
|
||||||
|
@ -52,6 +52,7 @@ entity-form<Task> NewTaskForm {
|
|||||||
fields: {
|
fields: {
|
||||||
description: {
|
description: {
|
||||||
show: true,
|
show: true,
|
||||||
|
label: none,
|
||||||
placeholder: "What needs to be done?"
|
placeholder: "What needs to be done?"
|
||||||
},
|
},
|
||||||
isDone: {
|
isDone: {
|
||||||
|
@ -46,6 +46,7 @@ data FormFieldTemplateData = FormFieldTemplateData
|
|||||||
, _fieldShow :: !Bool
|
, _fieldShow :: !Bool
|
||||||
, _fieldDefaultValue :: !WEF.DefaultValue
|
, _fieldDefaultValue :: !WEF.DefaultValue
|
||||||
, _fieldPlaceholder :: Maybe String
|
, _fieldPlaceholder :: Maybe String
|
||||||
|
, _fieldLabel :: Maybe String
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
instance ToJSON FormFieldTemplateData where
|
instance ToJSON FormFieldTemplateData where
|
||||||
@ -58,6 +59,7 @@ instance ToJSON FormFieldTemplateData where
|
|||||||
(WEF.DefaultValueString s) -> s
|
(WEF.DefaultValueString s) -> s
|
||||||
(WEF.DefaultValueBool b) -> Util.toLowerFirst $ show b
|
(WEF.DefaultValueBool b) -> Util.toLowerFirst $ show b
|
||||||
, "placeholder" .= _fieldPlaceholder f
|
, "placeholder" .= _fieldPlaceholder f
|
||||||
|
, "label" .= _fieldLabel f
|
||||||
]
|
]
|
||||||
|
|
||||||
-- | Given entity and an entity form for it, creates a single data structure
|
-- | Given entity and an entity form for it, creates a single data structure
|
||||||
@ -89,6 +91,7 @@ createFormFieldTD entityForm entityField = FormFieldTemplateData
|
|||||||
id
|
id
|
||||||
$ formFieldConfig >>= WEF._fieldDefaultValue
|
$ formFieldConfig >>= WEF._fieldDefaultValue
|
||||||
, _fieldPlaceholder = formFieldConfig >>= WEF._fieldPlaceholder
|
, _fieldPlaceholder = formFieldConfig >>= WEF._fieldPlaceholder
|
||||||
|
, _fieldLabel = fieldLabel
|
||||||
}
|
}
|
||||||
where
|
where
|
||||||
-- Configuration of a form field within entity-form, if there is any.
|
-- Configuration of a form field within entity-form, if there is any.
|
||||||
@ -105,6 +108,16 @@ createFormFieldTD entityForm entityField = FormFieldTemplateData
|
|||||||
defaultValueIfNothingInForm =
|
defaultValueIfNothingInForm =
|
||||||
getDefaultValueForFieldWithType $ Wasp.entityFieldType entityField
|
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.
|
-- | Generates entity creation form.
|
||||||
generateEntityCreateForm :: Wasp -> WEF.EntityForm -> FD.FileDraft
|
generateEntityCreateForm :: Wasp -> WEF.EntityForm -> FD.FileDraft
|
||||||
|
@ -8,7 +8,7 @@ module Parser.Entity.EntityForm
|
|||||||
, submitConfig
|
, submitConfig
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Text.Parsec (choice)
|
import Text.Parsec (choice, (<|>))
|
||||||
import Text.Parsec.String (Parser)
|
import Text.Parsec.String (Parser)
|
||||||
|
|
||||||
import qualified Wasp.EntityForm as WEF
|
import qualified Wasp.EntityForm as WEF
|
||||||
@ -123,12 +123,14 @@ createFieldConfig (fieldName, options) = WEF.Field
|
|||||||
, WEF._fieldShow = maybeGetFieldOptionShow options
|
, WEF._fieldShow = maybeGetFieldOptionShow options
|
||||||
, WEF._fieldDefaultValue = maybeGetFieldOptionDefaultValue options
|
, WEF._fieldDefaultValue = maybeGetFieldOptionDefaultValue options
|
||||||
, WEF._fieldPlaceholder = maybeGetFieldOptionPlaceholder options
|
, WEF._fieldPlaceholder = maybeGetFieldOptionPlaceholder options
|
||||||
|
, WEF._fieldLabel = maybeGetFieldOptionLabel options
|
||||||
}
|
}
|
||||||
|
|
||||||
data FieldOption
|
data FieldOption
|
||||||
= FieldOptionShow Bool
|
= FieldOptionShow Bool
|
||||||
| FieldOptionDefaultValue WEF.DefaultValue
|
| FieldOptionDefaultValue WEF.DefaultValue
|
||||||
| FieldOptionPlaceholder String
|
| FieldOptionPlaceholder String
|
||||||
|
| FieldOptionLabel (Maybe String)
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
-- | Parses a single field option, e.g. "show" or "defaultValue".
|
-- | Parses a single field option, e.g. "show" or "defaultValue".
|
||||||
@ -137,6 +139,7 @@ fieldOption = choice
|
|||||||
[ FieldOptionShow <$> P.waspPropertyBool "show"
|
[ FieldOptionShow <$> P.waspPropertyBool "show"
|
||||||
, FieldOptionDefaultValue <$> defaultValue
|
, FieldOptionDefaultValue <$> defaultValue
|
||||||
, FieldOptionPlaceholder <$> P.waspPropertyStringLiteral "placeholder"
|
, FieldOptionPlaceholder <$> P.waspPropertyStringLiteral "placeholder"
|
||||||
|
, FieldOptionLabel <$> fieldOptionLabel
|
||||||
]
|
]
|
||||||
|
|
||||||
defaultValue :: Parser WEF.DefaultValue
|
defaultValue :: Parser WEF.DefaultValue
|
||||||
@ -145,6 +148,11 @@ defaultValue = P.waspProperty "defaultValue" $ choice
|
|||||||
, WEF.DefaultValueBool <$> L.bool
|
, 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 :: [FieldOption] -> Maybe Bool
|
||||||
maybeGetFieldOptionShow options = U.headSafe [b | FieldOptionShow b <- options]
|
maybeGetFieldOptionShow options = U.headSafe [b | FieldOptionShow b <- options]
|
||||||
|
|
||||||
@ -153,3 +161,6 @@ maybeGetFieldOptionDefaultValue options = U.headSafe [dv | FieldOptionDefaultVa
|
|||||||
|
|
||||||
maybeGetFieldOptionPlaceholder :: [FieldOption] -> Maybe String
|
maybeGetFieldOptionPlaceholder :: [FieldOption] -> Maybe String
|
||||||
maybeGetFieldOptionPlaceholder options = U.headSafe [s | FieldOptionPlaceholder s <- options]
|
maybeGetFieldOptionPlaceholder options = U.headSafe [s | FieldOptionPlaceholder s <- options]
|
||||||
|
|
||||||
|
maybeGetFieldOptionLabel :: [FieldOption] -> Maybe (Maybe String)
|
||||||
|
maybeGetFieldOptionLabel options = U.headSafe [ms | FieldOptionLabel ms <- options]
|
||||||
|
@ -66,6 +66,23 @@ data Field = Field
|
|||||||
, _fieldShow :: Maybe Bool
|
, _fieldShow :: Maybe Bool
|
||||||
, _fieldDefaultValue :: Maybe DefaultValue
|
, _fieldDefaultValue :: Maybe DefaultValue
|
||||||
, _fieldPlaceholder :: Maybe String
|
, _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)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
data DefaultValue
|
data DefaultValue
|
||||||
|
@ -61,4 +61,5 @@ formFieldIsDone = GEF.FormFieldTemplateData
|
|||||||
, GEF._fieldShow = True
|
, GEF._fieldShow = True
|
||||||
, GEF._fieldDefaultValue = EF.DefaultValueBool True
|
, GEF._fieldDefaultValue = EF.DefaultValueBool True
|
||||||
, GEF._fieldPlaceholder = Nothing
|
, GEF._fieldPlaceholder = Nothing
|
||||||
|
, GEF._fieldLabel = Nothing
|
||||||
}
|
}
|
||||||
|
@ -78,12 +78,14 @@ spec_parseWasp =
|
|||||||
{ EF._fieldName = "description"
|
{ EF._fieldName = "description"
|
||||||
, EF._fieldShow = Just True
|
, EF._fieldShow = Just True
|
||||||
, EF._fieldDefaultValue = Just $ EF.DefaultValueString "doable task"
|
, EF._fieldDefaultValue = Just $ EF.DefaultValueString "doable task"
|
||||||
|
, EF._fieldLabel = Just Nothing
|
||||||
, EF._fieldPlaceholder = Just "What will you do?"
|
, EF._fieldPlaceholder = Just "What will you do?"
|
||||||
}
|
}
|
||||||
, EF.Field
|
, EF.Field
|
||||||
{ EF._fieldName = "isDone"
|
{ EF._fieldName = "isDone"
|
||||||
, EF._fieldShow = Just False
|
, EF._fieldShow = Just False
|
||||||
, EF._fieldDefaultValue = Just $ EF.DefaultValueBool False
|
, EF._fieldDefaultValue = Just $ EF.DefaultValueBool False
|
||||||
|
, EF._fieldLabel = Nothing
|
||||||
, EF._fieldPlaceholder = Nothing
|
, EF._fieldPlaceholder = Nothing
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -54,6 +54,7 @@ entity-form<Task> CreateTaskForm {
|
|||||||
description: {
|
description: {
|
||||||
show: true,
|
show: true,
|
||||||
defaultValue: "doable task",
|
defaultValue: "doable task",
|
||||||
|
label: none,
|
||||||
placeholder: "What will you do?"
|
placeholder: "What will you do?"
|
||||||
},
|
},
|
||||||
isDone: {
|
isDone: {
|
||||||
|
Loading…
Reference in New Issue
Block a user