Skip to content

Commit

Permalink
Merge pull request #1137 from dpc-sdp/feature/rpl-form-hidden-fields
Browse files Browse the repository at this point in the history
[R20-1967] add RplFormHidden component
  • Loading branch information
dylankelly authored May 3, 2024
2 parents 3c92b67 + 4ed71c9 commit 445cacf
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 6 deletions.
1 change: 1 addition & 0 deletions examples/nuxt-app/test/features/landingpage/forms.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Feature: Forms
Then a textarea field with the label "Message" should exist
| help | required |
| Enter your message | true |
Then a hidden field named "site_section" should exist with the value "DPC"

@mockserver
Scenario: Error summary
Expand Down
6 changes: 6 additions & 0 deletions examples/nuxt-app/test/fixtures/landingpage/full-form.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@
"accepted": "You must accept the terms"
}
},
{
"$formkit": "RplFormHidden",
"name": "site_section",
"id": "site_section",
"value": "DPC"
},
{
"$formkit": "RplFormActions",
"name": "submit",
Expand Down
12 changes: 10 additions & 2 deletions packages/nuxt-ripple/components/TideContentRating.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ onMounted(() => {
>
<template #default="{ value }">
<div class="tide-content-rating__rating">
<FormKit type="hidden" name="url" :value="pageUrl" />
<FormKit
type="hidden"
id="was_this_page_helpful_url"
type="RplFormHidden"
name="url"
:value="pageUrl"
:pii="false"
/>
<FormKit
id="was_this_page_helpful_section"
type="RplFormHidden"
name="site_section_name"
:value="siteSectionName"
:pii="false"
/>
<FormKit
id="was_this_page_helpful"
Expand Down
10 changes: 10 additions & 0 deletions packages/ripple-test-utils/step_definitions/components/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ Then(
}
)

Then(
'a hidden field named {string} should exist with the value {string}',
(name: string, value: string) => {
cy.get(`input[name=${name}]`).as('hiddenInput')

cy.get('@hiddenInput').should('have.attr', 'type', 'hidden')
cy.get('@hiddenInput').should('have.value', value)
}
)

When(
'I click {string} from the select field with label {string}',
(value: string, label: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ const getFormSchemaFromMapping = async (
switch (field['#type']) {
case 'hidden':
mappedField = {
$formkit: 'hidden',
$formkit: 'RplFormHidden',
key: fieldKey,
name: fieldKey,
id: fieldID,
value: field['#default_value']
value: field['#default_value'],
pii: false
}
break
case 'textfield':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
export default {
inheritAttrs: false
}
</script>

<script setup lang="ts">
interface Props {
id: string
name: string
value?: string
pii?: boolean
}
withDefaults(defineProps<Props>(), {
value: undefined,
invalid: false,
required: false,
pii: true
})
</script>

<template>
<input :id="id" :name="name" :value="value" v-bind="$attrs" />
</template>
48 changes: 48 additions & 0 deletions packages/ripple-ui-forms/src/inputs/hidden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { FormKitTypeDefinition } from '@formkit/core'
import {
createRplFormInputOnly,
inputLibrary,
rplFeatures
} from './input-utils'

/**
* Input definition for Ripple hidden input.
* @public
*/
export const hidden: FormKitTypeDefinition = {
/**
* The actual schema of the input, or a function that returns the schema.
*/
schema: createRplFormInputOnly({
$cmp: 'RplFormHidden',
props: {
id: '$id',
value: '$_value',
name: '$node.name',
pii: '$node.props.pii',
type: 'hidden'
}
}),
library: inputLibrary,
/**
* The type of node can be a list, group, or input.
*/
type: 'input',
/**
* The family of inputs this one belongs too. For example "text" and "email"
* are both part of the "text" family. This is primary used for styling.
*/
family: 'text',
/**
* An array of extra props to accept for this input.
*/
props: ['pii'],
/**
* Forces node.props.type to be this explicit value.
*/
forceTypeProp: 'hidden',
/**
* Additional features that should be added to your input
*/
features: rplFeatures
}
1 change: 1 addition & 0 deletions packages/ripple-ui-forms/src/inputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export { label } from './label'
export { fieldset } from './fieldset'
export { divider } from './divider'
export { actions } from './actions'
export { hidden } from './hidden'
19 changes: 18 additions & 1 deletion packages/ripple-ui-forms/src/inputs/input-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import FormkitInputError from '../components/RplForm/FormkitInputError.vue'
import FormkitOuter from '../components/RplForm/FormkitOuter.vue'
// @ts-expect-error vue SFC
import RplFormNumber from '../components/RplFormNumber/RplFormNumber.vue'
// @ts-expect-error vue SFC
import RplFormHidden from '../components/RplFormHidden/RplFormHidden.vue'

export const inputLibrary = {
RplFormInput: markRaw(RplFormInput),
Expand All @@ -81,7 +83,8 @@ export const inputLibrary = {
RplFormActions: markRaw(RplFormActions),
FormkitInputError: markRaw(FormkitInputError),
FormkitOuter: markRaw(FormkitOuter),
RplFormNumber: markRaw(RplFormNumber)
RplFormNumber: markRaw(RplFormNumber),
RplFormHidden: markRaw(RplFormHidden)
}

export const rplFeatures = [
Expand Down Expand Up @@ -145,6 +148,20 @@ export const createRplFormGroup = (
) as unknown as FormKitExtendableSchemaRoot
}

/*
* Creates a Formkit schema for UI-less fields
* this is useful for when you don't need the wrapping elements
* and just want the input itself, for example, when using a hidden input
*/
export const createRplFormInputOnly = (
cmp: FormKitSchemaComponent
): FormKitExtendableSchemaRoot => {
return createSection(
'input',
() => cmp
)() as unknown as FormKitExtendableSchemaRoot
}

export const defaultRplFormInputProps = {
onInput: '$handlers.DOMInput',
onBlur: '$handlers.blur',
Expand Down
5 changes: 4 additions & 1 deletion packages/ripple-ui-forms/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
date,
datePicker,
optionButtons,
fieldset
fieldset,
hidden
} from './inputs/index'

const rplFormInputs = () => {
Expand Down Expand Up @@ -61,6 +62,8 @@ rplFormInputs.library = (node) => {
return node.define(divider)
case 'RplFormActions':
return node.define(actions)
case 'RplFormHidden':
return node.define(hidden)
}
}

Expand Down

0 comments on commit 445cacf

Please sign in to comment.