@focuson/form_components
Version:
Components that can be used by @focuson/forms
95 lines (94 loc) • 5.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LabelAndMonthYearLength = exports.EndMonthYearDatePickerWithLength = exports.StartMonthYearDatePickerWithLength = void 0;
const state_1 = require("@focuson/state");
const datePicker_1 = require("./datePicker");
const lens_1 = require("@focuson/lens");
const labelAndInput_1 = require("./labelAndInput");
const transformers_1 = require("./transformers");
const utils_1 = require("@focuson/utils");
function StartMonthYearDatePickerWithLength(props) {
const { endDatePath, lengthPath, dateFormat, id, showMonthYearPicker } = props;
if (dateFormat !== 'MM-yyyy')
throw new Error(`The date picker with id ${id} has a dateformat '${dateFormat}'. It must be MM-yyyy`);
if (!showMonthYearPicker)
throw new Error(`The date picker with id ${id} doesn't have 'showMonthYearPicker' set to true`);
function onCheck(debug, props) {
return (eventName, date) => {
var _a;
const { id, state, regexForChange, parentState, onChange } = props;
const length = (_a = (0, utils_1.numberOrUndefined)(lengthPath.optJson())) === null || _a === void 0 ? void 0 : _a.toString();
const endDate = endDatePath.optJson();
const s = { length, endDate };
const newS = (0, utils_1.setStartDate)(s, date, debug);
const changeTxs = regexForChange === undefined || (date && date.match(regexForChange) !== null) ? (0, labelAndInput_1.makeInputChangeTxs)(id, parentState, onChange) : [];
const txs = [
[state.optional, () => date],
[endDatePath.optional, () => newS.endDate],
[lengthPath.optional, () => newS.length],
...changeTxs
];
if (debug)
console.log('StartMonthYearDatePickerWithLength', id, date, 'length', length, newS, newS, 'txs', (0, lens_1.displayTransformsInState)(state, txs));
state.massTransform((0, state_1.reasonFor)('DatePicker', eventName, id))(...txs);
};
}
return (0, datePicker_1.RawDatePicker)(onCheck)(props);
}
exports.StartMonthYearDatePickerWithLength = StartMonthYearDatePickerWithLength;
function EndMonthYearDatePickerWithLength(props) {
const { startDatePath, lengthPath, dateFormat, id, showMonthYearPicker } = props;
if (dateFormat !== 'MM-yyyy')
throw new Error(`The date picker with id ${id} has a dateformat '${dateFormat}'. It must be MM-yyyy`);
if (!showMonthYearPicker)
throw new Error(`The date picker with id ${id} doesn't have 'showMonthYearPicker' set to true`);
function onCheck(debug, props) {
return (eventName, date) => {
var _a;
const { id, state, regexForChange, parentState, onChange } = props;
const length = (_a = (0, utils_1.numberOrUndefined)(lengthPath.optJson())) === null || _a === void 0 ? void 0 : _a.toString();
const startDate = startDatePath.optJson();
const s = { length, startDate };
const newS = (0, utils_1.setEndDate)(s, date, debug);
const changeTxs = regexForChange === undefined || (date && date.match(regexForChange) !== null) ? (0, labelAndInput_1.makeInputChangeTxs)(id, parentState, onChange) : [];
const txs = [
[state.optional, () => date],
[startDatePath.optional, () => newS.startDate],
[lengthPath.optional, () => newS.length],
...changeTxs
];
if (debug)
console.log('EndMonthYearDatePickerWithLength', id, date, 'length', length, newS, newS, 'txs', (0, lens_1.displayTransformsInState)(state, txs));
state.massTransform((0, state_1.reasonFor)('DatePicker', eventName, id))(...txs);
};
}
return (0, datePicker_1.RawDatePicker)(onCheck)(props);
}
exports.EndMonthYearDatePickerWithLength = EndMonthYearDatePickerWithLength;
const LabelAndMonthYearLength = props => {
const selectFn = (state, id, value, parentState, onChange) => {
var _a, _b;
const main = state.main;
const debug = (_a = main === null || main === void 0 ? void 0 : main.debug) === null || _a === void 0 ? void 0 : _a.dateDebug;
const { fromDate, toDate } = props;
const length = (_b = (0, utils_1.numberOrUndefined)(value)) === null || _b === void 0 ? void 0 : _b.toString();
const startS = { startDate: fromDate.optJson(), endDate: toDate.optJson() };
const newS = (0, utils_1.setLength)(startS, length, debug);
const txs = [
[state.optional, () => length],
[fromDate.optional, () => newS.startDate],
[toDate.optional, () => newS.endDate],
...(0, labelAndInput_1.makeInputChangeTxs)(id, parentState, onChange)
];
if (debug)
console.log('LabelAndMonthYearLength', id, 'from', fromDate.optJson(), 'value', value, 'newS', newS, 'tx', (0, lens_1.displayTransformsInState)(state, txs));
state.massTransform((0, state_1.reasonFor)('Input', 'onChange', id))(...txs);
};
const labelAndNumber = (0, labelAndInput_1.LabelAndTInput)(Object.assign(Object.assign({}, transformers_1.StringTransformer), { selectFn }));
const propsForLabel = Object.assign({}, props);
delete propsForLabel.subtract;
delete propsForLabel.toDate;
delete propsForLabel.fromDate;
return labelAndNumber(propsForLabel);
};
exports.LabelAndMonthYearLength = LabelAndMonthYearLength;