From f4a9b4a58dd0c3ed9e97d31ced042e165a1c2652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claud=C3=A9ric=20Demers?= Date: Mon, 21 Jan 2019 23:22:50 -0500 Subject: [PATCH] feat: allow helperContainer prop to be a function returning an HTMLElement (#489) --- README.md | 46 +++++++++++++++++----------------- src/SortableContainer/index.js | 10 +++++++- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 7faf8bd4e..e2d2d5ecc 100644 --- a/README.md +++ b/README.md @@ -97,29 +97,29 @@ There are already a number of great Drag & Drop libraries out there (for instanc #### SortableContainer HOC -| Property | Type | Default | Description | -| :------------------------- | :---------------------------------------------------- | :--------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| axis | String | `y` | Items can be sorted horizontally, vertically or in a grid. Possible values: `x`, `y` or `xy` | -| lockAxis | String | | If you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop. Possible values: `x` or `y`. | -| helperClass | String | | You can provide a class you'd like to add to the sortable helper to add some styles to it | -| transitionDuration | Number | `300` | The duration of the transition when elements shift positions. Set this to `0` if you'd like to disable transitions | -| pressDelay | Number | `0` | If you'd like elements to only become sortable after being pressed for a certain time, change this property. A good sensible default value for mobile is `200`. Cannot be used in conjunction with the `distance` prop. | -| pressThreshold | Number | `5` | Number of pixels of movement to tolerate before ignoring a press event. | -| distance | Number | `0` | If you'd like elements to only become sortable after being dragged a certain number of pixels. Cannot be used in conjunction with the `pressDelay` prop. | -| shouldCancelStart | Function | [Function](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L48) | This function is invoked before sorting begins, and can be used to programatically cancel sorting before it begins. By default, it will cancel sorting if the event target is either an `input`, `textarea`, `select` or `option`. | -| updateBeforeSortStart | Function | | This function is invoked before sorting begins. It can return a promise, allowing you to run asynchronous updates (such as `setState`) before sorting begins. `function({node, index, collection}, event)` | -| onSortStart | Function | | Callback that is invoked when sorting begins. `function({node, index, collection}, event)` | -| onSortMove | Function | | Callback that is invoked during sorting as the cursor moves. `function(event)` | -| onSortOver | Function | | Callback that is invoked when moving over an item. `function({index, oldIndex, newIndex, collection}, e)` | -| onSortEnd | Function | | Callback that is invoked when sorting ends. `function({oldIndex, newIndex, collection}, e)` | -| useDragHandle | Boolean | `false` | If you're using the `SortableHandle` HOC, set this to `true` | -| useWindowAsScrollContainer | Boolean | `false` | If you want, you can set the `window` as the scrolling container | -| hideSortableGhost | Boolean | `true` | Whether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling. | -| lockToContainerEdges | Boolean | `false` | You can lock movement of the sortable element to it's parent `SortableContainer` | -| lockOffset | `OffsetValue`\* \| [`OffsetValue`\*, `OffsetValue`\*] | `"50%"` | When `lockToContainerEdges` is set to `true`, this controls the offset distance between the sortable helper and the top/bottom edges of it's parent `SortableContainer`. Percentage values are relative to the height of the item currently being sorted. If you wish to specify different behaviours for locking to the _top_ of the container vs the _bottom_, you may also pass in an `array` (For example: `["0%", "100%"]`). | -| getContainer | Function | | Optional function to return the scrollable container element. This property defaults to the `SortableContainer` element itself or (if `useWindowAsScrollContainer` is true) the window. Use this function to specify a custom container object (eg this is useful for integrating with certain 3rd party components such as `FlexTable`). This function is passed a single parameter (the `wrappedInstance` React element) and it is expected to return a DOM element. | -| getHelperDimensions | Function | [Function](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L58) | Optional `function({node, index, collection})` that should return the computed dimensions of the SortableHelper. See [default implementation](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L58) for more details | -| helperContainer | HTMLElement | `document.body` | By default, the cloned sortable helper is appended to the document body. Use this prop to specify a different container for the sortable clone to be appended to | +| Property | Type | Default | Description | +| :------------------------- | :---------------------------------------------------- | :------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| axis | String | `y` | Items can be sorted horizontally, vertically or in a grid. Possible values: `x`, `y` or `xy` | +| lockAxis | String | | If you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop. Possible values: `x` or `y`. | +| helperClass | String | | You can provide a class you'd like to add to the sortable helper to add some styles to it | +| transitionDuration | Number | `300` | The duration of the transition when elements shift positions. Set this to `0` if you'd like to disable transitions | +| pressDelay | Number | `0` | If you'd like elements to only become sortable after being pressed for a certain time, change this property. A good sensible default value for mobile is `200`. Cannot be used in conjunction with the `distance` prop. | +| pressThreshold | Number | `5` | Number of pixels of movement to tolerate before ignoring a press event. | +| distance | Number | `0` | If you'd like elements to only become sortable after being dragged a certain number of pixels. Cannot be used in conjunction with the `pressDelay` prop. | +| shouldCancelStart | Function | [Function](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L48) | This function is invoked before sorting begins, and can be used to programatically cancel sorting before it begins. By default, it will cancel sorting if the event target is either an `input`, `textarea`, `select` or `option`. | +| updateBeforeSortStart | Function | | This function is invoked before sorting begins. It can return a promise, allowing you to run asynchronous updates (such as `setState`) before sorting begins. `function({node, index, collection}, event)` | +| onSortStart | Function | | Callback that is invoked when sorting begins. `function({node, index, collection}, event)` | +| onSortMove | Function | | Callback that is invoked during sorting as the cursor moves. `function(event)` | +| onSortOver | Function | | Callback that is invoked when moving over an item. `function({index, oldIndex, newIndex, collection}, e)` | +| onSortEnd | Function | | Callback that is invoked when sorting ends. `function({oldIndex, newIndex, collection}, e)` | +| useDragHandle | Boolean | `false` | If you're using the `SortableHandle` HOC, set this to `true` | +| useWindowAsScrollContainer | Boolean | `false` | If you want, you can set the `window` as the scrolling container | +| hideSortableGhost | Boolean | `true` | Whether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling. | +| lockToContainerEdges | Boolean | `false` | You can lock movement of the sortable element to it's parent `SortableContainer` | +| lockOffset | `OffsetValue`\* \| [`OffsetValue`\*, `OffsetValue`\*] | `"50%"` | When `lockToContainerEdges` is set to `true`, this controls the offset distance between the sortable helper and the top/bottom edges of it's parent `SortableContainer`. Percentage values are relative to the height of the item currently being sorted. If you wish to specify different behaviours for locking to the _top_ of the container vs the _bottom_, you may also pass in an `array` (For example: `["0%", "100%"]`). | +| getContainer | Function | | Optional function to return the scrollable container element. This property defaults to the `SortableContainer` element itself or (if `useWindowAsScrollContainer` is true) the window. Use this function to specify a custom container object (eg this is useful for integrating with certain 3rd party components such as `FlexTable`). This function is passed a single parameter (the `wrappedInstance` React element) and it is expected to return a DOM element. | +| getHelperDimensions | Function | [Function](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L74-L77) | Optional `function({node, index, collection})` that should return the computed dimensions of the SortableHelper. See [default implementation](https://github.com/clauderic/react-sortable-hoc/blob/master/src/SortableContainer/index.js#L74-L77) for more details | +| helperContainer | HTMLElement \| Function | `document.body` | By default, the cloned sortable helper is appended to the document body. Use this prop to specify a different container for the sortable clone to be appended to. Accepts an `HTMLElement` or a function returning an `HTMLElement` that will be invoked before right before sorting begins | \* `OffsetValue` can either be a finite `Number` or a `String` made up of a number and a unit (`px` or `%`). Examples: `10` (which is the same as `"10px"`), `"50%"` diff --git a/src/SortableContainer/index.js b/src/SortableContainer/index.js index e02683897..6384033c9 100644 --- a/src/SortableContainer/index.js +++ b/src/SortableContainer/index.js @@ -105,10 +105,12 @@ export default function sortableContainer( ]), getContainer: PropTypes.func, getHelperDimensions: PropTypes.func, - helperContainer: + helperContainer: PropTypes.oneOfType( + PropTypes.func, typeof HTMLElement === 'undefined' ? PropTypes.any : PropTypes.instanceOf(HTMLElement), + ), }; static childContextTypes = { @@ -883,6 +885,12 @@ export default function sortableContainer( } get helperContainer() { + const {helperContainer} = this.props; + + if (typeof helperContainer === 'function') { + return helperContainer(); + } + return this.props.helperContainer || this.document.body; } };