diff --git a/examples/official-storybook/stories/addon-controls.stories.tsx b/examples/official-storybook/stories/addon-controls.stories.tsx index 6dea53b8f168..94f29aa214ad 100644 --- a/examples/official-storybook/stories/addon-controls.stories.tsx +++ b/examples/official-storybook/stories/addon-controls.stories.tsx @@ -6,7 +6,7 @@ export default { component: Button, argTypes: { children: { control: 'text', name: 'Children', mapping: { basic: 'BASIC' } }, - type: { control: 'text', name: 'Type' }, + type: { name: 'Type', control: { type: 'text', maxLength: 32 } }, json: { control: 'object', name: 'JSON' }, imageUrls: { control: { type: 'file', accept: '.png' }, name: 'Image Urls' }, label: { @@ -71,8 +71,10 @@ export default { const DEFAULT_NESTED_OBJECT = { a: 4, b: { c: 'hello', d: [1, 2, 3] } }; const Template = (args) => ( -
- +
+ {args.json &&
{JSON.stringify(args.json, null, 2)}
}
); diff --git a/lib/components/src/controls/Text.tsx b/lib/components/src/controls/Text.tsx index bbf18c74b015..dc76b95b05ce 100644 --- a/lib/components/src/controls/Text.tsx +++ b/lib/components/src/controls/Text.tsx @@ -11,7 +11,22 @@ const Wrapper = styled.label({ display: 'flex', }); -export const TextControl: FC = ({ name, value, onChange, onFocus, onBlur }) => { +const MaxLength = styled.div<{ isMaxed: boolean }>(({ isMaxed }) => ({ + marginLeft: '0.75rem', + paddingTop: '0.35rem', + color: isMaxed ? 'red' : undefined, +})); + +const format = (value?: TextValue) => value || ''; + +export const TextControl: FC = ({ + name, + value, + onChange, + onFocus, + onBlur, + maxLength, +}) => { const handleChange = (event: ChangeEvent) => { onChange(event.target.value); }; @@ -34,6 +49,7 @@ export const TextControl: FC = ({ name, value, onChange, onFocus, onB = ({ name, value, onChange, onFocus, onB valid={isValid ? null : 'error'} {...{ name, value: isValid ? value : '', onFocus, onBlur }} /> + {maxLength && ( + + {value?.length ?? 0} / {maxLength} + + )} ); }; diff --git a/lib/components/src/controls/types.ts b/lib/components/src/controls/types.ts index 668c366f8eef..aee13cc9f191 100644 --- a/lib/components/src/controls/types.ts +++ b/lib/components/src/controls/types.ts @@ -61,7 +61,9 @@ export interface NormalizedOptionsConfig { } export type TextValue = string; -export interface TextConfig {} +export interface TextConfig { + maxLength?: number; +} export type ControlType = | 'array'