Skip to content

Commit

Permalink
fix: shrink SelectWidget label only if value is not empty (rjsf-team#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ValYouW authored and shijistar committed Jun 8, 2023
1 parent 1d6708d commit d542fbf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions packages/material-ui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export default function SelectWidget<
}: WidgetProps<T, S, F>) {
const { enumOptions, enumDisabled } = options;

multiple = typeof multiple === "undefined" ? false : !!multiple;

const emptyValue = multiple ? [] : "";
const isEmpty =
typeof value === "undefined" ||
(multiple && value.length < 1) ||
(!multiple && value === emptyValue);

const _onChange = ({
target: { value },
Expand All @@ -61,7 +67,7 @@ export default function SelectWidget<
id={id}
name={id}
label={label || schema.title}
value={typeof value === "undefined" ? emptyValue : value}
value={isEmpty ? emptyValue : value}
required={required}
disabled={disabled || readonly}
autoFocus={autofocus}
Expand All @@ -74,11 +80,11 @@ export default function SelectWidget<
select // Apply this and the following props after the potential overrides defined in textFieldProps
InputLabelProps={{
...textFieldProps.InputLabelProps,
shrink: true,
shrink: !isEmpty,
}}
SelectProps={{
...textFieldProps.SelectProps,
multiple: typeof multiple === "undefined" ? false : multiple,
multiple,
}}
aria-describedby={ariaDescribedByIds<T>(id)}
>
Expand Down
12 changes: 9 additions & 3 deletions packages/mui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ export default function SelectWidget<
}: WidgetProps<T, S, F>) {
const { enumOptions, enumDisabled } = options;

multiple = typeof multiple === "undefined" ? false : !!multiple;

const emptyValue = multiple ? [] : "";
const isEmpty =
typeof value === "undefined" ||
(multiple && value.length < 1) ||
(!multiple && value === emptyValue);

const _onChange = ({
target: { value },
Expand All @@ -61,7 +67,7 @@ export default function SelectWidget<
id={id}
name={id}
label={label || schema.title}
value={typeof value === "undefined" ? emptyValue : value}
value={isEmpty ? emptyValue : value}
required={required}
disabled={disabled || readonly}
autoFocus={autofocus}
Expand All @@ -74,11 +80,11 @@ export default function SelectWidget<
select // Apply this and the following props after the potential overrides defined in textFieldProps
InputLabelProps={{
...textFieldProps.InputLabelProps,
shrink: true,
shrink: !isEmpty,
}}
SelectProps={{
...textFieldProps.SelectProps,
multiple: typeof multiple === "undefined" ? false : multiple,
multiple,
}}
aria-describedby={ariaDescribedByIds<T>(id)}
>
Expand Down

0 comments on commit d542fbf

Please sign in to comment.