sematable icon indicating copy to clipboard operation
sematable copied to clipboard

Provide function to AppsTableActions cell

Open mbury opened this issue 7 years ago • 3 comments

Hi, I want to ask how you resolve case when we must provide function to action cell?

class AppsTableActions extends Component {
  render() {
    const { row, onRmove } = this.props;
    return (
      <button onClick={() => onRmove(row.id)}>
        Remove
      </button>
    );
  }
}

For now I have two idea. First is use redux connect() to provide action creator to AppsTableActions. Second is generate columns by function and bind actions in component constructor:

const pepareCol = ({ onRemove }) => [
  {
    key: 'id',
    header: 'id',
  },
  {
    key: 'action',
    header: 'edit',
    Component: (props) => <ActionsCell {...props} onRemove={onRemove} />,
  },
];

  constructor(props) {
    super(props);
    this.columns = pepareCol({
      onRemove: props.removeEntity,
    });
  }

But maybe exists better solution?

mbury avatar Dec 04 '16 17:12 mbury

Hello,

I think the two examples you have are fine, especially connect()-ing the actions component. You can also use componentProps like this:

const pepareCol = ({ onRemove }) => [
  {
    key: 'id',
    header: 'id',
  },
  {
    key: 'action',
    header: 'edit',
    Component: ActionsCell,
    componentProps: { onRemove },
  },
];

  constructor(props) {
    super(props);
    this.columns = pepareCol({
      onRemove: props.removeEntity,
    });
  }

componentProps can be either an object, or a function that receives the row param, and returns an object of props that will be applied on the column component.

I think you may be trying to create a single sematable component where you can provide different callbacks for removing items. Hmm. In that case, I think we could change componentProps so it receives all table props. Then you wouldn't have to generate columns in constructor:

const columns = [
  {
    key: 'id',
    header: 'id',
  },
  {
    key: 'action',
    header: 'edit',
    Component: ActionsCell,
    componentProps: (row, tableProps) => ({
      onRemove: tableProps.onRemove,
    }),
  },
];

This requires some changes in Table, and TableRow.

amir-hadzic avatar Dec 05 '16 12:12 amir-hadzic

If you would like to make a PR for the componentProps changes, let me know if you have some questions.

amir-hadzic avatar Dec 05 '16 13:12 amir-hadzic

Thanks for response, Looks interesting. I will try implant it.

mbury avatar Dec 05 '16 20:12 mbury