Skip to content

Commit

Permalink
feat(fields): add TextInput (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Nov 28, 2022
1 parent 414c4da commit cdd610f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/fields/TextInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useCallback, useMemo } from 'react'
import { Input } from 'rsuite'
import styled from 'styled-components'

import type { InputProps } from 'rsuite'
import type { Promisable } from 'type-fest'

export type TextInputProps = Omit<InputProps, 'as' | 'defaultValue' | 'onChange' | 'value'> & {
defaultValue?: string
name: string
onChange?: (nextValue: string | undefined) => Promisable<void>
}
export function TextInput({ onChange, ...originalProps }: TextInputProps) {
const key = useMemo(
() => `${originalProps.name}-${JSON.stringify(originalProps.defaultValue)}`,
[originalProps.defaultValue, originalProps.name]
)

const handleChange = useCallback(
(nextValue: string | null) => {
if (!onChange) {
return
}

const normalizedNextValue = nextValue && nextValue.trim().length ? nextValue : undefined

onChange(normalizedNextValue)
},
[onChange]
)

return <StyledInput key={key} onChange={handleChange} {...originalProps} />
}

export const StyledInput = styled(Input)`
background-color: ${p => p.theme.color.white};
`
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { DateRangePicker } from './fields/DateRangePicker'
export { DatePicker } from './fields/DatePicker'
export { MultiSelect } from './fields/MultiSelect'
export { Select } from './fields/Select'
export { TextInput } from './fields/TextInput'

export { FormikCheckbox } from './formiks/FormikCheckbox'
export { FormikDatePicker } from './formiks/FormikDatePicker'
Expand All @@ -23,6 +24,7 @@ export type { DateRangePickerProps } from './fields/DateRangePicker'
export type { DatePickerProps } from './fields/DatePicker'
export type { MultiSelectProps } from './fields/MultiSelect'
export type { SelectProps } from './fields/Select'
export type { TextInputProps } from './fields/TextInput'

export type { FormikCheckboxProps } from './formiks/FormikCheckbox'
export type { FormikDatePickerProps } from './formiks/FormikDatePicker'
Expand Down
39 changes: 39 additions & 0 deletions stories/fields/TextInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useState } from 'react'

import { TextInput } from '../../src'
import { Output } from '../_components/Output'

import type { TextInputProps } from '../../src'

const args: TextInputProps = {
defaultValue: undefined,
name: 'myTextInput'
}

export default {
title: 'Fields/TextInput',
component: TextInput,

argTypes: {
defaultValue: {
control: 'text'
},
isMulti: {
control: 'boolean'
}
},

args
}

export function _TextInput(props: TextInputProps) {
const [outputValue, setOutputValue] = useState<string | undefined | '∅'>('∅')

return (
<>
<TextInput {...props} onChange={setOutputValue} />

{outputValue !== '∅' && <Output value={outputValue} />}
</>
)
}

0 comments on commit cdd610f

Please sign in to comment.