Skip to content

Commit

Permalink
docs: document maxLength property in TextField component
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartins90 committed Jan 8, 2025
1 parent 3d4c919 commit e1c5001
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/base-field/base-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ type BaseFieldProps = WithEnhancedClassName &
*/
maxWidth?: BoxProps['maxWidth']

/**
* The maximum number of characters that the input field can accept.
* When this limit is reached, the input field will not accept any more characters.
* The counter element will turn red when the number of characters is within 10 of the maximum limit.
*/
maxLength?: number

/**
* Used internally by components composed using `BaseField`. It is not exposed as part of
* the public props of such components.
Expand Down
22 changes: 22 additions & 0 deletions src/text-field/text-field.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,25 @@ Hence, the description is never needed when the field is not focused anyway.
<WithTooltipExample />
</Story>
</Canvas>

## Max length

The `maxLength` prop is used to limit the number of characters that the user can input. When the user tries to input more characters than the maximum allowed, the field won't accept the input.

export function WithMaxLengthExample() {
const [value, setValue] = React.useState('Doist')
return (
<TextField
label="Company name"
maxLength={30}
value={value}
onChange={(event) => setValue(event.currentTarget.value)}
/>
)
}

<Canvas withToolbar>
<Story name="Max Length">
<WithMaxLengthExample />
</Story>
</Canvas>
6 changes: 6 additions & 0 deletions src/text-field/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ interface TextFieldProps
type?: TextFieldType
startSlot?: React.ReactElement | string | number
endSlot?: React.ReactElement | string | number
/**
* The maximum number of characters that the input field can accept.
* When this limit is reached, the input field will not accept any more characters.
* The counter element will turn red when the number of characters is within 10 of the maximum limit.
*/
maxLength?: number
}

const TextField = React.forwardRef<HTMLInputElement, TextFieldProps>(function TextField(
Expand Down

0 comments on commit e1c5001

Please sign in to comment.