From 16b2136843f9bba868413ad3dde974e82351b57d Mon Sep 17 00:00:00 2001 From: etienne-doyon Date: Wed, 13 Dec 2023 13:57:30 +0100 Subject: [PATCH 1/2] feat(meta): add option to display mandatory field hint --- .../dictionary/checkBox.component.tsx | 6 +++++ .../dictionary/date.component.tsx | 6 +++++ .../dictionary/password.component.tsx | 6 +++++ .../dictionary/select.component.tsx | 6 +++++ .../dictionary/text.component.tsx | 6 +++++ .../dictionary/checkBox.component.tsx | 6 +++++ .../dictionary/date.component.tsx | 6 +++++ .../dictionary/password.component.tsx | 8 +++++++ .../dictionary/select.component.tsx | 6 +++++ .../dictionary/submit.component.tsx | 15 +++++++++++- .../dictionary/text.component.tsx | 16 ++++++++++++- apps/demo/src/app/login.config.ts | 3 +++ apps/demo/src/app/register.config.ts | 3 +++ apps/docsite/docs/form-builder.md | 3 +++ libs/form-builder/README.md | 3 +++ .../src/lib/__tests__/fixtures.tsx | 1 + .../lib/components/formField.component.tsx | 24 +++++++++++++++++-- libs/form-builder/src/lib/formBuilder.tsx | 3 ++- libs/form-builder/src/lib/types.ts | 11 ++++++--- .../__tests__/getSchemaInfo.util.spec.ts | 1 + .../src/lib/utils/getSchemaInfo.util.ts | 5 +++- 21 files changed, 135 insertions(+), 9 deletions(-) diff --git a/apps/demo/src/app/examples/with-material-ui/dictionary/checkBox.component.tsx b/apps/demo/src/app/examples/with-material-ui/dictionary/checkBox.component.tsx index 176ea16..73edcef 100644 --- a/apps/demo/src/app/examples/with-material-ui/dictionary/checkBox.component.tsx +++ b/apps/demo/src/app/examples/with-material-ui/dictionary/checkBox.component.tsx @@ -16,6 +16,7 @@ export const Checkbox = ({ onChange, propRef, value, + shouldDisplayRequiredHint, }: { 'data-testid': string; errorMessage: string; @@ -29,9 +30,14 @@ export const Checkbox = ({ propRef: Ref; type?: string; value?: boolean; + shouldDisplayRequiredHint?: boolean; }) => { const inputProps = useMemo(() => ({ ref: propRef, 'aria-label': 'controlled' }), [propRef]); + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( diff --git a/apps/demo/src/app/examples/with-material-ui/dictionary/date.component.tsx b/apps/demo/src/app/examples/with-material-ui/dictionary/date.component.tsx index 777ad0d..91ead9a 100644 --- a/apps/demo/src/app/examples/with-material-ui/dictionary/date.component.tsx +++ b/apps/demo/src/app/examples/with-material-ui/dictionary/date.component.tsx @@ -27,6 +27,7 @@ export const DateInput = ({ onChange, onBlur, propRef, + shouldDisplayRequiredHint, }: { 'data-testid': string; errors: FieldErrors; @@ -41,6 +42,7 @@ export const DateInput = ({ value?: string | number; validation: Validations; setFieldValue: (id: Path, value: any) => void; + shouldDisplayRequiredHint?: boolean; }) => { const inputProps = useMemo(() => ({ ref: propRef }), [propRef]); const rules = getValidationRulesHints({ @@ -50,6 +52,10 @@ export const DateInput = ({ const hasError = !!checkRules(value, rules).length; + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return (
{ const inputProps = useMemo(() => ({ ref: propRef }), [propRef]); @@ -48,6 +50,10 @@ export const Password = ({ const hasError = !!checkRules(value, rules).length; + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( { const inputProps = useMemo(() => ({ ref: propRef }), [propRef]); + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( {label} diff --git a/apps/demo/src/app/examples/with-material-ui/dictionary/text.component.tsx b/apps/demo/src/app/examples/with-material-ui/dictionary/text.component.tsx index 42fb89d..ea37678 100644 --- a/apps/demo/src/app/examples/with-material-ui/dictionary/text.component.tsx +++ b/apps/demo/src/app/examples/with-material-ui/dictionary/text.component.tsx @@ -17,6 +17,7 @@ export const Text = ({ type, value, multiline, + shouldDisplayRequiredHint, }: { 'data-testid': string; errorMessage: string; @@ -31,10 +32,15 @@ export const Text = ({ type?: string; value?: string | number; multiline?: boolean; + shouldDisplayRequiredHint?: boolean; }) => { const inputProps = useMemo(() => ({ ref: propRef }), [propRef]); const error = errors && errors.type && errorMessage; + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( ) => void; value: boolean; label: string; + shouldDisplayRequiredHint?: boolean; }) => { + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return (
diff --git a/apps/demo/src/app/examples/with-styled-components/dictionary/date.component.tsx b/apps/demo/src/app/examples/with-styled-components/dictionary/date.component.tsx index b56b9a9..cd6969a 100644 --- a/apps/demo/src/app/examples/with-styled-components/dictionary/date.component.tsx +++ b/apps/demo/src/app/examples/with-styled-components/dictionary/date.component.tsx @@ -12,6 +12,7 @@ import { RuleList } from '../atoms/rule-list.component'; export const DateInput = ({ errors, validation, + shouldDisplayRequiredHint, label, ...props }: { @@ -23,6 +24,7 @@ export const DateInput = ({ id: string; setFieldValue: (id: Path, value: any) => void; onChange: (event: any) => void; + shouldDisplayRequiredHint?: boolean; }) => { const rules = getValidationRulesHints({ errors, @@ -32,6 +34,10 @@ export const DateInput = ({ const fieldError = errors && errors.type; const isValid = !!(props.value && !hasError && !fieldError); + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( { const rules = getValidationRulesHints({ errors, @@ -27,11 +30,16 @@ export const Password = ({ const fieldError = errors && errors.type; const isValid = !!(props.value && !hasError && !fieldError); + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return ( ) => void; value: string | number; label: string; choices: string[] | number[]; multiple?: boolean; + shouldDisplayRequiredHint?: boolean; }) => { + if (shouldDisplayRequiredHint) { + label += ' *'; + } + return (