Skip to content

Commit

Permalink
feat(input): use id instead of display name
Browse files Browse the repository at this point in the history
  • Loading branch information
andresz1 committed Jul 21, 2023
1 parent 37d228c commit cea1398
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 39 deletions.
14 changes: 11 additions & 3 deletions packages/components/input/src/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export interface InputProps extends InputPrimitiveProps {
onValueChange?: (value: string) => void
}

export const Input = forwardRef<HTMLInputElement, InputProps>(
({ className, asChild, onValueChange, onChange, onKeyDown, ...others }, ref) => {
const Root = forwardRef<HTMLInputElement, InputProps>(
(
{ className, asChild, onValueChange, onChange, onKeyDown, disabled: disabledProp, ...others },
ref
) => {
const field = useFormFieldControl()
const group = useInputGroup()

Expand All @@ -33,6 +36,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
} = group
const Component = asChild ? Slot : 'input'
const state = field.state ?? group.state
const disabled = group.disabled ?? disabledProp

const handleChange: ChangeEventHandler<HTMLInputElement> = event => {
if (onChange) {
Expand Down Expand Up @@ -69,7 +73,7 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
hasTrailingIcon: !!hasTrailingIcon,
hasClearButton: !!hasClearButton,
})}
disabled={group.disabled}
disabled={disabled}
required={isRequired}
aria-describedby={description}
aria-invalid={isInvalid}
Expand All @@ -81,4 +85,8 @@ export const Input = forwardRef<HTMLInputElement, InputProps>(
}
)

export const Input = Object.assign(Root, {
id: 'Input',
})

Input.displayName = 'Input'
2 changes: 1 addition & 1 deletion packages/components/input/src/InputAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export const InputAddon = forwardRef<HTMLDivElement, PropsWithChildren<InputAddo
}
)

InputAddon.displayName = 'InputAddon'
InputAddon.displayName = 'InputGroup.Addon'
6 changes: 5 additions & 1 deletion packages/components/input/src/InputClearButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface InputClearButtonProps extends ComponentPropsWithoutRef<'button'
'aria-label': string
}

