From e0670b11f8f94d0cfbad163cbf0ca29e06e7f3fe Mon Sep 17 00:00:00 2001 From: ilbrando Date: Thu, 27 Jun 2024 13:25:15 +0200 Subject: [PATCH] Joy input readonly (#15) * Joy FormText and FormNumber supports readOnly * Rush change --- .../joy-input-readonly_2024-06-27-08-53.json | 10 ++++++++++ .../storybook/src/stories/joy/form-fields.stories.tsx | 8 +++++--- .../src/form-components/form-number/form-number.tsx | 7 +++++-- .../src/form-components/form-text/form-text.tsx | 8 +++++--- 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 common/changes/@ilbrando/simple-form-joy/joy-input-readonly_2024-06-27-08-53.json diff --git a/common/changes/@ilbrando/simple-form-joy/joy-input-readonly_2024-06-27-08-53.json b/common/changes/@ilbrando/simple-form-joy/joy-input-readonly_2024-06-27-08-53.json new file mode 100644 index 0000000..df62b8a --- /dev/null +++ b/common/changes/@ilbrando/simple-form-joy/joy-input-readonly_2024-06-27-08-53.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@ilbrando/simple-form-joy", + "comment": "FormText and FormNumber supports readOnly property also when the form field is disabled.", + "type": "patch" + } + ], + "packageName": "@ilbrando/simple-form-joy" +} \ No newline at end of file diff --git a/examples/storybook/src/stories/joy/form-fields.stories.tsx b/examples/storybook/src/stories/joy/form-fields.stories.tsx index 4100d9f..878bccc 100644 --- a/examples/storybook/src/stories/joy/form-fields.stories.tsx +++ b/examples/storybook/src/stories/joy/form-fields.stories.tsx @@ -38,12 +38,13 @@ const radioGroupOptions: RadioGroupOption[] = optionValues; type FormStoryProps = { isDisabled: boolean; isRequired: boolean; + isReadOnly: boolean; size: "sm" | "md" | "lg"; reserveSpaceForValidationMessage: boolean; }; const FormStory = (props: FormStoryProps) => { - const { isDisabled, isRequired, size, reserveSpaceForValidationMessage } = props; + const { isDisabled, isRequired, isReadOnly, size, reserveSpaceForValidationMessage } = props; const { required, minCount, equal } = useValidationRules(); @@ -115,8 +116,8 @@ const FormStory = (props: FormStoryProps) => { - - + + @@ -146,6 +147,7 @@ const meta = { args: { isDisabled: false, isRequired: false, + isReadOnly: false, size: "md", reserveSpaceForValidationMessage: false } diff --git a/packages/simple-form-joy/src/form-components/form-number/form-number.tsx b/packages/simple-form-joy/src/form-components/form-number/form-number.tsx index 3a97fbd..e03b10c 100644 --- a/packages/simple-form-joy/src/form-components/form-number/form-number.tsx +++ b/packages/simple-form-joy/src/form-components/form-number/form-number.tsx @@ -14,12 +14,14 @@ type FormNumberProps> const isValidValue = (value: string) => /^[-]?(\d+)$/.test(value); export const FormNumber = function >(props: FormNumberProps) { - const { formManager, fieldName, disabled, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled, readOnly, label, size, reserveSpaceForValidationMessage, ...rest } = props; const { texts } = useLocalization(); const editor = getEditor(formManager, fieldName, disabled); + const isDisabled = !(readOnly ?? false) && (disabled ?? false); // If readonly is true we don't disable the input because then the user can't copy the value + const [textBoxValue, setTextBoxValue] = useState(hasValue(editor.value) ? editor.value.toString() : ""); useEffect(() => { @@ -27,7 +29,7 @@ export const FormNumber = function + { @@ -45,6 +47,7 @@ export const FormNumber = function diff --git a/packages/simple-form-joy/src/form-components/form-text/form-text.tsx b/packages/simple-form-joy/src/form-components/form-text/form-text.tsx index 694e8e8..29f832f 100644 --- a/packages/simple-form-joy/src/form-components/form-text/form-text.tsx +++ b/packages/simple-form-joy/src/form-components/form-text/form-text.tsx @@ -14,10 +14,12 @@ export type FormTextProps>(props: FormTextProps) { - const { formManager, fieldName, textTransform, disabled, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, textTransform, disabled, readOnly, label, size, reserveSpaceForValidationMessage, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); + const isDisabled = !(readOnly ?? false) && (disabled ?? false); // If readonly is true we don't disable the input because then the user can't copy the value + const parseValue = (value: string) => { if (!hasValueAndNotEmptyString(value)) return null; if (!hasValue(textTransform)) return value; @@ -32,8 +34,8 @@ export const FormText = function - editor.setFieldValue(parseValue(e.target.value))} {...rest} /> + + editor.setFieldValue(parseValue(e.target.value))} readOnly={readOnly} {...rest} /> ); };