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

Remove onChangeHandler from Select and TextField #1576

Merged
merged 1 commit into from
Jan 18, 2023
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
15 changes: 12 additions & 3 deletions packages/toolpad-components/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ export interface SelectOption {
label?: string;
}

export type SelectProps = TextFieldProps & {
export type SelectProps = Omit<TextFieldProps, 'value' | 'onChange'> & {
value: string;
onChange: (newValue: string) => void;
options: (string | SelectOption)[];
};

function Select({ options, value, defaultValue, fullWidth, sx, ...rest }: SelectProps) {
function Select({ options, value, onChange, defaultValue, fullWidth, sx, ...rest }: SelectProps) {
const handleChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event.target.value);
},
[onChange],
);

return (
<TextField
select
sx={{ ...(!fullWidth && !value ? { width: 120 } : {}), ...sx }}
fullWidth={fullWidth}
value={value}
onChange={handleChange}
{...rest}
>
{options.map((option) => {
Expand Down Expand Up @@ -49,7 +59,6 @@ export default createComponent(Select, {
helperText: 'The currently selected value.',
typeDef: { type: 'string' },
onChangeProp: 'onChange',
onChangeHandler: (event: React.ChangeEvent<HTMLSelectElement>) => event.target.value,
defaultValue: '',
defaultValueProp: 'defaultValue',
},
Expand Down
16 changes: 12 additions & 4 deletions packages/toolpad-components/src/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ import {
import { createComponent } from '@mui/toolpad-core';
import { SX_PROP_HELPER_TEXT } from './constants';

export type TextFieldProps = MuiTextFieldProps & {
export type TextFieldProps = Omit<MuiTextFieldProps, 'value' | 'onChange'> & {
value: string;
onChange: (newValue: string) => void;
alignItems?: BoxProps['alignItems'];
justifyContent?: BoxProps['justifyContent'];
};

function TextField({ defaultValue, ...props }: TextFieldProps) {
return <MuiTextField {...props} />;
function TextField({ defaultValue, onChange, ...props }: TextFieldProps) {
const handleChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
onChange(event.target.value);
},
[onChange],
);

return <MuiTextField {...props} onChange={handleChange} />;
}

export default createComponent(TextField, {
Expand All @@ -24,7 +33,6 @@ export default createComponent(TextField, {
helperText: 'The value that is controlled by this text input.',
typeDef: { type: 'string' },
onChangeProp: 'onChange',
onChangeHandler: (event: React.ChangeEvent<HTMLInputElement>) => event.target.value,
defaultValue: '',
defaultValueProp: 'defaultValue',
},
Expand Down