Skip to content

Commit

Permalink
fix: InputColor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Innders committed Dec 31, 2024
1 parent 2593eb1 commit dd29c6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/Dropdowns/SortingDropdown/SortCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { forwardRef } from 'react'
import { SortCardType } from './SortingDropdown'
import { Icon } from '../../Icon'
import * as Styled from './SortCard.styled'
import clsx from 'clsx'

interface SortCardProps extends SortCardType, Omit<React.HTMLAttributes<HTMLDivElement>, 'id'> {
onRemove: () => void
Expand All @@ -12,7 +13,13 @@ interface SortCardProps extends SortCardType, Omit<React.HTMLAttributes<HTMLDivE
const SortCard = forwardRef<HTMLDivElement, SortCardProps>(
({ id, label, sortOrder, onRemove, onSortBy, disabled, ...props }, ref) => {
return (
<Styled.Card {...props} ref={ref} tabIndex={0} $disabled={!!disabled}>
<Styled.Card
{...props}
ref={ref}
tabIndex={0}
$disabled={!!disabled}
className={clsx('sort-chip', props.className)}
>
<Icon
icon="close"
onClick={(e) => {
Expand Down
9 changes: 6 additions & 3 deletions src/Inputs/InputColor/ColorPickerPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import clsx from 'clsx'
import { ChangeEvent, FC, FocusEvent, forwardRef } from 'react'
import styled, { css } from 'styled-components'

// types
export interface ColorPickerPreviewProps {
export interface ColorPickerPreviewProps extends React.HTMLAttributes<HTMLDivElement> {
onClick?: () => void
onChange?: (e: ChangeEvent<HTMLInputElement>) => void
backgroundColor?: string
Expand Down Expand Up @@ -68,16 +69,17 @@ const ColorButton = styled.button`
`

const ColorPickerPreview = forwardRef<HTMLDivElement, ColorPickerPreviewProps>(
({ onClick, onChange, backgroundColor, value, onBlur }, ref) => {
({ onClick, onChange, backgroundColor, value, onBlur, ...props }, ref) => {
return (
<Wrapper ref={ref}>
<Wrapper ref={ref} className={clsx('color-picker-preview', props.className)} {...props}>
<Input
type="color"
disabled={!onChange}
tabIndex={!onChange ? -1 : 0}
onChange={onChange && onChange}
value={value}
onBlur={onBlur}
className="color-picker-input"
/>
<Checkerboard />
<ColorButton
Expand All @@ -88,6 +90,7 @@ const ColorPickerPreview = forwardRef<HTMLDivElement, ColorPickerPreviewProps>(
disabled={!onClick}
tabIndex={!onClick ? -1 : 0}
onClick={onClick}
className="color-picker-button"
/>
</Wrapper>
)
Expand Down
12 changes: 7 additions & 5 deletions src/Inputs/InputColor/InputColor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ const formatsConfig = {
},
}

export interface InputColorProps {
export interface InputColorProps extends Omit<HTMLAttributes<HTMLDivElement>, 'onChange'> {
value: string | number[]
onChange: (event: { target: { value: string | number[] } }) => void
alpha?: boolean
format?: 'hex' | 'float' | 'uint8' | 'uint16'
className?: string
style?: HTMLAttributes<HTMLDivElement>['style']
pt?: {
preview?: HTMLAttributes<HTMLDivElement>
}
}

// REACT FUNCTIONAL COMPONENT
export const InputColor = forwardRef<HTMLDivElement, InputColorProps>(
({ value, onChange, alpha, format = 'hex', className, style }, ref) => {
({ value, onChange, alpha, format = 'hex', pt = { preview: {} }, ...props }, ref) => {
const isHex = format === 'hex'

let initValue: string | number[]
Expand Down Expand Up @@ -226,8 +227,9 @@ export const InputColor = forwardRef<HTMLDivElement, InputColorProps>(
const useDialog = alpha || ['uint16', 'float'].includes(format)

return (
<div ref={ref} className={className} style={style}>
<div ref={ref} {...props}>
<ColorPickerPreview
{...pt.preview}
onClick={useDialog ? handleOpenDialog : undefined}
backgroundColor={previewBG}
value={hex}
Expand Down

0 comments on commit dd29c6f

Please sign in to comment.