Skip to content

Commit

Permalink
chore: generate random id/htmlFor if no exist
Browse files Browse the repository at this point in the history
  • Loading branch information
mleralec committed Jan 5, 2022
1 parent d2bf428 commit 47c8b29
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/Field/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ describe('<Field />', () => {
const label = field.querySelector('label')
const input = field.querySelector('input')

expect(label.htmlFor).toBe('field')
expect(label.htmlFor).toBe(input.id)
userEvent.click(label)
expect(input).toHaveFocus()
expect(document.activeElement).toBe(input)
Expand All @@ -143,12 +145,14 @@ describe('<Field />', () => {
const label = field.querySelector('label')
const input = field.querySelector('input')

expect(label.htmlFor).toBe('field')
expect(label.htmlFor).toBe(input.id)
userEvent.click(label)
expect(input).toHaveFocus()
expect(document.activeElement).toBe(input)
})

it.skip('should focus input when click on label when using neither name nor id on input (the fallback value of the id is created randomly)', async () => {
it('should focus input when click on label when using neither name nor id on input (the fallback value of the id is created randomly)', async () => {
const { getByTestId } = render(
<Field dataTestId="field" label={labelText}>
<Input />
Expand All @@ -159,6 +163,8 @@ describe('<Field />', () => {
const label = field.querySelector('label')
const input = field.querySelector('input')

expect(label.htmlFor.substring(0, 10)).toBe('wui-field-')
expect(label.htmlFor).toBe(input.id)
userEvent.click(label)
expect(input).toHaveFocus()
expect(document.activeElement).toBe(input)
Expand Down
4 changes: 2 additions & 2 deletions packages/Field/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CreateWuiProps, forwardRef } from '@welcome-ui/system'
// Fields
import { RowContainer } from './layout'
import * as S from './styles'
import { getBaseType, getVariant } from './utils'
import { generateRandomId, getBaseType, getVariant } from './utils'

export type Size = 'sm' | 'md' | 'lg'

Expand Down Expand Up @@ -49,7 +49,7 @@ export const Field = forwardRef<'div', FieldProps>(
const Container = layout === 'row' ? RowContainer : Fragment
const variant = getVariant({ error, warning })
const hintText = variant ? error || warning : hint
const htmlFor = children.props.id || children.props.name
const htmlFor = children.props.id || children.props.name || generateRandomId()

const child = React.cloneElement(React.Children.only(children), {
disabled,
Expand Down
2 changes: 2 additions & 0 deletions packages/Field/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export const getVariant = ({ error, warning }: VariantProps): VariantReturn => {
if (warning) return 'warning'
return undefined
}

export const generateRandomId = (): string => `wui-field-${Math.random().toString(36).slice(2)}`

0 comments on commit 47c8b29

Please sign in to comment.