export const InputClearButton = forwardRef<HTMLButtonElement, InputClearButtonProps>(
const Root = forwardRef<HTMLButtonElement, InputClearButtonProps>(
({ className, tabIndex = -1, onClick, ...others }, ref) => {
const { onClear, hasTrailingIcon } = useInputGroup()

Expand Down Expand Up @@ -45,4 +45,8 @@ export const InputClearButton = forwardRef<HTMLButtonElement, InputClearButtonPr
}
)

export const InputClearButton = Object.assign(Root, {
id: 'ClearButton',
})

InputClearButton.displayName = 'InputGroup.ClearButton'
25 changes: 10 additions & 15 deletions packages/components/input/src/InputGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ export const InputGroup = forwardRef<HTMLDivElement, PropsWithChildren<InputGrou
},
forwardRef
) => {
const getDisplayName = (element?: ReactElement) => {
return element ? (element.type as FC).displayName : ''
const getElementId = (element?: ReactElement) => {
return element ? (element.type as FC & { id?: string }).id : ''
}

const findElement = (...values: string[]) => {
return children.find(child => values.includes(getDisplayName(child) || ''))
return children.find(child => values.includes(getElementId(child) || ''))
}

const children = Children.toArray(childrenProp).filter(isValidElement)
const input = findElement('Input', 'Textarea') as
const input = findElement('Input') as
| DetailedReactHTMLElement<InputProps, HTMLInputElement>
| undefined
const props = input?.props || {}
Expand All @@ -67,17 +67,12 @@ export const InputGroup = forwardRef<HTMLDivElement, PropsWithChildren<InputGrou
props.onValueChange
)
const state = field.state ?? stateProp
const leadingAddon = findElement('InputGroup.LeadingAddon')
const leadingIcon = findElement('InputGroup.LeadingIcon', 'TextareaGroup.LeadingIcon')
const clearButton = findElement('InputGroup.ClearButton')
const trailingAddon = findElement('InputGroup.TrailingAddon')
const stateIndicator = findElement(
'InputGroup.StateIndicator',
'TextareaGroup.StateIndicator'
) || <InputStateIndicator />
const trailingIcon = state
? stateIndicator
: findElement('InputGroup.TrailingIcon', 'TextareaGroup.TrailingIcon')
const leadingAddon = findElement('LeadingAddon')
const leadingIcon = findElement('LeadingIcon')
const clearButton = findElement('ClearButton')
const trailingAddon = findElement('TrailingAddon')
const stateIndicator = findElement('StateIndicator') || <InputStateIndicator />
const trailingIcon = state ? stateIndicator : findElement('TrailingIcon')
const hasLeadingAddon = !!leadingAddon
const hasTrailingAddon = !!trailingAddon
const hasLeadingIcon = !!leadingIcon
Expand Down
4 changes: 2 additions & 2 deletions packages/components/input/src/InputIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const InputIcon = ({ className, intent, children, ...others }: InputIconP
intent={intent}
className={cx(
className,
'pointer-events-none absolute top-1/2 -translate-y-1/2',
'pointer-events-none absolute top-[calc(var(--sz-44)/2)] -translate-y-1/2',
intent ? undefined : 'text-neutral peer-focus:text-outline-high'
)}
{...others}
Expand All @@ -19,4 +19,4 @@ export const InputIcon = ({ className, intent, children, ...others }: InputIconP
)
}

InputIcon.displayName = 'InputIcon'
InputIcon.displayName = 'InputGroup.Icon'
20 changes: 11 additions & 9 deletions packages/components/input/src/InputLeadingAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { InputAddon, InputAddonProps } from './InputAddon'

export type InputLeadingAddonProps = InputAddonProps

export const InputLeadingAddon = forwardRef<HTMLDivElement, InputLeadingAddonProps>(
({ className, ...others }, ref) => (
<InputAddon
ref={ref}
className={cx(className, 'rounded-l-lg !rounded-r-none mr-[-1px]')}
{...others}
/>
)
)
const Root = forwardRef<HTMLDivElement, InputLeadingAddonProps>(({ className, ...others }, ref) => (
<InputAddon
ref={ref}
className={cx(className, 'rounded-l-lg !rounded-r-none mr-[-1px]')}
{...others}
/>
))

export const InputLeadingAddon = Object.assign(Root, {
id: 'LeadingAddon',
})

InputLeadingAddon.displayName = 'InputGroup.LeadingAddon'
1 change: 1 addition & 0 deletions packages/components/input/src/InputLeadingIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export const InputLeadingIcon = ({ className, ...others }: InputLeadingIconProps
<InputIcon className={cx(className, 'left-lg')} {...others} />
)

InputLeadingIcon.id = 'LeadingIcon'
InputLeadingIcon.displayName = 'InputGroup.LeadingIcon'
1 change: 1 addition & 0 deletions packages/components/input/src/InputStateIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export const InputStateIndicator = ({
)
}

InputStateIndicator.id = 'StateIndicator'
InputStateIndicator.displayName = 'InputGroup.StateIndicator'
6 changes: 5 additions & 1 deletion packages/components/input/src/InputTrailingAddon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InputAddon, InputAddonProps } from './InputAddon'

export type InputTrailingAddonProps = InputAddonProps

export const InputTrailingAddon = forwardRef<HTMLDivElement, InputTrailingAddonProps>(
const Root = forwardRef<HTMLDivElement, InputTrailingAddonProps>(
({ className, ...others }, ref) => (
<InputAddon
ref={ref}
Expand All @@ -15,4 +15,8 @@ export const InputTrailingAddon = forwardRef<HTMLDivElement, InputTrailingAddonP
)
)

export const InputTrailingAddon = Object.assign(Root, {
id: 'TrailingAddon',
})

InputTrailingAddon.displayName = 'InputGroup.TrailingAddon'
1 change: 1 addition & 0 deletions packages/components/input/src/InputTrailingIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export const InputTrailingIcon = ({ className, ...others }: InputTrailingIconPro
<InputIcon className={cx(className, 'right-lg')} {...others} />
)

InputTrailingIcon.id = 'TrailingIcon'
InputTrailingIcon.displayName = 'InputGroup.TrailingIcon'
7 changes: 0 additions & 7 deletions packages/components/input/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,3 @@ export const InputGroup: FC<InputGroupProps> & {
StateIndicator: InputStateIndicator,
ClearButton: InputClearButton,
})

InputGroup.LeadingAddon.displayName = 'InputGroup.LeadingAddon'
InputGroup.TrailingAddon.displayName = 'InputGroup.TrailingAddon'
InputGroup.LeadingIcon.displayName = 'InputGroup.LeadingIcon'
InputGroup.TrailingIcon.displayName = 'InputGroup.TrailingIcon'
InputGroup.StateIndicator.displayName = 'InputGroup.StateIndicator'
InputGroup.ClearButton.displayName = 'InputGroup.ClearButton'

0 comments on commit cea1398

Please sign in to comment.