Skip to content

Commit

Permalink
feat #218 - Add 'focused' prop to comps related to Form
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Feb 16, 2021
1 parent f5ba46c commit d64aaa3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 17 deletions.
16 changes: 7 additions & 9 deletions src/components/Button/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'antd/lib/button/style/index.css'
import 'antd/lib/spin/style/index.css'
import { Button as AntDButton } from 'antd'
import { ButtonProps as AntDButtonProps } from 'antd/es/button'
import classnames from 'classnames'
import { CommonComponentProps } from '../types'
import { createUseStyles } from 'react-jss'
Expand Down Expand Up @@ -38,6 +37,7 @@ export interface ButtonProps extends CommonComponentProps {
* Adds the disabled attribute and styles (opacity, gray scale filter, no pointer events).
*/
disabled?: boolean
focused?: boolean
/**
* Renders a skeleton for the button.
*/
Expand Down Expand Up @@ -65,27 +65,25 @@ export const Button: FC<ButtonProps> = ({
classes = [],
dataTag,
disabled = false,
focused = false,
loading = false,
onClick,
pending = false,
primary = false,
skeletonHeight = 32,
skeletonWidth = 75
}: ButtonProps) => {
const antDProps: AntDButtonProps = {
className: classnames(classes),
disabled: pending || disabled,
onClick,
type: primary ? 'primary' : 'default'
}

useStyles()

return loading ? (
<Skeleton height={skeletonHeight} width={skeletonWidth} />
) : (
<AntDButton
{...antDProps}
autoFocus={focused}
className={classnames(classes)}
disabled={pending || disabled}
onClick={onClick}
type={primary ? 'primary' : 'default'}
{...getDataTestAttributeProp('button', dataTag)}
>
{pending && (
Expand Down
9 changes: 2 additions & 7 deletions src/components/Form/FormInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getFormFieldDataTag } from '../utils'
import { Controller, useFormContext } from 'react-hook-form'
import FieldContext, { FieldContextProps } from '../FieldContext'
import { Input, InputProps } from 'components/Input'
import React, { FC, KeyboardEvent, useContext, useEffect, useRef } from 'react'
import React, { FC, KeyboardEvent, useContext, useRef } from 'react'

export interface FormInputProps
extends BaseFieldProps,
Expand Down Expand Up @@ -42,12 +42,6 @@ const FormInput: FC<FormInputProps> = ({
if (e.key === 'Enter') e.preventDefault()
}

useEffect(() => {
if (focused && inputRef.current) {
inputRef.current.focus()
}
}, [focused])

if (required) {
rules.required = true
}
Expand All @@ -70,6 +64,7 @@ const FormInput: FC<FormInputProps> = ({
<Input
dataTag={getFormFieldDataTag(name)}
error={errors[name]}
focused={focused}
fullWidth={fullWidth}
inputRef={inputRef}
loading={loading}
Expand Down
1 change: 0 additions & 1 deletion src/components/Form/FormTimeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export interface FormTimeInputProps
const FormTimeInput: FC<FormTimeInputProps> = ({
label,
labelSkeletonWidth,
focused,
fullWidth,
name,
required,
Expand Down
3 changes: 3 additions & 0 deletions src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface InputProps extends BaseFormElementProps {
addonAfter?: string
addonBefore?: string
inputRef?: RefObject<AntDInput>
focused?: boolean
onFocus?: () => void
onKeyDown?: (e: KeyboardEvent) => void
/**
Expand All @@ -53,6 +54,7 @@ export const Input: FC<InputProps> = (props: InputProps) => {
classes = [],
dataTag,
disabled = false,
focused = false,
inputRef,
onBlur = noop,
onChange,
Expand Down Expand Up @@ -93,6 +95,7 @@ export const Input: FC<InputProps> = (props: InputProps) => {
<AntDInput
addonAfter={addonAfter}
addonBefore={addonBefore}
autoFocus={focused}
className={cn(componentClasses.container, inputClasses)}
disabled={disabled}
onBlur={onBlur}
Expand Down
3 changes: 3 additions & 0 deletions src/components/TimeInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface TimeInputProps
* @default 'hh:mm A'
* */
displayFormat?: string
focused?: boolean
onChange?: (value: number) => void
onFocus?: () => void
/**
Expand Down Expand Up @@ -47,6 +48,7 @@ export const TimeInput: FC<TimeInputProps> = (props: TimeInputProps) => {
dataTag,
disabled = false,
displayFormat = 'hh:mm A',
focused = false,
onBlur = noop,
onChange,
onFocus = noop,
Expand Down Expand Up @@ -87,6 +89,7 @@ export const TimeInput: FC<TimeInputProps> = (props: TimeInputProps) => {
<InputSkeleton width={120} />
) : (
<AntDTimeInput
autoFocus={focused}
className={cn({ [componentClasses.error]: error }, classes)}
defaultValue={formatTime(format, defaultValue)}
disabled={disabled}
Expand Down

0 comments on commit d64aaa3

Please sign in to comment.