Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InputControl] Add prefix prop #23824

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/components/src/input-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ A function that receives the value of the input.
- Type: `Function`
- Required: Yes

### prefix

Renders an element on the left side of the input.

- Type: `React.ReactNode`
- Required: No

### size

Adjusts the size of the input.
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/input-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useState, forwardRef } from '@wordpress/element';
import Backdrop from './backdrop';
import InputField from './input-field';
import Label from './label';
import { Container, Root, Suffix } from './styles/input-control-styles';
import { Container, Root, Prefix, Suffix } from './styles/input-control-styles';
import { isValueEmpty } from '../utils/values';

function useUniqueId( idProp ) {
Expand All @@ -42,6 +42,7 @@ export function InputControl(
onFocus = noop,
onValidate = noop,
onKeyDown = noop,
prefix,
size = 'default',
suffix,
value,
Expand Down Expand Up @@ -93,6 +94,11 @@ export function InputControl(
disabled={ disabled }
isFocused={ isFocused }
>
{ prefix && (
<Prefix className="components-input-control__prefix">
{ prefix }
</Prefix>
) }
<InputField
{ ...props }
className="components-input-control__input"
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/input-control/stories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ function Example() {
'default'
),
suffix: text( 'suffix', '' ),
prefix: text( 'prefix', '' ),
};

const suffixMarkup = props.suffix ? <div>{ props.suffix }</div> : null;
const prefixMarkup = props.prefix ? <div>{ props.prefix }</div> : null;

return (
<InputControl
{ ...props }
onChange={ ( v ) => setValue( v ) }
prefix={ prefixMarkup }
suffix={ suffixMarkup }
value={ value }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ export const LegendText = ( props ) => (
<BaseLegendText { ...props } as="span" />
);

export const Prefix = styled.span`
box-sizing: border-box;
display: block;
`;

export const Suffix = styled.span`
box-sizing: border-box;
display: block;
Expand Down