Skip to content

Commit

Permalink
fix: closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
viclafouch committed Sep 26, 2022
1 parent a1eb2fa commit 544e619
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/components/TextFieldChip/TextFieldChips.test.tsx
Expand Up @@ -340,4 +340,21 @@ describe('components/TextFieldChips', () => {

expect(screen.getByTestId('EmailIcon')).toBeTruthy()
})

test('should control the input value', async () => {
const callbackOnInputChange = vi.fn(() => {})
render(
<TextFieldChips
chips={['test']}
onInputChange={callbackOnInputChange}
inputValue="Hello world"
/>
)

expect(testUtils.getInputElement().value).toBe('Hello world')

await testUtils.typeInInputElement(' ')

expect(callbackOnInputChange).toBeCalledWith('Hello world ')
})
})
32 changes: 30 additions & 2 deletions src/components/TextFieldChip/TextFieldChips.tsx
Expand Up @@ -25,6 +25,7 @@ type TextFieldChipsProps = TextFieldProps & {
disableDeleteOnBackspace?: boolean
addOnWhichKey?: string | string[]
disableEdition?: boolean
inputValue?: string
validate?: MuiChipsInputProps['validate']
onInputChange?: (inputValue: string) => void
onDeleteChip?: (chipIndex: number) => void
Expand Down Expand Up @@ -61,11 +62,18 @@ const TextFieldChips = React.forwardRef(
className,
renderChip,
addOnWhichKey,
onFocus,
inputValue: inputValueControlled,
...restTextFieldProps
} = props
const [inputValue, setInputValue] = React.useState<string>('')
const [inputValueUncontrolled, setInputValueUncontrolled] =
React.useState<string>('')
const [textError, setTextError] = React.useState<string>('')
const inputElRef = React.useRef<HTMLDivElement | null>(null)
const isFocusingRef = React.useRef<boolean>(false)
const isControlledRef = React.useRef(
typeof inputValueControlled === 'string'
)
const [chipIndexEditable, setChipIndexEditable] = React.useState<
null | number
>(null)
Expand All @@ -76,9 +84,16 @@ const TextFieldChips = React.forwardRef(
setTextError('')
}

const isControlled = isControlledRef.current
const inputValue = isControlled
? (inputValueControlled as string)
: inputValueUncontrolled

const updateInputValue = (newInputValue: string) => {
onInputChange?.(newInputValue)
setInputValue(newInputValue)
if (!isControlled) {
setInputValueUncontrolled(newInputValue)
}
}

const updateChipIndexEditable = (chipIndex: number) => {
Expand All @@ -101,12 +116,17 @@ const TextFieldChips = React.forwardRef(
}

const handleClickAway = () => {
if (!isFocusingRef.current) {
return
}
if (chipIndexEditable !== null) {
clearChipIndexEditable()
clearInputValue()
} else if (clearInputOnBlur) {
clearInputValue()
}

isFocusingRef.current = false
}

const handleRef = (ref: HTMLDivElement | null): void => {
Expand Down Expand Up @@ -212,6 +232,12 @@ const TextFieldChips = React.forwardRef(
onKeyDown?.(event)
}

const handleFocus = (event: React.FocusEvent<HTMLInputElement>) => {
event.preventDefault()
onFocus?.(event)
isFocusingRef.current = true
}

const handleClearAll = (event: React.MouseEvent<HTMLButtonElement>) => {
event.preventDefault()
if (!hideClearAll && !disabled) {
Expand Down Expand Up @@ -254,6 +280,7 @@ const TextFieldChips = React.forwardRef(
className={`MuiChipsInput-TextField ${className || ''}`}
size={size}
placeholder="Type and press enter"
onFocus={handleFocus}
inputProps={{
onKeyDown: handleKeyDown,
...restInputProps
Expand Down Expand Up @@ -317,6 +344,7 @@ TextFieldChips.defaultProps = {
addOnWhichKey: KEYBOARD_KEY.enter,
onDeleteChip: () => {},
onAddChip: () => {},
inputValue: undefined,
onEditChip: () => {},
renderChip: undefined,
onDeleteAllChips: () => {},
Expand Down
2 changes: 2 additions & 0 deletions src/index.tsx
Expand Up @@ -34,6 +34,7 @@ const MuiChipsInput = React.forwardRef(
renderChip,
disableEdition,
addOnWhichKey,
inputValue,
...restTextFieldProps
} = props as Required<MuiChipsInputProps>

Expand Down Expand Up @@ -86,6 +87,7 @@ const MuiChipsInput = React.forwardRef(
disabled={disabled}
disableEdition={disableEdition}
validate={validate}
inputValue={inputValue}
hideClearAll={hideClearAll}
addOnWhichKey={addOnWhichKey}
{...restTextFieldProps}
Expand Down
1 change: 1 addition & 0 deletions src/index.types.ts
Expand Up @@ -27,6 +27,7 @@ export type MuiChipsInputChip = string
export interface BaseMuiChipsInputProps {
value?: MuiChipsInputChip[]
onInputChange?: (inputValue: string) => void
inputValue?: string
onAddChip?: (chipValue: MuiChipsInputChip, chipIndex: number) => void
onDeleteChip?: (chipValue: MuiChipsInputChip, chipIndex: number) => void
onEditChip?: (chipValue: MuiChipsInputChip, chipIndex: number) => void
Expand Down

0 comments on commit 544e619

Please sign in to comment.