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

feat: add swipe actions support for Ti.UI.TableView #14065

Merged
merged 4 commits into from
Jun 25, 2024

Conversation

hansemannn
Copy link
Collaborator

@hansemannn hansemannn commented Jun 15, 2024

This PR adds support for swipe actions inside the Ti.UI.TableView component. It also refactors the old list view API for edit actions to use the new swipe actions API as well, which brings support for supporting both leading and trailing edit actions.

Demo.mp4

Test Case:

const win = Ti.UI.createWindow({ backgroundColor: 'white' });
const editActions = [ {
	identifier: 'approve',
	title: 'Approve',
	color: 'green',
	state: 'leading'
}, {
	identifier: 'edit',
	title: 'Edit',
	color: 'blue',
	state: 'trailing'
}, {
	identifier: 'insert',
	title: 'Insert',
	color: 'orange' // defaults to "trailing"
}, {
	identifier: 'delete',
	title: 'Delete',
	style: Ti.UI.iOS.ROW_ACTION_STYLE_DESTRUCTIVE,
	state: 'trailing'
} ];

const tableData = [
	{
		title: 'Apples'
	},
	{
		title: 'Bananas'
	},
	{
		editable: true, // used by table views
		canEdit: true, // used by list views
		title: 'Potatoes (swipe!)',
		editActions
	}
];

let selectedIndex = 0;
const [ table, list, tabs ] = [ createTable(), createList(), createTabs() ];

win.add([ table, list, tabs ]);
win.open();

function createTable() {
	const table = Ti.UI.createTableView({ top: 80, data: tableData });
	table.addEventListener('editaction', onEditActionSelected);

	return table;
}

function createList() {
	const listView = Ti.UI.createListView({
		visible: false,
		top: 80,
		sections: [ Ti.UI.createListSection({
			items: tableData.map(cell => ({ properties: cell }))
		}) ]
	});

	listView.addEventListener('editaction', onEditActionSelected);

	return listView;
}

function createTabs() {
	const tabs = Ti.UI.createTabbedBar({ labels: [ 'Table View', 'List View' ], index: selectedIndex, top: 55 });

	tabs.addEventListener('click', ({ index }) => {
		selectedIndex = index;

		if (index === 0) {
			list.visible = false;
			table.visible = true;
		} else {
			list.visible = true;
			table.visible = false;
		}
	});

	return tabs;
}

function onEditActionSelected(event) {
	console.warn(event);

	if (event.identifier === 'delete') {
		if (selectedIndex === 0) {
			table.deleteRow(event.row);
		} else {
			list.sections[0].deleteItemsAt(event.itemIndex, 1);
		}
	}
}

@m1ga
Copy link
Contributor

m1ga commented Jun 15, 2024

Tested the demo code and it runs fine. Items show up and the events are fired.
But didn't do any other tests or checks.

@dlewis23
Copy link
Contributor

Tested this myself today on my app and everything works great. I added it to two tables with no issues.

@hansemannn hansemannn merged commit 40808cd into tidev:master Jun 25, 2024
6 checks passed
@designbymind
Copy link

Working great on my end as well. Awesome work! 🪄

@hansemannn hansemannn deleted the feature/table-view-swipe-actions branch June 25, 2024 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants