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

[TypeScript] Fix missing fullWidth prop on <FilterLiveSearch> and harmonize label prop type between CommonInputProps and LabeledProps #7757

Merged
merged 4 commits into from
Jun 2, 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
8 changes: 7 additions & 1 deletion packages/ra-core/src/util/FieldTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ export interface FieldTitleProps {
isRequired?: boolean;
resource?: string;
source?: string;
label?: string | ReactElement | false;
label?: string | ReactElement | boolean;
}

export const FieldTitle = (props: FieldTitleProps) => {
const { source, label, resource, isRequired } = props;
const translateLabel = useTranslateLabel();

if (label === true) {
throw new Error(
'Label parameter must be a string, a ReactElement or false'
);
}

if (label === false || label === '') {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/Labeled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export interface LabeledProps extends StackProps {
fullWidth?: boolean;
htmlFor?: string;
isRequired?: boolean;
label?: string | ReactElement | false;
label?: string | ReactElement | boolean;
resource?: string;
source?: string;
}
Expand Down
110 changes: 54 additions & 56 deletions packages/ra-ui-materialui/src/list/filter/FilterLiveSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,63 +22,61 @@ import { TextInput } from '../../input';
* </Card>
* );
*/
export const FilterLiveSearch = memo(
(props: {
source?: string;
sx?: SxProps;
variant?: 'filled' | 'outlined';
}) => {
const { source = 'q', variant, ...rest } = props;
const { filterValues, setFilters } = useListFilterContext();
const translate = useTranslate();
export const FilterLiveSearch = memo((props: FilterLiveSearchProps) => {
const { source = 'q', variant, ...rest } = props;
const { filterValues, setFilters } = useListFilterContext();
const translate = useTranslate();

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target) {
setFilters(
{ ...filterValues, [source]: event.target.value },
null
);
} else {
const { [source]: _, ...filters } = filterValues;
setFilters(filters, null);
}
};
const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
if (event.target) {
setFilters({ ...filterValues, [source]: event.target.value }, null);
} else {
const { [source]: _, ...filters } = filterValues;
setFilters(filters, null);
}
};

const initialValues = useMemo(
() => ({
[source]: filterValues[source],
}),
[filterValues, source]
);
const initialValues = useMemo(
() => ({
[source]: filterValues[source],
}),
[filterValues, source]
);

const onSubmit = () => undefined;
let label = translate('ra.action.search');
const onSubmit = () => undefined;
let label = translate('ra.action.search');

return (
<Form defaultValues={initialValues} onSubmit={onSubmit}>
<TextInput
resettable
helperText={false}
source={source}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<SearchIcon color="disabled" />
</InputAdornment>
),
}}
onChange={handleChange}
size="small"
{...(variant === 'outlined'
? { variant: 'outlined', label }
: {
placeholder: label,
label: false,
hiddenLabel: true,
})}
{...rest}
/>
</Form>
);
}
);
return (
<Form defaultValues={initialValues} onSubmit={onSubmit}>
<TextInput
resettable
helperText={false}
source={source}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<SearchIcon color="disabled" />
</InputAdornment>
),
}}
onChange={handleChange}
size="small"
{...(variant === 'outlined'
? { variant: 'outlined', label }
: {
placeholder: label,
label: false,
hiddenLabel: true,
})}
{...rest}
/>
</Form>
);
});

export interface FilterLiveSearchProps {
source?: string;
sx?: SxProps;
fullWidth?: boolean;
variant?: 'filled' | 'outlined';
}