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 SelectInput to TypeScript #3527

Merged
merged 2 commits into from
Aug 15, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import assert from 'assert';
import { render, cleanup, fireEvent } from '@testing-library/react';
import { Form } from 'react-final-form';
import { TranslationContext } from 'ra-core';
Expand All @@ -24,7 +23,9 @@ describe('<SelectInput />', () => {
render={() => <SelectInput {...defaultProps} />}
/>
);
const input = getByLabelText('resources.posts.fields.language');
const input = getByLabelText(
'resources.posts.fields.language'
) as HTMLInputElement;
expect(input.value).toEqual('ang');
});

Expand Down Expand Up @@ -290,7 +291,7 @@ describe('<SelectInput />', () => {
});

it('should use optionText with an element value as text identifier', () => {
const Foobar = ({ record }) => (
const Foobar = ({ record }: { record?: any }) => (
<span data-value={record.id} aria-label={record.foobar} />
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React, { useCallback } from 'react';
import React, { useCallback, FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import MenuItem from '@material-ui/core/MenuItem';
import { makeStyles } from '@material-ui/core/styles';
import { useInput, FieldTitle, useTranslate } from 'ra-core';
import { useInput, FieldTitle, useTranslate, InputProps } from 'ra-core';

import ResettableTextField from './ResettableTextField';
import InputHelperText from './InputHelperText';
import { TextFieldProps } from '@material-ui/core/TextField';

const sanitizeRestProps = ({
addLabel,
Expand Down Expand Up @@ -47,7 +48,7 @@ const sanitizeRestProps = ({
translateChoice,
validation,
...rest
}) => rest;
}: any) => rest;

const useStyles = makeStyles(theme => ({
input: {
Expand Down Expand Up @@ -129,7 +130,9 @@ const useStyles = makeStyles(theme => ({
* <SelectInput source="gender" choices={choices} disableValue="not_available" />
*
*/
const SelectInput = ({
const SelectInput: FunctionComponent<
InputProps<TextFieldProps> & Omit<TextFieldProps, 'label' | 'helperText'>
> = ({
allowEmpty,
choices,
className,
Expand Down Expand Up @@ -176,8 +179,8 @@ const SelectInput = ({

const renderMenuItemOption = useCallback(
choice => {
if (React.isValidElement(optionText)) {
return React.cloneElement(optionText, {
if (React.isValidElement<{ record: any }>(optionText)) {
return React.cloneElement<{ record: any }>(optionText, {
record: choice,
});
}
Expand Down