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

[RFR] Migrate RadioButtonGroupInput to TypeScript #3539

Merged
merged 2 commits into from
Aug 21, 2019
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
3 changes: 2 additions & 1 deletion packages/ra-ui-materialui/src/input/CheckboxGroupInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { FieldTitle, useInput, InputProps } from 'ra-core';
import defaultSanitizeRestProps from './sanitizeRestProps';
import CheckboxGroupInputItem from './CheckboxGroupInputItem';
import { CheckboxProps } from '@material-ui/core/Checkbox';
import { InputWithOptionsProps } from './InputWithOptions';

const sanitizeRestProps = ({
setFilter,
Expand Down Expand Up @@ -90,7 +91,7 @@ const useStyles = makeStyles(theme => ({
* The object passed as `options` props is passed to the material-ui <Checkbox> components
*/
const CheckboxGroupInput: FunctionComponent<
InputProps<CheckboxProps> & FormControlProps
InputWithOptionsProps & InputProps<CheckboxProps> & FormControlProps
> = ({
choices,
helperText,
Expand Down
12 changes: 12 additions & 0 deletions packages/ra-ui-materialui/src/input/InputWithOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ReactElement } from 'react';

type OptionTextFunction<ChoiceType = any> = (record: ChoiceType) => string;

export interface InputWithOptionsProps<ChoiceType = any> {
choices: ChoiceType[];
optionText:
| string
| OptionTextFunction<ChoiceType>
| ReactElement<{ record: ChoiceType }>;
optionValue: string;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import React from 'react';
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import {
makeStyles,
FormControl,
FormHelperText,
InputLabel,
FormLabel,
RadioGroup,
} from '@material-ui/core';
import { RadioGroupProps } from '@material-ui/core/RadioGroup';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mui components props have to be imported this way

import { FormControlProps } from '@material-ui/core/FormControl';
import get from 'lodash/get';
import { useInput, FieldTitle } from 'ra-core';
import { useInput, FieldTitle, InputProps } from 'ra-core';

import sanitizeRestProps from './sanitizeRestProps';
import InputHelperText from './InputHelperText';
import RadioButtonGroupInputItem from './RadioButtonGroupInputItem';
import { InputWithOptionsProps } from './InputWithOptions';

const useStyles = makeStyles({
label: {
Expand Down Expand Up @@ -77,7 +80,9 @@ const useStyles = makeStyles({
*
* The object passed as `options` props is passed to the material-ui <RadioButtonGroup> component
*/
export const RadioButtonGroupInput = ({
export const RadioButtonGroupInput: FunctionComponent<
InputWithOptionsProps & InputProps<RadioGroupProps> & FormControlProps
> = ({
choices,
classes: classesOverride,
helperText,
Expand Down Expand Up @@ -117,14 +122,14 @@ export const RadioButtonGroupInput = ({
margin="normal"
{...sanitizeRestProps(rest)}
>
<InputLabel component="legend" shrink className={classes.label}>
<FormLabel component="legend" className={classes.label}>
<FieldTitle
label={label}
source={source}
resource={resource}
isRequired={isRequired}
/>
</InputLabel>
</FormLabel>

<RadioGroup id={id} {...input} {...options}>
{choices.map(choice => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const RadioButtonGroupInputItem = ({
}) => {
const translate = useTranslate();

const choiceName = isValidElement(optionText) // eslint-disable-line no-nested-ternary
const choiceName = isValidElement<{ record: any }>(optionText)
? cloneElement(optionText, { record: choice })
: typeof optionText === 'function'
? optionText(choice)
Expand Down