From 3cbc01171939cb5ba3099de67f0800f11e6943c7 Mon Sep 17 00:00:00 2001 From: Juan Polanco Date: Mon, 26 Sep 2022 10:51:36 -0500 Subject: [PATCH] Add input-specific `propType.source` type Most of the current sources read HTML values / attributes, but for `input` elements it is sometimes needed to get the actual "current" value from the JS API, since it could have been changed before the component was initialized. This allows users to fetch user input from a form after it being rendered on the server (and after certain plugins autofill its content), so it's a better representation of the value on the page. It also allows reading a complete form into a single property. --- CHANGELOG.md | 1 + docs/api/props.md | 50 ++- docs/guide/props.md | 4 +- src/lib/props/propDefinitions.types.ts | 11 +- .../createFormPropertySource.test.ts | 274 ++++++++++++++++ .../createFormPropertySource.testutils.ts | 77 +++++ .../createFormPropertySource.ts | 111 +++++++ src/lib/test-utils/propTypes.ts | 11 + src/lib/utils/global.ts | 2 + .../core/props/FormProps.stories.ts | 309 ++++++++++++++++++ 10 files changed, 845 insertions(+), 5 deletions(-) create mode 100644 src/lib/props/property-sources/createFormPropertySource.test.ts create mode 100644 src/lib/props/property-sources/createFormPropertySource.testutils.ts create mode 100644 src/lib/props/property-sources/createFormPropertySource.ts create mode 100644 src/lib/test-utils/propTypes.ts create mode 100644 test-storybook/src/components/core/props/FormProps.stories.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d3dd0aa..da1c00c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Add `event` binding to refComponents - Add the `'custom'` `propType.source` for user-defined extraction functions +- Add `form` source, allowing you to easily extract initial values from forms or inputs ### Changed diff --git a/docs/api/props.md b/docs/api/props.md index b2d760dd..14ca8f23 100644 --- a/docs/api/props.md +++ b/docs/api/props.md @@ -33,7 +33,7 @@ export type PropTypeDefinition = { shapeType?: Function; sourceOptions?: { target?: string; - type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html' | 'custom'; + type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html' | 'form' | 'custom'; name?: string; options?: { cssPredicate?: Array; @@ -265,7 +265,7 @@ HTML besides the default behaviour. ```ts declare function source(options: { target?: string; - type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html', | 'custom'; + type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html' | 'form' | 'custom'; name?: string; options?: { cssPredicate?: Array; @@ -276,7 +276,8 @@ declare function source(options: { * `target?: string` – The refName (those you configure as part of the component options) from which you want to extract this property. Defaults to the data-component element. -* `type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html' | 'custom'` - The type source you want to extract. +* `type?: 'data' | 'json' | 'attr' | 'css' | 'text' | 'html' | 'form' | 'custom'` - The type source + you want to extract. Defaults to the `data + json + css` source (`css` only for boolean props). * `data` – Reads the `data-attribute` from your target element. * `json` – Reads the object key from a `