Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions addons/hr_holidays/static/src/views/carryover_day_selection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useProps } from "@odoo/owl";
import { registry } from "@web/core/registry";
import { SelectionField, selectionField } from "@web/views/fields/selection/selection_field";


export class CarryoverDaySelection extends SelectionField {
props = useProps();

get options() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a good practice to add

/**
 * @override
 * /
 get options(){

As it comes from SelectionField

const allOptions = super.options;
const monthFieldName = this.props.monthField;
const currentMonth = this.props.record.data[monthFieldName];

if (!monthFieldName || !currentMonth) {
return allOptions;
}

const monthMaxDays = {
"1": 31, "2": 29, "3": 31, "4": 30, "5": 31, "6": 30,
"7": 31, "8": 31, "9": 30, "10": 31, "11": 30, "12": 31
};
Comment on lines +18 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can try to make it prettier, something should exists to get the last day of a given month without hardcoding it 👀 (using luxon or standard js Date 🤷‍♂️)

const maxAllowedDays = monthMaxDays[currentMonth];

return allOptions.filter((option) => parseInt(option[0], 10) <= maxAllowedDays);
}
}

registry.category("fields").add("carryover_day_widget", {
...selectionField,
component: CarryoverDaySelection,

supportedOptions: [
...(selectionField.supportedOptions || []),
{ name: "month_field", type: "string" }
],

extractProps: (fieldInfo, dynamicInfo) => {
const props = selectionField.extractProps(fieldInfo, dynamicInfo);

if (fieldInfo.options && fieldInfo.options.month_field) {
props.monthField = fieldInfo.options.month_field;
}

return props;
}
Comment on lines +32 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good, you can use attrs too. options are more used to set multiples values that will be evaluated. and attrs will be just string. so in your case, an attr is more adapted here I think 👀
And don't forget to add fieldDependencies, so that you'll not be forced to add the field in the view in invisible to be able to retreive it in the js record. 😄

});
8 changes: 4 additions & 4 deletions addons/hr_holidays/views/hr_leave_accrual_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@
</span>
<span name="biyearly" invisible="frequency != 'biyearly'">
on the
<field nolabel="1" name="first_month_day" class="o_hr_narrow_field-3" placeholder="select a day" required="frequency == 'biyearly'"/>
<field nolabel="1" name="first_month_day" class="o_hr_narrow_field-3" widget="carryover_day_widget" options="{'month_field': 'first_month'}" placeholder="select a day" required="frequency == 'biyearly'"/>
of
<field name="first_month" class="o_hr_narrow_field-5" placeholder="select a month" required="frequency == 'biyearly'"/>
and the
<field nolabel="1" name="second_month_day" class="o_hr_narrow_field-3" placeholder="select a day" required="frequency == 'biyearly'"/>
<field nolabel="1" name="second_month_day" class="o_hr_narrow_field-3" widget="carryover_day_widget" options="{'month_field': 'second_month'}" placeholder="select a day" required="frequency == 'biyearly'"/>
of
<field nolabel="1" name="second_month" class="o_hr_narrow_field-5" placeholder="select a month" required="frequency == 'biyearly'"/>
</span>
<span name="yearly" invisible="frequency != 'yearly'">
on the
<field nolabel="1" name="yearly_day" class="o_hr_narrow_field-3" required="frequency == 'yearly'" placeholder="select a day"/>
<field nolabel="1" name="yearly_day" class="o_hr_narrow_field-3" widget="carryover_day_widget" options="{'month_field': 'yearly_month'}" required="frequency == 'yearly'" placeholder="select a day"/>
of
<field nolabel="1" name="yearly_month" class="o_hr_narrow_field-5" required="frequency == 'yearly'" placeholder="select a month"/>
</span>
Expand Down Expand Up @@ -205,7 +205,7 @@
<span id="carryover_custom_date">
: the
<field name="carryover_day" placeholder="select a day"
required="carryover_date == 'other'"/>
required="carryover_date == 'other'" widget="carryover_day_widget" options="{'month_field': 'carryover_month'}" class="w-auto"/>
of
<field name="carryover_month" placeholder="select a month"
required="carryover_date == 'other'"/>
Expand Down