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

Issue on TypeScript FieldInputProps typings #693

Open
dolphinlin opened this issue Dec 11, 2019 · 1 comment
Open

Issue on TypeScript FieldInputProps typings #693

dolphinlin opened this issue Dec 11, 2019 · 1 comment

Comments

@dolphinlin
Copy link

dolphinlin commented Dec 11, 2019

about #606

Description

FieldInputProps extends AnyObject cause keyof unexpected operation.

microsoft/TypeScript#35600

example

type KeyOfFieldInputProps = keyof FieldInputProps<string, HTMLInputElement>;
// type KeyOfFieldInputProps = 'string' | 'number';

in my opinion, extending AnyObject is unnecessary, cuz the type definition must be exactly.

@dolphinlin dolphinlin changed the title Issue on TypeScript FieldRenderProps typings Issue on TypeScript FieldInputProps typings Dec 11, 2019
@dolphinlin
Copy link
Author

dolphinlin commented Dec 11, 2019

#441 (comment)

field shouldn't put props that not define in FieldInputProps, if we wanna pass props to down level component, we have to wrap a HOC or something else.

example

// DatePicker.tsx
import React, { useCallback } from 'react';
import moment from 'moment';
import { useField, UseFieldConfig } from 'react-final-form';

import { DatePicker, Form } from 'antd';
import { FormItemProps } from 'antd/lib/form';
import { DatePickerProps } from 'antd/lib/date-picker/interface';

type FinalDatePickerWrapperType = {
  name: string;
  formProps?: FormItemProps;
  pickerProps?: Omit<DatePickerProps, 'onChange'>;
  children?: React.ReactNode;
} & UseFieldConfig<number>;

type TimeChangeFunc = NonNullable<DatePickerProps['onChange']>;

const FinalDatePickerWrapper: React.FC<FinalDatePickerWrapperType> = ({
  name,
  formProps,
  pickerProps,
  children,
  ...config
}) => {
  const { input, meta } = useField<number, HTMLInputElement>(name, config);
  const handleTimeChange = useCallback<TimeChangeFunc>(
    m => input.onChange(m ? m.startOf('second').unix() : 0),
    [input],
  );

  return (
    <Form.Item help={meta.touched && !meta.active && meta.error} {...formProps}>
      <DatePicker
        {...pickerProps}
        onChange={handleTimeChange}
        onOpenChange={e => {
          if (e.valueOf()) {
            input.onFocus();
          } else {
            input.onBlur();
          }
        }}
        value={moment.unix(input.value)}
      />
    </Form.Item>
  );
};

export default FinalDatePickerWrapper;


// main.tsx
import DatePicker from './DatePicker';

<DatePicker formProps={{}} pickerProps={{}}/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant