Skip to content

Commit

Permalink
Add 'placeholder' attribute to taxonomy block and add 'placeholder' a… (
Browse files Browse the repository at this point in the history
woocommerce#46938)

* Add 'placeholder' attribute to taxonomy block and add 'placeholder' attribute to category field in Simple Product Template

* Only show placeholder when nothing is selected

* Remove suffix from function parameters

* Add changelog for component

* fix unit tests
  • Loading branch information
nathanss authored May 3, 2024
1 parent 163e579 commit 08508df
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/js/components/changelog/add-placeholder-taxonomy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: update

Show 'placeholder' in SelectTree only when nothing is selected
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ interface SelectTreeProps extends TreeControlProps {
export const SelectTree = function SelectTree( {
items,
treeRef: ref,
placeholder,
isLoading,
disabled,
initialInputValue,
Expand Down Expand Up @@ -92,6 +91,13 @@ export const SelectTree = function SelectTree( {
}
}, [ isFocused ] );

let placeholder: string | undefined = '';
if ( Array.isArray( props.selected ) ) {
placeholder = props.selected.length === 0 ? props.placeholder : '';
} else if ( props.selected ) {
placeholder = props.placeholder;
}

const inputProps: React.InputHTMLAttributes< HTMLInputElement > = {
className: 'woocommerce-experimental-select-control__input',
id: `${ props.id }-input`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@ describe( 'SelectTree', () => {
} );

it( 'should show the popover only when focused', () => {
const { queryByPlaceholderText, queryByText } = render(
const { queryByRole, queryByText } = render(
<SelectTree { ...DEFAULT_PROPS } />
);
expect( queryByText( 'Item 1' ) ).not.toBeInTheDocument();
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryByText( 'Item 1' ) ).toBeInTheDocument();
} );

it( 'should show create button when callback is true ', () => {
const { queryByText, queryByPlaceholderText } = render(
const { queryByText, queryByRole } = render(
<SelectTree
{ ...DEFAULT_PROPS }
shouldShowCreateButton={ () => true }
/>
);
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryByText( 'Create new' ) ).toBeInTheDocument();
} );
it( 'should not show create button when callback is false or no callback', () => {
const { queryByText, queryByPlaceholderText } = render(
const { queryByText, queryByRole } = render(
<SelectTree { ...DEFAULT_PROPS } />
);
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryByText( 'Create new' ) ).not.toBeInTheDocument();
} );
it( 'should show a root item when focused and child when expand button is clicked', () => {
const { queryByText, queryByLabelText, queryByPlaceholderText } =
const { queryByText, queryByLabelText, queryByRole } =
render( <SelectTree { ...DEFAULT_PROPS } /> );
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryByText( 'Item 1' ) ).toBeInTheDocument();

expect( queryByText( 'Item 2' ) ).not.toBeInTheDocument();
Expand All @@ -69,38 +69,38 @@ describe( 'SelectTree', () => {
} );

it( 'should show selected items', () => {
const { queryAllByRole, queryByPlaceholderText } = render(
const { queryAllByRole, queryByRole } = render(
<SelectTree { ...DEFAULT_PROPS } selected={ [ mockItems[ 0 ] ] } />
);
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryAllByRole( 'treeitem' )[ 0 ] ).toHaveAttribute(
'aria-selected',
'true'
);
} );

it( 'should show Create "<createValue>" button', () => {
const { queryByPlaceholderText, queryByText } = render(
const { queryByText, queryByRole } = render(
<SelectTree
{ ...DEFAULT_PROPS }
createValue="new item"
shouldShowCreateButton={ () => true }
/>
);
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
expect( queryByText( 'Create "new item"' ) ).toBeInTheDocument();
} );
it( 'should call onCreateNew when Create "<createValue>" button is clicked', () => {
const mockFn = jest.fn();
const { queryByPlaceholderText, queryByText } = render(
const { queryByRole, queryByText } = render(
<SelectTree
{ ...DEFAULT_PROPS }
createValue="new item"
shouldShowCreateButton={ () => true }
onCreateNew={ mockFn }
/>
);
queryByPlaceholderText( 'Type here' )?.focus();
queryByRole( 'textbox' )?.focus();
queryByText( 'Create "new item"' )?.click();
expect( mockFn ).toBeCalledTimes( 1 );
} );
Expand Down
4 changes: 4 additions & 0 deletions packages/js/product-editor/changelog/add-placeholder-taxonomy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add 'placeholder' attribute to taxonomy block
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ Help text that appears for the name field in the dialog that appears when creati

Label for the parent taxonomy field in the dialog that appears when creating a new taxonomy.

### placeholder

- **Type:** `String`
- **Required:** `No`

Placeholder for when the input field is empty.


## Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"parentTaxonomyText": {
"type": "string",
"__experimentalRole": "content"
},
"placeholder": {
"type": "string",
"__experimentalRole": "content"
}
},
"supports": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface TaxonomyBlockAttributes extends BlockAttributes {
createTitle: string;
dialogNameHelpText?: string;
parentTaxonomyText?: string;
placeholder?: string;
}

export function Edit( {
Expand All @@ -56,6 +57,7 @@ export function Edit( {
dialogNameHelpText,
parentTaxonomyText,
disabled,
placeholder,
} = attributes;
const [ searchValue, setSearchValue ] = useState( '' );
const [ allEntries, setAllEntries ] = useState< Taxonomy[] >( [] );
Expand Down Expand Up @@ -114,6 +116,7 @@ export function Edit( {
multiple
createValue={ searchValue }
onInputChange={ searchDelayed }
placeholder={ placeholder }
shouldNotRecursivelySelect
shouldShowCreateButton={ ( typedValue ) =>
! typedValue ||
Expand Down
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/add-category-placeholder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

New product editor: Add 'placeholder' attribute to category field in Simple Product Template
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ private function add_organization_group_blocks() {
'createTitle' => __( 'Create new category', 'woocommerce' ),
'dialogNameHelpText' => __( 'Shown to customers on the product page.', 'woocommerce' ),
'parentTaxonomyText' => __( 'Parent category', 'woocommerce' ),
'placeholder' => __( 'Search or create categories…', 'woocommerce' ),
),
)
);
Expand Down

0 comments on commit 08508df

Please sign in to comment.