Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Select): handle "defaultValue" #23463

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix: handle \"defaultValue\"",
"packageName": "@fluentui/react-select",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe('Select', () => {
expect(result.container).toMatchSnapshot();
});

it('handles the defaultValue attribute', () => {
const { getByTestId } = render(
<Select defaultValue="B">
<option>A</option>
<option data-testid="option-b">B</option>
<option>C</option>
</Select>,
);

expect((getByTestId('option-b') as HTMLOptionElement).selected).toBeTruthy();
});

it('handles the disabled attribute', () => {
const { getByTestId } = render(<Select data-testid="select" disabled />);
expect((getByTestId('select') as HTMLSelectElement).disabled).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ import type { SelectProps, SelectState } from './Select.types';
* @param ref - reference to the `<select>` element in Select
*/
export const useSelect_unstable = (props: SelectProps, ref: React.Ref<HTMLSelectElement>): SelectState => {
const { select, icon, root, appearance = 'outline', onChange, size = 'medium' } = props;
const {
defaultValue,
value,
select,
icon,
root,
appearance = 'outline',

onChange,
size = 'medium',
} = props;

const nativeProps = getPartitionedNativeProps({
props,
primarySlotTagName: 'select',
excludedPropNames: ['appearance', 'onChange', 'size'],
excludedPropNames: ['appearance', 'defaultValue', 'onChange', 'size', 'value'],
});

const state: SelectState = {
Expand All @@ -32,6 +42,8 @@ export const useSelect_unstable = (props: SelectProps, ref: React.Ref<HTMLSelect
select: resolveShorthand(select, {
required: true,
defaultProps: {
defaultValue,
value,
ref,
...nativeProps.primary,
},
Expand All @@ -47,8 +59,7 @@ export const useSelect_unstable = (props: SelectProps, ref: React.Ref<HTMLSelect
};

state.select.onChange = useEventCallback(event => {
const value = (event.target as HTMLSelectElement).value;
onChange?.(event, { value });
onChange?.(event, { value: (event.target as HTMLSelectElement).value });
});

return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const InitialValue = () => {
return (
<>
<label htmlFor={selectId}>Color</label>
<Select id={selectId}>
<Select defaultValue="Green" id={selectId}>
<option>Red</option>
<option selected>Green</option>
<option>Green</option>
<option>Blue</option>
</Select>
</>
Expand Down