|
| 1 | +import {dom, styled} from 'grainjs'; |
| 2 | +import {testId, vars} from 'app/client/ui2018/cssVars'; |
| 3 | +import {makeT} from 'app/client/lib/localization'; |
| 4 | +import {primaryButton} from 'app/client/ui2018/buttons'; |
| 5 | +import {iconSpan} from 'app/client/ui2018/icons'; |
| 6 | +import BaseView from 'app/client/components/BaseView'; |
| 7 | + |
| 8 | +const t = makeT('NewRecordButton'); |
| 9 | + |
| 10 | +const translationStrings = { |
| 11 | + 'record': 'New record', |
| 12 | + 'single': 'New card', |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * "New Record" button for the given view that inserts a new record at the end on click. |
| 17 | + * |
| 18 | + * Appears in the bottom-left corner of its parent element. |
| 19 | + */ |
| 20 | +export function newRecordButton(view: BaseView) { |
| 21 | + const viewType = view.viewSection.parentKey.peek(); |
| 22 | + |
| 23 | + const translationString = translationStrings[viewType as keyof typeof translationStrings] |
| 24 | + || 'New record'; |
| 25 | + return cssNewRecordButton( |
| 26 | + iconSpan('Plus'), |
| 27 | + dom('span', t(translationString)), |
| 28 | + dom.on('click', () => view.insertRow()), |
| 29 | + testId('new-record-button'), |
| 30 | + testId(`new-record-button-${view.constructor.name}`), |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +const cssNewRecordButton = styled(primaryButton, ` |
| 35 | + position: absolute; |
| 36 | + bottom: -12px; |
| 37 | + left: -12px; |
| 38 | + z-index: ${vars.newRecordButtonZIndex}; |
| 39 | + display: flex; |
| 40 | + align-items: center; |
| 41 | + gap: 6px; |
| 42 | +
|
| 43 | + /* 16px on the plus icon is blurry, 17px is sharp, needs more test. */ |
| 44 | + & > span:first-child { |
| 45 | + width: 17px; |
| 46 | + height: 17px; |
| 47 | + } |
| 48 | +`); |
0 commit comments