Skip to content

Commit

Permalink
Clarify by creating showPlaceholderOption variable in all changed t…
Browse files Browse the repository at this point in the history
…hemes
  • Loading branch information
nickgros committed Jul 8, 2024
1 parent 89621ac commit 6c4b39b
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
4 changes: 3 additions & 1 deletion packages/antd/src/widgets/SelectWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export default function SelectWidget<
name: id,
};

const showPlaceholderOption = !multiple && schema.default === undefined;

const selectOptions: DefaultOptionType[] | undefined = useMemo(() => {
if (Array.isArray(enumOptions)) {
const options = [...enumOptions];
if (!multiple && schema.default === undefined) {
if (showPlaceholderOption) {
options.unshift({ value: '', label: placeholder || '' });
}
return options.map(({ value: optionValue, label: optionLabel }, index) => ({
Expand Down
3 changes: 2 additions & 1 deletion packages/bootstrap-4/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default function SelectWidget<
}
}
const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple);
const showPlaceholderOption = !multiple && schema.default === undefined;

return (
<Form.Control
Expand Down Expand Up @@ -78,7 +79,7 @@ export default function SelectWidget<
}}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{!multiple && schema.default === undefined && <option value=''>{placeholder}</option>}
{showPlaceholderOption && <option value=''>{placeholder}</option>}
{(enumOptions as any).map(({ value, label }: any, i: number) => {
const disabled: any = Array.isArray(enumDisabled) && (enumDisabled as any).indexOf(value) != -1;
return (
Expand Down
3 changes: 2 additions & 1 deletion packages/chakra-ui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
const _onFocus = ({ target }: FocusEvent<HTMLInputElement>) =>
onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, emptyValue));

const showPlaceholderOption = !multiple && schema.default === undefined;
const _valueLabelMap: any = {};
const displayEnumOptions: OptionsOrGroups<any, any> = useMemo(() => {
if (Array.isArray(enumOptions)) {
const options = [...enumOptions];
if (!multiple && schema.default === undefined) {
if (showPlaceholderOption) {
options.unshift({ value: '', label: placeholder || '' });
}
return options.map((option: EnumOptionsType<S>, index: number) => {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/components/widgets/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,27 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
const newValue = getValue(event, multiple);
return onFocus(id, enumOptionsValueForIndex<S>(newValue, enumOptions, optEmptyVal));
},
[onFocus, id, schema, multiple, options]
[onFocus, id, schema, multiple, enumOptions, optEmptyVal]
);

const handleBlur = useCallback(
(event: FocusEvent<HTMLSelectElement>) => {
const newValue = getValue(event, multiple);
return onBlur(id, enumOptionsValueForIndex<S>(newValue, enumOptions, optEmptyVal));
},
[onBlur, id, schema, multiple, options]
[onBlur, id, schema, multiple, enumOptions, optEmptyVal]
);

const handleChange = useCallback(
(event: ChangeEvent<HTMLSelectElement>) => {
const newValue = getValue(event, multiple);
return onChange(enumOptionsValueForIndex<S>(newValue, enumOptions, optEmptyVal));
},
[onChange, schema, multiple, options]
[onChange, schema, multiple, enumOptions, optEmptyVal]
);

const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple);
const showPlaceholderOption = !multiple && schema.default === undefined;

return (
<select
Expand All @@ -83,7 +84,7 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
onChange={handleChange}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{!multiple && schema.default === undefined && <option value=''>{placeholder}</option>}
{showPlaceholderOption && <option value=''>{placeholder}</option>}
{Array.isArray(enumOptions) &&
enumOptions.map(({ value, label }, i) => {
const disabled = enumDisabled && enumDisabled.indexOf(value) !== -1;
Expand Down
3 changes: 2 additions & 1 deletion packages/fluentui-rc/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
const newValue = getValue(data, multiple);
return onChange(enumOptionsValueForIndex<S>(newValue, enumOptions, optEmptyVal));
};
const showPlaceholderOption = !multiple && schema.default === undefined;

return (
<Field
Expand All @@ -83,7 +84,7 @@ function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFSchema, F extend
selectedOptions={selectedIndexesAsArray}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{!multiple && schema.default === undefined && <Option value=''>{placeholder || ''}</Option>}
{showPlaceholderOption && <Option value=''>{placeholder || ''}</Option>}
{Array.isArray(enumOptions) &&
enumOptions.map(({ value, label }, i) => {
const disabled = enumDisabled && enumDisabled.indexOf(value) !== -1;
Expand Down
3 changes: 2 additions & 1 deletion packages/material-ui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function SelectWidget<
const _onFocus = ({ target }: FocusEvent<HTMLInputElement>) =>
onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, optEmptyVal));
const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple);
const showPlaceholderOption = !multiple && schema.default === undefined;

return (
<TextField
Expand Down Expand Up @@ -86,7 +87,7 @@ export default function SelectWidget<
}}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{!multiple && schema.default === undefined && <MenuItem value=''>{placeholder}</MenuItem>}
{showPlaceholderOption && <MenuItem value=''>{placeholder}</MenuItem>}
{Array.isArray(enumOptions) &&
enumOptions.map(({ value, label }, i: number) => {
const disabled: boolean = Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1;
Expand Down
3 changes: 2 additions & 1 deletion packages/mui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default function SelectWidget<
onFocus(id, enumOptionsValueForIndex<S>(target && target.value, enumOptions, optEmptyVal));
const selectedIndexes = enumOptionsIndexForValue<S>(value, enumOptions, multiple);
const { InputLabelProps, SelectProps, autocomplete, ...textFieldRemainingProps } = textFieldProps;
const showPlaceholderOption = !multiple && schema.default === undefined;

return (
<TextField
Expand Down Expand Up @@ -89,7 +90,7 @@ export default function SelectWidget<
}}
aria-describedby={ariaDescribedByIds<T>(id)}
>
{!multiple && schema.default === undefined && <MenuItem value=''>{placeholder}</MenuItem>}
{showPlaceholderOption && <MenuItem value=''>{placeholder}</MenuItem>}
{Array.isArray(enumOptions) &&
enumOptions.map(({ value, label }, i: number) => {
const disabled: boolean = Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1;
Expand Down
13 changes: 7 additions & 6 deletions packages/semantic-ui/src/SelectWidget/SelectWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ import { getSemanticProps } from '../util';

/**
* Returns and creates an array format required for semantic drop down
* @param {array} enumOptions- array of items for the dropdown
* @param {array} enumOptions - array of items for the dropdown
* @param {array} enumDisabled - array of enum option values to disable
* @param {boolean} showPlaceholderOption - whether to show a placeholder option
* @param {string} placeholder - placeholder option label
* @returns {*}
*/
function createDefaultValueOptionsForDropDown<S extends StrictRJSFSchema = RJSFSchema>(
schema: S,
enumOptions?: EnumOptionsType<S>[],
enumDisabled?: UIOptionsType['enumDisabled'],
multiple?: boolean,
showPlaceholderOption?: boolean,
placeholder?: string
) {
const disabledOptions = enumDisabled || [];
const options: DropdownItemProps[] = [];
if (!multiple && schema.default === undefined) {
if (showPlaceholderOption) {
options.push({ value: '', text: placeholder || '' });
}
options.push(
Expand Down Expand Up @@ -86,11 +87,11 @@ export default function SelectWidget<T = any, S extends StrictRJSFSchema = RJSFS
});
const { enumDisabled, enumOptions, emptyValue: optEmptyVal } = options;
const emptyValue = multiple ? [] : '';
const showPlaceholderOption = !multiple && schema.default === undefined;
const dropdownOptions = createDefaultValueOptionsForDropDown<S>(
schema,
enumOptions,
enumDisabled,
multiple,
showPlaceholderOption,
placeholder
);
const _onChange = (_: SyntheticEvent<HTMLElement>, { value }: DropdownProps) =>
Expand Down

0 comments on commit 6c4b39b

Please sign in to comment.