choiceChecklistHelpers.js 797 B

123456789101112131415161718192021222324252627282930313233343536
  1. export function parseChecklistValue(value) {
  2. if (!value || value === '') {
  3. return []
  4. }
  5. return value.split(',').map((segment) => segment.trim()).filter((segment) => segment !== '')
  6. }
  7. export function formatChecklistValue(selected) {
  8. if (!Array.isArray(selected) || selected.length === 0) {
  9. return ''
  10. }
  11. return selected.join(',')
  12. }
  13. export function toggleChoice(selected, value) {
  14. const current = Array.isArray(selected) ? [...selected] : []
  15. const index = current.indexOf(value)
  16. if (index === -1) {
  17. current.push(value)
  18. return current
  19. }
  20. current.splice(index, 1)
  21. return current
  22. }
  23. export function choiceLabel(choice) {
  24. return choice.title || choice.value || ''
  25. }
  26. export function allChoiceValues(choices) {
  27. return choices.map((choice) => choice.value)
  28. }