Skip to content

Commit

Permalink
FormFileUpload: Deprecate 36px default size (#67438)
Browse files Browse the repository at this point in the history
* FormFileUpload: Deprecate 36px default size

* Add changelog

* Tweak descriptions

Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
3 people authored Dec 2, 2024
1 parent b61f913 commit 8d80471
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 58 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- `CustomSelectControl`: Deprecate 36px default size ([#67441](https://github.com/WordPress/gutenberg/pull/67441)).
- `NumberControl`: Deprecate 36px default size ([#66730](https://github.com/WordPress/gutenberg/pull/66730)).
- `UnitControl`: Deprecate 36px default size ([#66791](https://github.com/WordPress/gutenberg/pull/66791)).
- `FormFileUpload`: Deprecate 36px default size ([#67438](https://github.com/WordPress/gutenberg/pull/67438)).

### Enhancements

Expand Down
106 changes: 58 additions & 48 deletions packages/components/src/form-file-upload/README.md
Original file line number Diff line number Diff line change
@@ -1,95 +1,105 @@
# FormFileUpload

FormFileUpload is a component that allows users to select files from their local device.
<!-- This file is generated automatically and cannot be edited directly. Make edits via TypeScript types and TSDocs. -->

## Usage
<p class="callout callout-info">See the <a href="https://wordpress.github.io/gutenberg/?path=/docs/components-formfileupload--docs">WordPress Storybook</a> for more detailed, interactive documentation.</p>

FormFileUpload allows users to select files from their local device.

```jsx
import { FormFileUpload } from '@wordpress/components';

const MyFormFileUpload = () => (
<FormFileUpload
accept="image/*"
onChange={ ( event ) => console.log( event.currentTarget.files ) }
>
Upload
</FormFileUpload>
<FormFileUpload
__next40pxDefaultSize
accept="image/*"
onChange={ ( event ) => console.log( event.currentTarget.files ) }
>
Upload
</FormFileUpload>
);
```

## Props

The component accepts the following props. Props not included in this set will be passed to the `Button` component.
### `__next40pxDefaultSize`

Start opting into the larger default height that will become the default size in a future version.

- Type: `boolean`
- Required: No
- Default: `false`

### accept
### `accept`

A string passed to `input` element that tells the browser which file types can be upload to the upload by the user use. e.g: `image/*,video/*`.
More information about this string is available in https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers.
A string passed to the `input` element that tells the browser which
[file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers)
can be uploaded by the user. e.g: `image/*,video/*`.

- Type: `String`
- Required: No
- Type: `string`
- Required: No

### children
### `children`

Children are passed as children of `Button`.

- Type: `Boolean`
- Required: No
- Type: `ReactNode`
- Required: No

### icon
### `icon`

The icon to render. Supported values are: Dashicons (specified as strings), functions, Component instances and `null`.
The icon to render in the default button.

- Type: `String|Function|Component|null`
- Required: No
- Default: `null`
See the `Icon` component docs for more information.

### multiple
- Type: `IconType`
- Required: No

### `multiple`

Whether to allow multiple selection of files or not.

- Type: `Boolean`
- Required: No
- Default: `false`
- Type: `boolean`
- Required: No
- Default: `false`

### onChange
### `onChange`

Callback function passed directly to the `input` file element.

Select files will be available in `event.currentTarget.files`.

- Type: `Function`
- Required: Yes
- Type: `ChangeEventHandler<HTMLInputElement>`
- Required: Yes

### onClick
### `onClick`

Callback function passed directly to the `input` file element.

This can be useful when you want to force a `change` event to fire when the user chooses the same file again. To do this, set the target value to an empty string in the `onClick` function.
This can be useful when you want to force a `change` event to fire when
the user chooses the same file again. To do this, set the target value to
an empty string in the `onClick` function.

```jsx
<FormFileUpload
onClick={ ( event ) => ( event.target.value = '' ) }
onChange={ onChange }
__next40pxDefaultSize
onClick={ ( event ) => ( event.target.value = '' ) }
onChange={ onChange }
>
Upload
Upload
</FormFileUpload>
```

- Type: `Function`
- Required: No

### render
- Type: `MouseEventHandler<HTMLInputElement>`
- Required: No

Optional callback function used to render the UI. If passed, the component does not render the default UI (a button) and calls this function to render it. The function receives an object with property `openFileDialog`, a function that, when called, opens the browser native file upload modal window.
### `render`

- Type: `Function`
- Required: No
Optional callback function used to render the UI.

### __next40pxDefaultSize

Start opting into the larger default height that will become the default size in a future version.
If passed, the component does not render the default UI (a button) and
calls this function to render it. The function receives an object with
property `openFileDialog`, a function that, when called, opens the browser
native file upload modal window.

- Type: `Boolean`
- Required: No
- Default: `false`
- Type: `(arg: { openFileDialog: () => void; }) => ReactNode`
- Required: No
5 changes: 5 additions & 0 deletions packages/components/src/form-file-upload/docs-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "../../schemas/docs-manifest.json",
"displayName": "FormFileUpload",
"filePath": "./index.tsx"
}
13 changes: 12 additions & 1 deletion packages/components/src/form-file-upload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import { useRef } from '@wordpress/element';
import Button from '../button';
import type { WordPressComponentProps } from '../context';
import type { FormFileUploadProps } from './types';
import { maybeWarnDeprecated36pxSize } from '../utils/deprecated-36px-size';

/**
* FormFileUpload is a component that allows users to select files from their local device.
* FormFileUpload allows users to select files from their local device.
*
* ```jsx
* import { FormFileUpload } from '@wordpress/components';
*
* const MyFormFileUpload = () => (
* <FormFileUpload
* __next40pxDefaultSize
* accept="image/*"
* onChange={ ( event ) => console.log( event.currentTarget.files ) }
* >
Expand All @@ -40,6 +42,15 @@ export function FormFileUpload( {
ref.current?.click();
};

if ( ! render ) {
maybeWarnDeprecated36pxSize( {
componentName: 'FormFileUpload',
__next40pxDefaultSize: props.__next40pxDefaultSize,
// @ts-expect-error - We don't "officially" support all Button props but this likely happens.
size: props.size,
} );
}

const ui = render ? (
render( { openFileDialog } )
) : (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const Template: StoryFn< typeof FormFileUpload > = ( props ) => {
export const Default = Template.bind( {} );
Default.args = {
children: 'Select file',
__next40pxDefaultSize: true,
};

export const RestrictFileTypes = Template.bind( {} );
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/form-file-upload/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import FormFileUpload from '..';
import _FormFileUpload from '..';

/**
* Browser dependencies
*/
const { File } = window;

const FormFileUpload = (
props: React.ComponentProps< typeof _FormFileUpload >
) => <_FormFileUpload __next40pxDefaultSize { ...props } />;

// @testing-library/user-event considers changing <input type="file"> to a string as a change, but it do not occur on real browsers, so the comparisons will be against this result
const fakePath = expect.objectContaining( {
target: expect.objectContaining( {
Expand Down
18 changes: 10 additions & 8 deletions packages/components/src/form-file-upload/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ export type FormFileUploadProps = {
*/
__next40pxDefaultSize?: boolean;
/**
* A string passed to `input` element that tells the browser which file types can be
* upload to the upload by the user use. e.g: `image/*,video/*`.
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers.
* A string passed to the `input` element that tells the browser which
* [file types](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers)
* can be uploaded by the user. e.g: `image/*,video/*`.
*/
accept?: InputHTMLAttributes< HTMLInputElement >[ 'accept' ];
/**
* Children are passed as children of `Button`.
*/
children?: ReactNode;
/**
* The icon to render in the `Button`.
* The icon to render in the default button.
*
* See the `Icon` component docs for more information.
*/
icon?: ComponentProps< typeof Icon >[ 'icon' ];
/**
Expand All @@ -50,10 +51,11 @@ export type FormFileUploadProps = {
*
* ```jsx
* <FormFileUpload
* onClick={ ( event ) => ( event.target.value = '' ) }
* onChange={ onChange }
* __next40pxDefaultSize
* onClick={ ( event ) => ( event.target.value = '' ) }
* onChange={ onChange }
* >
* Upload
* Upload
* </FormFileUpload>
* ```
*/
Expand Down

0 comments on commit 8d80471

Please sign in to comment.