Skip to content

Commit a97db59

Browse files
Fix 4942 so that placeholder in chakra-ui NativeSelectWidget is render properly (#4946)
1 parent 0782d68 commit a97db59

File tree

6 files changed

+1215
-21
lines changed

6 files changed

+1215
-21
lines changed

CHANGELOG.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,44 @@ should change the heading of the (upcoming) version to include a major version b
2020

2121
## @rjsf/antd
2222

23-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
23+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
2424

2525
## @rjsf/chakra-ui
2626

27-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
27+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
28+
- Updated `NativeSelectWidget` so that is properly renders a placeholder, fixing [#4942](https://github.com/rjsf-team/react-jsonschema-form/issues/4942)
2829

2930
## @rjsf/core
3031

31-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
32+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
3233

3334
## @rjsf/daisyui
3435

35-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
36+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
3637

3738
## @rjsf/fluentui-rc
3839

39-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
40+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
4041

4142
## @rjsf/mantine
4243

43-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
44+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
4445

4546
## @rjsf/mui
4647

47-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
48+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
4849

4950
## @rjsf/react-bootstrap
5051

51-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
52+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
5253

5354
## @rjsf/semantic-ui
5455

55-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
56+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
5657

5758
## @rjsf/shadcn
5859

59-
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing ([#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927))
60+
- Fixed duplicate React keys in datalist when schema examples and default have different types, fixing [#4927](https://github.com/rjsf-team/react-jsonschema-form/issues/4927)
6061
# 6.2.5
6162

6263
## @rjsf/mui

packages/chakra-ui/src/SelectNativeWidget/NativeSelectWidget.tsx renamed to packages/chakra-ui/src/NativeSelectWidget/NativeSelectWidget.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
WidgetProps,
1212
} from '@rjsf/utils';
1313
import { OptionsOrGroups } from 'chakra-react-select';
14-
import { createListCollection, NativeSelect as ChakraSelect } from '@chakra-ui/react';
14+
import { createListCollection, NativeSelect } from '@chakra-ui/react';
1515

1616
import { Field } from '../components/ui/field';
1717
import { getChakra } from '../utils';
@@ -79,12 +79,9 @@ export default function NativeSelectWidget<
7979
disabled: Array.isArray(enumDisabled) && enumDisabled.indexOf(value) !== -1,
8080
};
8181
});
82-
if (showPlaceholderOption) {
83-
(displayEnumOptions as any[]).unshift({ value: '', label: placeholder || '' });
84-
}
8582
}
8683
return { valueLabelMap: valueLabelMap, displayEnumOptions: displayEnumOptions };
87-
}, [enumDisabled, enumOptions, placeholder, showPlaceholderOption]);
84+
}, [enumDisabled, enumOptions]);
8885

8986
const selectedIndex = enumOptionsIndexForValue<S>(value, enumOptions, false);
9087

@@ -116,8 +113,8 @@ export default function NativeSelectWidget<
116113
label={labelValue(label, hideLabel || !label)}
117114
{...chakraProps}
118115
>
119-
<ChakraSelect.Root>
120-
<ChakraSelect.Field
116+
<NativeSelect.Root>
117+
<NativeSelect.Field
121118
id={id}
122119
onBlur={_onBlur}
123120
onChange={_onChange}
@@ -126,14 +123,19 @@ export default function NativeSelectWidget<
126123
value={formValue}
127124
aria-describedby={ariaDescribedByIds(id)}
128125
>
126+
{showPlaceholderOption ? (
127+
<option value='' disabled hidden>
128+
{placeholder || ''}
129+
</option>
130+
) : undefined}
129131
{selectOptions.items.map((item) => (
130132
<option key={item.value} value={item.value}>
131133
{item.label}
132134
</option>
133135
))}
134-
</ChakraSelect.Field>
135-
<ChakraSelect.Indicator />
136-
</ChakraSelect.Root>
136+
</NativeSelect.Field>
137+
<NativeSelect.Indicator />
138+
</NativeSelect.Root>
137139
</Field>
138140
);
139141
}
File renamed without changes.

packages/chakra-ui/src/Widgets/Widgets.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import CheckboxesWidget from '../CheckboxesWidget/CheckboxesWidget';
55
import RadioWidget from '../RadioWidget/RadioWidget';
66
import RangeWidget from '../RangeWidget/RangeWidget';
77
import SelectWidget from '../SelectWidget/SelectWidget';
8-
import NativeSelectWidget from '../SelectNativeWidget/NativeSelectWidget';
8+
import NativeSelectWidget from '../NativeSelectWidget/NativeSelectWidget';
99
import TextareaWidget from '../TextareaWidget/TextareaWidget';
1010
import UpDownWidget from '../UpDownWidget/UpDownWidget';
1111
import { FormContextType, RegistryWidgetsType, RJSFSchema, StrictRJSFSchema } from '@rjsf/utils';
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { RJSFSchema, UiSchema } from '@rjsf/utils';
2+
import { render } from '@testing-library/react';
3+
import validator from '@rjsf/validator-ajv8';
4+
5+
import Form from './WrappedForm';
6+
7+
describe('NativeSelectWidget', () => {
8+
test('select field with enum', async () => {
9+
const schema: RJSFSchema = {
10+
type: 'object',
11+
properties: {
12+
color: {
13+
type: 'string',
14+
enum: ['red', 'green', 'blue'],
15+
},
16+
},
17+
};
18+
const uiSchema: UiSchema = {
19+
color: {
20+
'ui:widget': 'NativeSelectWidget',
21+
'ui:placeholder': 'Pick something',
22+
},
23+
};
24+
const { asFragment } = render(<Form schema={schema} uiSchema={uiSchema} validator={validator} />);
25+
expect(asFragment()).toMatchSnapshot();
26+
});
27+
test('select field multiple choice', async () => {
28+
const schema: RJSFSchema = {
29+
type: 'array',
30+
items: {
31+
type: 'string',
32+
enum: ['foo', 'bar', 'fuzz', 'qux'],
33+
},
34+
uniqueItems: true,
35+
};
36+
const uiSchema: UiSchema = {
37+
'ui:widget': 'NativeSelectWidget',
38+
'ui:placeholder': 'Pick something',
39+
};
40+
const { asFragment } = render(<Form schema={schema} uiSchema={uiSchema} validator={validator} />);
41+
expect(asFragment()).toMatchSnapshot();
42+
});
43+
test('select field multiple choice with labels', async () => {
44+
const schema: RJSFSchema = {
45+
type: 'array',
46+
items: {
47+
type: 'number',
48+
anyOf: [
49+
{
50+
enum: [1],
51+
title: 'Blue',
52+
},
53+
{
54+
enum: [2],
55+
title: 'Red',
56+
},
57+
{
58+
enum: [3],
59+
title: 'Green',
60+
},
61+
],
62+
},
63+
uniqueItems: true,
64+
};
65+
const uiSchema: UiSchema = {
66+
'ui:widget': 'NativeSelectWidget',
67+
'ui:placeholder': 'Pick something',
68+
};
69+
const { asFragment } = render(<Form schema={schema} uiSchema={uiSchema} validator={validator} />);
70+
expect(asFragment()).toMatchSnapshot();
71+
});
72+
});

0 commit comments

Comments
 (0)