From 6a463f6186dd690c40b50712045c349a6b26243d Mon Sep 17 00:00:00 2001 From: ilbrando Date: Mon, 22 Jul 2024 11:12:13 +0200 Subject: [PATCH] Sx prop on form control (#19) * Easy access to sx prop on FormControl * Rush change --- .../sx-prop-on-form-control_2024-07-22-09-10.json | 10 ++++++++++ .../form-control-wrapper/form-control-wrapper.tsx | 4 ++-- .../form-autocomplete/form-autocomplete-multiple.tsx | 12 ++++++++++-- .../form-autocomplete/form-autocomplete.tsx | 12 ++++++++++-- .../form-components/form-checkbox/form-checkbox.tsx | 11 +++++++++-- .../src/form-components/form-number/form-number.tsx | 12 ++++++++++-- .../form-radio-group/form-radio-group.tsx | 12 ++++++++++-- .../form-range-slider/form-range-slider.tsx | 12 ++++++++++-- .../src/form-components/form-switch/form-switch.tsx | 11 +++++++++-- .../src/form-components/form-text/form-text.tsx | 12 ++++++++++-- .../simple-form-joy/src/form-components/types.ts | 2 ++ 11 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 common/changes/@ilbrando/simple-form-joy/sx-prop-on-form-control_2024-07-22-09-10.json diff --git a/common/changes/@ilbrando/simple-form-joy/sx-prop-on-form-control_2024-07-22-09-10.json b/common/changes/@ilbrando/simple-form-joy/sx-prop-on-form-control_2024-07-22-09-10.json new file mode 100644 index 0000000..058bf76 --- /dev/null +++ b/common/changes/@ilbrando/simple-form-joy/sx-prop-on-form-control_2024-07-22-09-10.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@ilbrando/simple-form-joy", + "comment": "New sxFormControl prop to access sx prop on FormControl wrapper", + "type": "minor" + } + ], + "packageName": "@ilbrando/simple-form-joy" +} \ No newline at end of file diff --git a/packages/simple-form-joy/src/components/form-control-wrapper/form-control-wrapper.tsx b/packages/simple-form-joy/src/components/form-control-wrapper/form-control-wrapper.tsx index 14455c8..16f5920 100644 --- a/packages/simple-form-joy/src/components/form-control-wrapper/form-control-wrapper.tsx +++ b/packages/simple-form-joy/src/components/form-control-wrapper/form-control-wrapper.tsx @@ -13,14 +13,14 @@ export type FormControlWrapperProps = FormFieldBaseSharedProps & { }; export const FormControlWrapper = (props: FormControlWrapperProps) => { - const { isRequired, isDisabled, size, label, errorMessage, reserveSpaceForValidationMessage, children } = props; + const { isRequired, isDisabled, size, label, errorMessage, reserveSpaceForValidationMessage, sxFormControl, children } = props; const { effectiveReserveSpaceForValidationMessage } = useJoyFormUtils(reserveSpaceForValidationMessage); const showErrorMessage = hasValue(errorMessage) || effectiveReserveSpaceForValidationMessage; return ( - + {hasValue(label) && {label}} {children} {showErrorMessage && {errorMessage ?? (effectiveReserveSpaceForValidationMessage ? <>{"\u00A0"} : null)}} diff --git a/packages/simple-form-joy/src/form-components/form-autocomplete/form-autocomplete-multiple.tsx b/packages/simple-form-joy/src/form-components/form-autocomplete/form-autocomplete-multiple.tsx index b21f205..a8c0951 100644 --- a/packages/simple-form-joy/src/form-components/form-autocomplete/form-autocomplete-multiple.tsx +++ b/packages/simple-form-joy/src/form-components/form-autocomplete/form-autocomplete-multiple.tsx @@ -20,14 +20,22 @@ export type FormAutocompleteMultipleProps>(props: FormAutocompleteMultipleProps) { - const { formManager, fieldName, disabled, options, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled, options, label, size, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); const optionValues = useMemo(() => options.map(x => x.value), [options]); return ( - + >(props: FormAutocompleteProps) { - const { formManager, fieldName, disabled, options, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled, options, label, size, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); const optionValues = useMemo(() => options.map(x => x.value), [options]); return ( - + > = OmitSafe & FormFieldBaseProps; export const FormCheckbox = function >(props: FormCheckboxProps) { - const { formManager, fieldName, disabled, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled, label, size, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); return ( - + editor.setFieldValue(e.target.checked)} label={label} {...rest} /> ); 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 230903d..6705a6e 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,7 +14,7 @@ type FormNumberProps> const isValidValue = (value: string) => /^[-]?(\d+)$/.test(value); export const FormNumber = function >(props: FormNumberProps) { - const { formManager, fieldName, disabled, readOnly, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled, readOnly, label, size, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const { texts } = useLocalization(); @@ -29,7 +29,15 @@ export const FormNumber = function + { diff --git a/packages/simple-form-joy/src/form-components/form-radio-group/form-radio-group.tsx b/packages/simple-form-joy/src/form-components/form-radio-group/form-radio-group.tsx index 825aacf..67dac81 100644 --- a/packages/simple-form-joy/src/form-components/form-radio-group/form-radio-group.tsx +++ b/packages/simple-form-joy/src/form-components/form-radio-group/form-radio-group.tsx @@ -20,14 +20,22 @@ export type FormRadioGroupProps>(props: FormRadioGroupProps) { - const { formManager, fieldName, disabled = false, label, size, options, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, disabled = false, label, size, options, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); const isValueString = options.length > 0 && typeof options[0].value === "string"; return ( - + editor.setFieldValue((isValueString ? e.target.value : parseInt(e.target.value)) as TFormValue)} {...rest}> {options.map(item => ( diff --git a/packages/simple-form-joy/src/form-components/form-range-slider/form-range-slider.tsx b/packages/simple-form-joy/src/form-components/form-range-slider/form-range-slider.tsx index 8d638a1..802d8ee 100644 --- a/packages/simple-form-joy/src/form-components/form-range-slider/form-range-slider.tsx +++ b/packages/simple-form-joy/src/form-components/form-range-slider/form-range-slider.tsx @@ -14,7 +14,7 @@ export type FormRangeSliderValue = { type FormRangeSliderProps> = OmitSafe & FormFieldBaseProps; export const FormRangeSlider = function >(props: FormRangeSliderProps) { - const { formManager, fieldName, label, size, disabled, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, label, size, disabled, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); @@ -29,7 +29,15 @@ export const FormRangeSlider = function + diff --git a/packages/simple-form-joy/src/form-components/form-switch/form-switch.tsx b/packages/simple-form-joy/src/form-components/form-switch/form-switch.tsx index bbf2075..1089d98 100644 --- a/packages/simple-form-joy/src/form-components/form-switch/form-switch.tsx +++ b/packages/simple-form-joy/src/form-components/form-switch/form-switch.tsx @@ -15,7 +15,7 @@ export type FormSwitchProps>(props: FormSwitchProps) { - const { formManager, fieldName, disabled, label, labelPlacement = "end", size, reserveSpaceForValidationMessage, sx, ...rest } = props; + const { formManager, fieldName, disabled, label, labelPlacement = "end", size, reserveSpaceForValidationMessage, sxFormControl, sx, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); @@ -24,7 +24,14 @@ export const FormSwitch = function + {label} 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 6fe513e..0bfdc37 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,7 +14,7 @@ export type FormTextProps>(props: FormTextProps) { - const { formManager, fieldName, textTransform, disabled, readOnly, label, size, reserveSpaceForValidationMessage, ...rest } = props; + const { formManager, fieldName, textTransform, disabled, readOnly, label, size, reserveSpaceForValidationMessage, sxFormControl, ...rest } = props; const editor = getEditor(formManager, fieldName, disabled); @@ -34,7 +34,15 @@ export const FormText = function + editor.setFieldValue(parseValue(e.target.value))} readOnly={readOnly} {...rest} /> ); diff --git a/packages/simple-form-joy/src/form-components/types.ts b/packages/simple-form-joy/src/form-components/types.ts index eabb27e..7751e1e 100644 --- a/packages/simple-form-joy/src/form-components/types.ts +++ b/packages/simple-form-joy/src/form-components/types.ts @@ -11,6 +11,8 @@ export type FormFieldBaseSharedProps = Pick & { * prop if you want to override the value on each instance. */ reserveSpaceForValidationMessage?: boolean; + /** The sx prop for the wrapping `FormControl` */ + sxFormControl?: FormControlProps["sx"]; }; export type FormFieldBaseProps> = FormFieldBaseSharedProps & {