Skip to content

Commit

Permalink
Merge pull request #6546 from uktrade/form-flash-message-type
Browse files Browse the repository at this point in the history
Allow flash message type to be specified in Form
  • Loading branch information
peterhudec authored Feb 22, 2024
2 parents 8835ab5 + ded7548 commit b4e4d77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/client/components/Form/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,25 @@ const dispatchToProps = (dispatch) => ({
type: 'FORM__STEP_DEREGISTER',
stepName,
}),
writeFlashMessage: (message) =>
writeFlashMessage: (message) => {
if (!message) return

if (typeof message === 'string') {
dispatch({
type: 'FLASH_MESSAGE__WRITE_TO_SESSION',
messageType: 'success',
message,
})
return
}

const [heading, body, messageType] = message
dispatch({
type: 'FLASH_MESSAGE__WRITE_TO_SESSION',
messageType: 'success',
message,
}),
messageType,
message: [heading, body],
})
},
})

/**
Expand Down Expand Up @@ -520,7 +533,7 @@ const dispatchToProps = (dispatch) => ({
* `window.location.href`, the _soft_ mode uses React-Router.
* @param {Props['flashMessage']} [props.flashMessage] - A function which should
* convert the result of the submission task and the submitted form values into
* a flash message text or a tuple of `[heading, body]`, which will be used as
* a flash message text or a tuple of `[heading, body, type]`, which will be used as
* a flash message when the submission task resolves.
* @param {Props['initialValues']} [props.initialValues] - An object mapping
* field names to their initial values.
Expand Down
3 changes: 2 additions & 1 deletion src/client/components/Form/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Values = Record<string, string>
type Errors = Record<string, string>
type FlashMessageHeading = string
type FlashMessageBody = string
type FlashMessage = FlashMessageBody | [FlashMessageHeading, FlashMessageBody]
type FlashMessageType = 'info' | 'success' | 'warning' | 'error' | 'muted'
type FlashMessage = FlashMessageBody | [FlashMessageHeading, FlashMessageBody, FlashMessageType?]

type ChildFn = ({
errors: Errors,
Expand Down

0 comments on commit b4e4d77

Please sign in to comment.