From 0db323e6039edca2ad2fc6fe3234638252d688c0 Mon Sep 17 00:00:00 2001 From: jasquat <2487833+jasquat@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:24:58 -0400 Subject: [PATCH] add fuzz to the id attribute of a checkbox so the carbon element knows what has been clicked w/ burnettk (#482) Co-authored-by: jasquat --- .../CheckboxWidget/CheckboxWidget.tsx | 12 +++- .../CheckboxesWidget/CheckboxesWidget.tsx | 59 +++++++++---------- spiffworkflow-frontend/src/rjsf/helpers.tsx | 11 ++++ 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/spiffworkflow-frontend/src/rjsf/carbon_theme/CheckboxWidget/CheckboxWidget.tsx b/spiffworkflow-frontend/src/rjsf/carbon_theme/CheckboxWidget/CheckboxWidget.tsx index 43eb058fb..25fca6043 100644 --- a/spiffworkflow-frontend/src/rjsf/carbon_theme/CheckboxWidget/CheckboxWidget.tsx +++ b/spiffworkflow-frontend/src/rjsf/carbon_theme/CheckboxWidget/CheckboxWidget.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { Checkbox } from '@carbon/react'; import { WidgetProps } from '@rjsf/utils'; -import { getCommonAttributes } from '../../helpers'; +import { getCommonAttributes, makeid } from '../../helpers'; function CheckboxWidget(props: WidgetProps) { const { @@ -42,10 +42,16 @@ function CheckboxWidget(props: WidgetProps) { rawErrors ); + // if the parent rjsf schema is not of type "object", then rjsf sends "root" through as the id. + // this creates issues with the carbon checkbox where it will not accept any clicks to the checkbox + // so add fuzz to the id to ensure it is unique. + // https://github.com/rjsf-team/react-jsonschema-form/issues/1824 + const uniqueId = makeid(10); + return ( { @@ -54,36 +53,34 @@ function CheckboxesWidget({ }: React.FocusEvent) => onFocus(id, value); return ( - <> - - {Array.isArray(enumOptions) && - enumOptions.map((option, index: number) => { - const checked = value.indexOf(option.value) !== -1; - const itemDisabled = - Array.isArray(enumDisabled) && - enumDisabled.indexOf(option.value) !== -1; - const checkbox = ( - - ); - return ( - - ); - })} - - + + {Array.isArray(enumOptions) && + enumOptions.map((option, index: number) => { + const checked = value.indexOf(option.value) !== -1; + const itemDisabled = + Array.isArray(enumDisabled) && + enumDisabled.indexOf(option.value) !== -1; + const checkbox = ( + + ); + return ( + + ); + })} + ); } diff --git a/spiffworkflow-frontend/src/rjsf/helpers.tsx b/spiffworkflow-frontend/src/rjsf/helpers.tsx index 779f3a9b0..f4dbf4703 100644 --- a/spiffworkflow-frontend/src/rjsf/helpers.tsx +++ b/spiffworkflow-frontend/src/rjsf/helpers.tsx @@ -45,3 +45,14 @@ export const getCommonAttributes = ( tooltipText, }; }; + +// https://stackoverflow.com/a/1349426/6090676 +export const makeid = (length: number) => { + let result = ''; + const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; + const charactersLength = characters.length; + for (let i = 0; i < length; i += 1) { + result += characters.charAt(Math.floor(Math.random() * charactersLength)); + } + return result; +};