0.5.3 • Published 2 years ago

react-selectable-items v0.5.3

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

react-selectable-items

render a row of selectable items

example

create a feedback rating component with stars as the items

import { SelectableItems, Unselected } from 'react-selectable-items'

type FeedbackRating = Unselected | 1 | 2 | 3 | 4 | 5

export function Stars() {
	const [starsSelectedUpUntil, setStarsSelectedUpUntil] = useState<FeedbackRating>(0)
	
	console.log({ starsSelectedUpUntil })

	return <SelectableItems<FeedbackRating>
		count={5}
		
		ItemEmpty={StarEmpty}
		ItemSelected={StarFilled}
		
		selectedUpUntil={starsSelectedUpUntil}
		setSelectedUpUntil={setStarsSelectedUpUntil}
		
		selectionStrategy="all-before-and-current"
		selectionDirection="left-to-right"
	/>
}
	
function StarEmpty() {
	return <span>⭐</span>
}

function StarFilled() {
	return <span>🌠</span>
}

styling

though, we've never actually used this ourselves

.selectable-items__initial-wrapper { }

.selectable-items__item { }

.selectable-items__item--selected { }
.selectable-items__item--not-selected { }

.selectable-items__item--first { }
.selectable-items__item--last { }

example with an error state

you can do all the same, but additionally provide a Wrapper prop, thus overriding our default & very minimal wrapper, and in your Wrapper you can e.g. show a red border if the field was required but wasn't selected.

import styled from '@emotion/styled';

export function Stars() {
	const [starsSelectedUpUntil, setStarsSelectedUpUntil] = useState<FeedbackRating>(0)
	const [hasError, setHasError] = useState<boolean>(false)

	return <SelectableItems<FeedbackRating>
		count={5}
		
		ItemEmpty={StarEmpty}
		ItemSelected={StarFilled}
		
		selectedUpUntil={starsSelectedUpUntil}
		setSelectedUpUntil={setStarsSelectedUpUntil}
		
		selectionStrategy="all-before-and-current"
		selectionDirection="left-to-right"

		Wrapper={({ children }) => (
			<ErrorBorderWrapper hasError={!!hasError}>
				{children}
			</ErrorBorderWrapper>
		)}
	/>
}

const ErrorBorderWrapper = styled.div<{ hasError: boolean }>`
	display: inline-block;

	${({ hasError }) =>
		!hasError
			? ``
			: `
		border: 1px solid red;
		border-radius: 4px;
	`}
`;

License

MIT (c) 2022 Kipras Melnikovas

0.5.3

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.5

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago