Skip to content

Commit

Permalink
[RFR] Migrate RadioButtonGroupInput to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Aug 19, 2019
1 parent caa931f commit f9dda1b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
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,16 +1,17 @@
import React from 'react';
import React, { FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import FormControl from '@material-ui/core/FormControl';
import FormControl, { FormControlProps } from '@material-ui/core/FormControl';
import FormHelperText from '@material-ui/core/FormHelperText';
import InputLabel from '@material-ui/core/InputLabel';
import RadioGroup from '@material-ui/core/RadioGroup';
import FormLabel from '@material-ui/core/FormLabel';
import RadioGroup, { RadioGroupProps } from '@material-ui/core/RadioGroup';
import { makeStyles } from '@material-ui/core/styles';
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 @@ -75,7 +76,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,
helperText,
label,
Expand Down Expand Up @@ -114,14 +117,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

0 comments on commit f9dda1b

Please sign in to comment.